Description
1.
(define my-list (list 1 2 3 4))
(define (foo x)
(lambda (y)
(if (even? (+ x y))
(lambda (z) (* z 2))
(lambda (z) (* z 3)))))
(define (bar ls)
(if (empty? ls)
‘()
(cons ((car ls) 10) (bar (cdr ls)))))
Why is the output ‘(20 30 20 30)? To answer this question, trace and/or explain in detail the evaluation steps of (bar ( map (foo 3) my-list)) to show how the output ‘(20 30 20 30) is produced.
2.
A recursive, but not higher order function viral-growth that returns a list of the minute-by-minute growth when one person spreads a video to three other people per minute:
(define (viral-growth mins ppl)
(if (= 0 mins)
‘()
(cons (* 3 ppl) (viral-growth (- mins 1) (* 3 ppl)))))
> (viral-growth 4 1)
‘(3 9 27 81)
And a higher order function repeated that returns another function, f, applied count times:
(define (repeated f count)
(lambda (x)
(if (= count 0)
x
(f ((repeated f (- count 1)) x)))))
> ((repeated add-one 3) 1)
4
Rewrite viral-growth so that it can be passed to repeated and so that the count variable of repeated (rather than a mins variable in viral-growth) determines how many minutes of viral growth pass. Rewrite viral-growth to accept only one argument, the number of people who start the spread, and to return a lambda that accepts a list argument.
(define (viral-growth ppl)
(lambda (ls)
your code goes here
If you rewrite viral-growth correctly, you should be able to pass it to repeated as follows:
> ((repeated (viral-growth 1) 4) ‘())
‘(3 9 27 81)
An answer that returns the list in reverse order ‘(81 27 9 3) will be accepted but with an automatic deduction of 3 points. Also, do not use any built-in function except for empty?, *, cons, car, or cdr.