Warning! You are being recorded (and transcribed).
Approximate overview
cons.Academic/Scholarly
Cultural
Peer
Wellness
Misc
In helping students, Maddy and I have observed that one of the most important sets of qustions you should be asking yourself are “What type does this procedure take as input?” and “What type does this procedure return?”
That applies for both procedures you use and procedures you write.
For example, some of you were returning null when you should have
been returning a number (e.g., in tally-wins).
If you encounter runaway recursion,
(a) Make sure that you are simplifying an appropriate parameter.
(b) Make sure that the recursion happens after you ensure that you haven’t reached the base case.
(c) Reread your base-case test.
Can we go over the double dagger problems?
Sure!
I was unsure about how to combine cadr and caddr together in one command
as it gave me an unbound identifier error.
There’s a limit to how many
a’s andd’s you can have in thecaddadarprocedures.
You can use composition.
(o cadr caddr)is the same as(cadaddr).
Is there a way to implement a generalized version of car/cdr so that
when you get to the really complex structures, you can still type it in
shorthand (e.g., caadddadaddr or something)? Or do you just have to get
used to writing it differently when you get past a certain point?
You can write a recursive two-parameter procedure that takes the “addadada” as one parameter (probably a string) and the pair structure as another.
Stay tuned. Sam will think about it during lab.
Here we go.
;;; (c_r str) -> procedure?
;;; str : string? (consisting of only #\a and #\r)
;;; Build the procedure corresponding to c<str>r.
;;;
;;; For example, `(c_r "adddddd")` gives the equivalent of
;;; `caddddddr` (if it were defined).
(define c_r
(let ([letter->fun (lambda (ch) (if (char=? ch #\a) car cdr))])
(lambda (str)
(apply o (map letter->fun (string->list str))))))
(define caddddddr (c_r "adddddd"))
> (caddddddr (string->list "abcdefghijkl"))
#\g
Can you show an example of composing car and cdr.
Let’s write something that grabs element six of a list (using 0 as the first element).
We’d like to use
caddddddr
> (car letters)
'a
> (cadr letters)
'b
> (caddddddr letters)
. . caddddddr: undefined;
cannot reference an identifier before its definition
> (define caddddddr (o car cdr cdr cdr cdr cdr cdr))
> (caddddddr letters)
How should we get started on the most general carpet procedure?
;;; (carpet pattern size color-x color-y n) -> image?
;;; pattern ; string? (length 9, composed only of x, X, y, and Y)
;;; size : positive real?
;;; color-x : color?
;;; color-y : color?
;;; n : non-negative integer.
;;; Create a `size`-by-`size` image of a fractal carpet with `n` levels
;;; of recursion, using `color-x` as the "primary" color and `color-y`
;;; as the "secondary" color.
;;;
;;; The pattern is given by the letters in pattern, where `X` means
;;; "recurse" keeping colors as they are", `Y` means "recurse swapping
;;; the two colors", `x` means "square in `color-x`" and `y` means
;;; "square in `color-y`".
;;;
;;; The positions of the letters correspond to the parts of the pattern
;;;
;;; 0 1 2
;;; 3 4 5
;;; 6 7 8
string-ref.#\x, #\X,
#\y, or #\Y? That seems to be a case for a helper procedure.above and beside.let binding(define carpet
(lambda (...)
(if (base-case-test)
(solid-square ???)
(let ([x (solid-square ...)]
[X (carpet ...)]
[y (solid-square ...)]
[Y (carpet ...)])
(above (beside (helper (string-ref pattern 0) x X y Y)
(helper (string-ref pattern 1) x X y Y)
(helper (string-ref pattern 2) x X y Y))
...)))))
;;; (helper ch x X y Y) -> image?
;;; ch : char?
;;; x : img?
;;; X : img?
;;; y : img?
;;; Y : img?
;;; Pick one of the four images based on the value of `ch`.
(define helper
(lambda (ch)
(cond
[(equal? ch #\x)
x]
... // X, y, Y
[else
(error "Invalid character! Only x's and y's permitted.")])))
Some tips:
n items has n pairs (cons cells) in it.
(list 'a 'b 'c) ->