CS 440 Midterm Exam (Take-Home)
This midterm exam consists of 7 separate exercises --- 3 on recursion and lists, 3 on higher order functions, and 1 involving an addition to an interpreter. They are described below:
Part 2: Higher order functions
until: takes a predicate pred, a function fn, and a starting value x, and returns the list of
values (x, (fn x), (fn (fn x)), ...), terminating on the first value which satisfies pred.
E.g.,
> (until (lambda (x) (> x 100)) (lambda (x) (* 2 x))
1)
'(1 2 4 8 16 32 64)
> (until (curry = 10) add1 0) '(0 1 2 3 4 5 6 7 8 9)
●
alternately: takes a list of functions and a list of values, and returns the list created by
applying each function, in turn, to successive values of the list. E.g.,
> (alternately (list add1 sub1 sqr) (range 10)) '(1 0 4 4 3 25 7 6 64 10)
> (alternately (list string-upcase string-downcase string-length) (list "Hello" "How" "Are" "You" "This" "Fine" "Day?"))
'("HELLO" "how" 3 "YOU" "this" 4 "DAY?")
●
stride: a macro that takes a variable name var, a stride length n, a list lst, and an expression expr, and returns the list of values resulting from evaluating expr with var set to each n-th value from the lst. E.g.,
> (stride
'("HELLO"
> (stride
'(0 25 100 225 400 625)
x 2 '("hello" "how" "are" "you" "this" "fine" "day") (string-upcase x))
"ARE" "THIS" "DAY")
x 5 (range 30) (sqr x))