Due: before class on Monday 29 September

Except for problem #1, these are all Scheme programming problems.

We recommend you use the online IDE at https://try.scheme.org/ to work on these. If you click the little “+” next to “README.html”, you can create a new scheme file for you to save your definitions, and then save/load this into the interactive window on the left.

Be sure to print out your work and bring it to class by the deadline, just like with any other HW.

  1. Consider the following small Scheme program:

    (and (< 3 5) #t)

    Perform a complete tokenization and bottom-up parse of this program using the Scheme syntax as defined in class

    For the bottom-up parse, record an ordered list of decisions the parser makes. Each decision should be either:

    • shift the next token; or
    • reduce according to one of the grammar rules

    Here are the first few decisions, to get you started:

    • shift token LP (
    • shift token SYM and
    • reduce exp -> SYM
  2. Assume we have a variable x defined, like

    (define x 10)

    Write a Scheme expression which multiplies x by 3 and then adds 12.

  3. Define a Scheme function area that takes two arguments width and height, and returns the area of the resulting rectangle.

    It should work like this:

    > (area 3 4)
    12
    > (area 100 1)
    100
    > (area 8 50)
    400
  4. Define a Scheme function big-sum? that takes two integer arguments and returns true/false whether their sum is at least 100.

    It should work like this:

    > (big-sum? 50 5)
    #f
    > (big-sum? 0 1000)
    #t
    > (big-sum? 70 70)
    #t
    > (big-sum? 65 35)
    #t
  5. Define a Scheme function two-true? that takes three boolean arguments and checks whether exactly two of them are true.

    It should work like this:

    > (two-true? #t #t #f)
    #t
    > (two-true? #f #t #f)
    #f
    > (two-true? #f #t #t)
    #t
    > (two-true? #f #f #f)
    #f
    > (two-true? #t #t #t)
    #f
  6. Using only cons and '() and the components 1, 2, and 'fish, write a Scheme expression that evaluates to the list

    (1 fish 2 fish)
  7. Define a Scheme function get-second which takes any list and returns the second element in the list. You can assume the list has at least two elements to start out with.

    It should work like this:

    > (get-second '(a b c d e))
    b
    > (get-second (cons 1 (cons 2 (cons 3 '()))))
    2
  8. Define a Scheme function swap which takes any list with exactly two elements, and returns a new list with those same elements in reverse order.

    It should work like this:

    > (swap '(1 2))
    (2 1)
    > (swap '(guns roses))
    (roses guns)
  9. Here is a definition for a Scheme function called ?:

    (define ?
      (lambda (n)
        (and (>= n 1)
             (or (= n 1)
                 (? (/ n 2))))))

    Copy-paste this function into your Scheme IDE and experiment by running it on different arguments.

    1. How many arguments does ? take, and what type should the argument(s) be?

    2. What is the type of the return value from ??

    3. State what this function does, in plain English. It should only require a few words.

    4. Look at the code and try to understand it. Explain briefly how the function actually works.

  10. (bonus, optional)

    Explain what this function does and how it works:

    (define f
      (lambda (n)
        ((lambda (h) (h h 0 0 1))
         (lambda (h c v p)
           (if (= c n) v (h h (+ c 1) (+ v p) v))))))