Skip to main content

CSC 151.01, Class 30: Analyzing procedures

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
  • Debrief on yesterday’s lab.
  • Quick review of homework 6.
  • Lab.
  • Debrief.

News / Etc.

  • Quiz 9 returned.
  • If you take notes on the gridded cards, please discard your notes when you leave.
  • We will probably not get through more than half of today’s lab today. That’s okay. There’s lab time on Friday.
  • It’s preregistration time. Things to consider:
    • CSC 161 is an awesome course. I’m told it’s very different than
      1. (For some, that’s a positive. For others, that’s a negative.)
    • Wilson short course: Human Centered Design for Global Social Transformation
    • Wilson short course: Leadership in a Future of Automation and Income Inequality.
    • ENG 295-01. Lighting the Page: Digital Methods in Literary Studies. (ENG-120 prereq)
    • HIS 295-01. Digital Methods in Historical Studies. (HIS-100 prereq)

Upcoming Work

  • Writeup for class 29 due Wednesday at 10:30 p.m.
  • Read Project description for Friday’s class.
    • No, it’s not ready.
  • Exam 3
    • Prologue due Friday.
    • Exam due Tuesday the 14th.
    • Cover pages due Wednesday the 15th.
    • Epilogues due Wednesday the 15th.
  • Quiz Friday
    • Trees
    • Higher-order procedures
    • Files

Extra credit (Academic/Artistic)

Extra credit (Peer)

  • Voice Recital (students of Nicolas Miguel), Friday, 7ish, Sebring-Lewis

Extra credit (Misc)

  • Pioneer weekend. (Today is the last day to sign up.)

Other good things

  • Pub Quiz tonight! (At Bobs)
  • Anime talk tomorrow night.

Questions

On the exam

Can I use length on problem 1?
I’d prefer that you didn’t.
Snarky answer: Sure, if you don’t care about getting full credit.
I have questions about a particular strategy I’m using. Can I reveal it to the whole class or should I email you?
Email me.

On the analysis reading

On other topics

How do I write a procedure that turns a pair structure into a string?

(define pair-structure->string
  (lambda (val)
    (cond
      [(integer? val)
       (number->string val)]
      [(null? val)
       "()"]
      [(pair? val)
       (string-append "("
                      (pair-structure->string (car val))
                      ; Goal of kernel: Produce the string for everything that's left/
                      (let kernel ([remaining (cdr val)])
                        (cond
                          [(number? remaining)
                           (string-append " . " (number->string remaining))]
                          [(null? remaining)
                           ""]
                          [(pair? remaining)
                           (string-append " "
                                          (pair-structure->string (car remaining))
                                          (kernel (cdr remaining)))]
                          [else
                           " still-not-sure"]))
                      ")")]
      [else
       
       "I'm not ready to handle that yet"])))

Debrief from prior class

How did you figure out that the sum was correct?

  • (apply + (map string->number (file->words FILE)))
  • Open the file in DrRacket. Put (+ at the start. Put ‘)’ at the end. Evaluate.
  • Open the file in a spreadsheet and use the spreadsheet sum.

Notes on HW6

Problem 1: Partitition

  • Key idea: If you are partition a list of n elements into k pieces, Make a piece of size n/k and then recurse.
  • Partition takes as input a list and a positive integer and returns a list of lists.
  • Starting point: (if (= 1 k) (list lst) ...)
  • Why (list lst) and not lst? Because I know that my return type is “list of lists”.

Code

#lang racket
(require csc151)

; partition lst into k mostly-equal size pieces
(define partition1
  (lambda (lst k)
    (if (= 1 k)
        (list lst)
        ; Three things
        ;   Grab the first length/k elements
        ;   recurse on the remaining elements
        ;   Join 'em together
        (cons (take lst (ceiling (/ (length lst) k)))
              (partition1 (drop lst (ceiling (/ (length lst) k)))
                          (- k 1))))))
(define partition2
  (lambda (lst k)
    (if (= 1 k)
        (list lst)
        ; Three things
        ;   Grab the first length/k elements
        ;   recurse on the remaining elements
        ;   Join 'em together
        (let ([num-elements-in-first-list (ceiling (/ (length lst) k))])
          (cons (take lst num-elements-in-first-list)
                (partition2 (drop lst num-elements-in-first-list)
                            (- k 1)))))))

(define partition
  (lambda (lst k)
    (let kernel ([lst lst]
                 [k k]
                 [len (length lst)])
      (if (= 1 k)
          (list lst)
          ; Three things
          ;   Grab the first length/k elements
          ;   recurse on the remaining elements
          ;   Join 'em together
          (let ([num-elements-in-first-list (ceiling (/ len k))])
            (cons (take lst num-elements-in-first-list)
                  (kernel (drop lst num-elements-in-first-list)
                          (- k 1)
                          (- len num-elements-in-first-list))))))))

; Determine if a string is a palindrome.
; Goal: Use numeric recursion.

(define palindrome?
  (lambda (str)
    (let kernel ([left 0]
                 [right (- (string-length str) 1)])
      (if (>= left right)
          #t
          (and (char=? (string-ref str left) (string-ref str right))
               (kernel (increment left) (decrement right)))))))

(define remove-at
  (lambda (lst pos)
    (if (zero? pos)
        (cdr lst)
        (cons (car lst)
              (remove-at (cdr lst)
                         (decrement pos))))))

(define insert-at
  (lambda (lst val pos)
    (if (zero? pos)
        (cons val lst)
        (cons (car lst)
              (insert-at (cdr lst)
                         val
                         (decrement pos))))))

(define insert-everywhere
  (lambda (val lst)
    (let ([len (length lst)])
      (let kernel ([pos 0])
        (if (> pos len)
            null
            (cons (insert-at lst val pos)
                  (kernel (+ pos 1))))))))

(define perms
  (lambda (lst)
    ; Base case: Singleton or empty list
    (if (or (null? lst) (null? (cdr lst)))
        (list lst)
        (let ([partial (perms (cdr lst))] ; A list of permutations of part of the list
              [first (car lst)])
          ; Sam's goal: Insert first into each element of partial.
          ; Then join 'em together
          (let kernel ([remaining partial])
            (if (null? remaining)
                null
                (append (insert-everywhere first (car remaining))
                        (kernel (cdr remaining)))))))))

Lab

Debrief