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.
-
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
-
Assume we have a variable
xdefined, like(define x 10)Write a Scheme expression which multiplies x by 3 and then adds 12.
-
Define a Scheme function
areathat takes two argumentswidthandheight, 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 -
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 -
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 -
Using only
consand'()and the components1,2, and'fish, write a Scheme expression that evaluates to the list(1 fish 2 fish) -
Define a Scheme function
get-secondwhich 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 -
Define a Scheme function
swapwhich 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) -
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.
-
How many arguments does
?take, and what type should the argument(s) be? -
What is the type of the return value from
?? -
State what this function does, in plain English. It should only require a few words.
-
Look at the code and try to understand it. Explain briefly how the function actually works.
-
-
(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))))))