Approximate overview
Note: For mentor sessions, I’d like reflections and not just “this is what we did.
Write programs that produce unpredictable output.
Grinnell has suggested that we make up “random ids” for the students in our classes that we can use in online platforms.
Write a procedure, (random-id), that creates a string consisting of a random six-letter identifier of the form consonant-vowel-consonant-vowel-consonant-vowel, with all letters uppercase.
> (random-id)
"PALEQO"
> (random-id)
"ZEDUMI"
Isn’t that fun?
One of the points of the int-list->string exercise was that printing
of lists is optimistic in Racket. We assume we have a list until
proven otherwise.
We’ll use a local helper rather than the separate procedure. To
define local recursive helpers, we use letrec rather than `let.
(The reason is complicated.)
I’m using cond rather than if because I expect to add more cases.
(define int-list->string
(letrec ([; helper is used for all but the car; it add spaces and values
helper
(lambda (val)
(cond
[(null? val)
""]
[else
(string-append " "
(number->string (car val))
(helper (cdr val)))]))])
(lambda (val)
(cond
[(null? val)
"()"]
[else
(string-append "("
(number->string (car val))
(helper (cdr val))
")")]))))
Next version: Add support for a dot at the end.
; What if the pair structure does not end in null? What would you
; change in this code?
(define int-listish->string
(letrec ([; helper is used for all but the car; it add spaces and values
helper
(lambda (val)
(cond
[(null? val)
""]
[(number? val) ; ADDED
(string-append " . " (number->string val))] ; ADDED
[else
(string-append " "
(number->string (car val))
(helper (cdr val)))]))])
(lambda (val)
(cond
[(null? val)
"()"]
[else
(string-append "("
(number->string (car val))
(helper (cdr val))
")")]))))
Minor update: Allow it to handle integers as well as lists of integers.
(define int-thing->string
(letrec ([helper
(lambda (val)
(cond
[(null? val)
""]
[(number? val)
(string-append " . " (number->string val))]
[else
(string-append " "
(number->string (car val))
(helper (cdr val)))]))])
(lambda (val)
(cond
[(null? val)
"()"]
[(number? val)
(number->string val)]
[else
(string-append "("
(number->string (car val))
(helper (cdr val))
")")]))))
Next variant: Add support for nested structures.
(define int-thing->string
(letrec ([helper
(lambda (val)
(cond
[(null? val)
""]
[(number? val)
(string-append " . " (number->string val))]
[else
(string-append " "
(int-thing->string (car val))
(helper (cdr val)))]))])
(lambda (val)
(cond
[(null? val)
"()"]
[(number? val)
(number->string val)]
[else
(string-append "("
(int-thing->string (car val))
(helper (cdr val))
")")]))))
What’s a vector literal vs. a vector?
A vector literal is one we create with a tick.
'#(1 2 3)
A “normal” vector is one we create in almost any other way (with
vectorormake-vectororlist->vectoror …)
We often avoid vector literals because they do not permit
vector-set!(or other mutation).
Can we see an example of vector-set!
Yup
Why do the examples have so many /kernel at the end.
We use
/kernelto name our helper procedures.
I’ll work on changing that.
What is when?
(when TEST EXP1 EXP2 ...)is an alternate tocondandif. It first evaluatesTEST. If test holds, it evaluate all the expressions in turn. If the test doesn’t hold, thewhenreturns nothing.
Shorthand for
(cond [TEST EXP1 EXP2 ...]). Easier to read, formats better.
Can I think of it as a do-while loop?
I won’t stop you, but that’s not how most people think of it.
It is useful for recursive repetition.
Will we ever do while loops.
Nope. You have recursion. Why would you need anything else?
vectors.rkt.pg1260.txt, too.See board for memory model.
(time exp) evaluates the expression, prints out the time, and then
returns the value of the expression.