Functional Problem Solving (CSC 151 2015S) : EBoards

CSC151.01 2015S, Review Session Week 10: Pairs and Pair Structures


Overview

Your Exam Questions

In problem 4, why doesn't the turtle just do everything on its own?

From what you know, turtles only do things when you make them the parameter to a procedure. (turtle-forward! turtle 10).
But the "script" is not a list of full expressions; it's a list of procedures.

    (list
     ; A function that, given a turtle, makes the turtle go forward 15
     (r-s turtle-forward! 15) 
     ; A function that, given a turtle, tells the turtle to raise the
     ; pen
     turtle-up!
     ; A function that, given a turtle, tells the turtle to go forward
     ; 10
     (r-s turtle-forward! 10)
     ; Guess
     turtle-down!)

There is no automatic way in Scheme to say "Here's a list of four procedures. Run all four of them on the same turtle."

Your goal is to write something that does just that.

Can we talk about turtle-sparkle!? Let's look at a picture and see how it's constructed.

For the first sparkle, you can think of it in multiple ways.

Make three long lines (from the center), then three short lines (from the center).

Make three really long lines (not from the center). This seems to be a hard way to think about the problem.

Note that I wrote "long" and "short", but it's more "first distance" then "second distance".

For problem 5, why did you give us (sample-turtle)?

It makes the examples easier. It makes your development a bit easier. It shows good practice. It avoids the situation in which you say "My code doesn't work", but you don't send me an example.

For rdc in problem 2, should the output be the same order as the input?

Definitely!

Your Topical/Quiz Questions

Why in deep/tree recursion do you also have to recurse over the car?

Consider the following badly-drawn tree

                    OO
                   /  \
                  /    \
                 OO    OO
                /  \  /  \
               /   4 9    5
              OO
             /  \
            3   1

Suppose I want to find the smallest value in that tree. If I don't look in the car, I miss the 1.

Suppose I want to find the largest value. If I don't look in the cdr, I miss the 9.

About the Quiz

Main topics:

Likely quiz questions:

Example: Draw (a b c . d)

Sorry, you weren't here to see the whiteboard.

It's like a list, so we start by drawing a spine of a list.

But it has that weird "curse period", so we know that at the end, we have the last value, rather than null.

Three cons cells, the last of which is (cons c d).

Example: Interpret this code

    (define ccc
      (lambda (t)
        (if (pair? t)
            (+ 1 (ccc (car t)) (ccc (cdr t)))
            0)))

    > (define that-tree-I-drew-above
        (cons (cons (cons 3 1)
                    4)
              (cons 9 5)))
    > (ccc that-tree-I-drew-above)
    4

Example, continued: What does ccc compute?

The number of pairs (cons cells) in a pair structure

Example: Interpret this code

    (define vm
      (lambda (v)
        (let ([l (vector-length v))
          (let kernel ([i 1]
                       [m (vector-ref v 0)])
             (if (>= i l)
                 m
                 (kernel (+ i 1)
                         (max (vector-ref v i) m)))))))

    > (vm (vector 3 1 8 9 2))

Example: Finish this code

    (define number-tree-sum
      (lambda (ntree)
        (if __________________
            (+ ___________________________
               ___________________________)
            ntree)))