






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







;; list of all elements in L less than max (define (limit L max) (filter (lambda(x)(< x max)) L)) ;;........................
(define (filter2 pred? L) (define (loop L) (if (null? L) '() (let ((hd (car L)) (rst (cdr L))) (if (< hd max) ;;* (cons hd (loop rst)) (loop rst))))) (loop L))
(define (limit2 L max) (filter2 (lambda(x)(< x max)) L)) ;;........................
(define (filter3 max L) (define (loop L) (if (null? L) '() (let ((hd (car L)) (rst (cdr L))) (if (< hd max) (cons hd (loop rst)) (loop rst))))) (loop L))
(define (limit3 L max) (filter3 max L)) ;;* ;;........................
(define (filter4 L) ;;* (define (loop L) (if (null? L) '() (let ((hd (car L)) (rst (cdr L))) (if (< hd max) (cons hd (loop rst)) (loop rst))))) (loop L))
(define (limit4 L max) (filter4 L)) ;;* ;;........................
1D. limit4 returns the same value as limit. {2} ___T _X_F Describe ___F Punt
The variable max is unbound in filter4.
1E. limit5 returns the same value as limit. {2} _X_T ___F Describe ___F Punt Unless you noticed one too many right parens, in which case filter5 can’t work because it is not syntactically correct.
_X_F Describe ___F Punt
variables L and max are both unknown inside filter6; also an extra rpar. If you said something about values returned you didn’t get full credit.
1G. filter5 might take a much longer time using eval-1 than the other filters because it calls (car L) twice and L might be very long. (T/F F) {1} 1H. loop in filter2 runs an iterative process. (T/F F) {1}
1I. f1 and f2 below seem to return the same values for any L, a list of integers. Why? {3} (define (f1 L) (accumulate + 0 (filter odd? (map square L)))) (define (f2 L) (accumulate + 0 (map square (filter odd? L))))
SOLUTION. The squares of even numbers are even and the squares of odd numbers are odd. Therefore the result is the same if you filter out the odd ones before or after squaring: you get the same result.
(define (f a b c) (list a (list b) c))
It is designed to be called like this:
STk> (f 1 2 3) ;; a=1 b=2 c= (1 (2) 3)
We'd like to create a function g that is just like f except that each argument to g is preceded by a symbol which we shall call a keyword. This keyword makes explicit the parameter with which the actual argument is to be associated, eliminating the need to remember the exact order of the parameters:
STk> (g 'a 1 'b 2 'c 3) (1 (2) 3) STk> (g 'b 2 'a 1 'c 3) (1 (2) 3) STk> (g 'c 3 'b 2 'a 1) (1 (2) 3) The goal is to write a function make-keyword-proc that takes two arguments. The first is a regular Scheme procedure like f. The second is a list of the keywords in the order that the procedure expects. make-keyword-proc should return a function like g that takes keyword arguments and calls f with the arguments in the order f expects. Continuing the example above, to create the function g from f we can use make-keyword-proc like this:
STk> (define g (make-keyword-proc f '(a b c)))
The keywords do not have to match the names of the formal parameters of f :
STk> (define g2 (make-keyword-proc f '(cs61a cool is))) STk> (g2 'cs61a 1 'is 2 'cool 3) ;;note, keyword order irrelevant in call (1 (2) 3) Additionally, a call like STk> (g2 'is 2 'cs61a 1 'cool 3 'notcool 4) must work because notcool is not a keyword and doesn't affect the call to f. Your program should work so STk> (g2 'cs61a 1) will call f on arguments 1, #f, #f.
2A.First write a program, call it evenmember , with the following specification: ___Punt (20%) {3 points} ;; given an element x and a list L whose length is even, ;; return the sublist of L beginning with the first value eqv? to x, ;; checking only positions 0, 2, 4 .. Examples ;;(evenmember 'b '(a x b y c z)) is (b y c z) ;;(evenmember 'b '(a b b y c z)) is (b y c z) ;;(evenmember 'b '(a b c d) is ()
;;SOLUTION (define (evenmember x L) (if (pair? L) (if (eqv? (car L) x) L (evenmember x (cddr L))) '()))
4A. Define a function every-nth that takes two arguments integer k and integer n > 0, and returns a stream : the infinite stream of numbers beginning at the specified integer k and whose succeeding elements are k+n , k+ 2 n , k +3 n , ... For example, if k =0 and n =3, the stream will begin with 0, 3, 6, 9, 12, 15, 18. {2 pts}
SOLUTION (define (every-nth start n)(cons-stream start (every-nth (+ start n) n)))
4B. Define a stream r1 of all non-negative integer multiples of 3. {1, 1} [Why did some people not use every-nth for this??]
(define r1 (every-nth 0 3))
Define another stream r2 of all non-negative integer multiples of 5.
(define r2 (every-nth 0 5))
4C. Define a function both-streams that given two streams s1 and s2 each of (increasing) numbers, returns a new stream s3 of those numbers that appear in both s1 and s2. For example, (both-streams r1 r2) would begin with 0, 15, 30, ... {5 pts}
SOLUTION (define (both-streams s1 s2) (cond ((null-stream? s1) '()) ((null-stream? s2) '()) ((eqv? (stream-car s1)(stream-car s2)) (cons-stream (stream-car s1) (both-streams (stream-cdr s1)(stream-cdr s2)))) ((< (stream-car s1) (stream-car s2)) (both-streams (stream-cdr s1)s2)) (else (both-streams s1 (stream-cdr s2)))))))
4D. Are there any circumstances in which both-streams will not return? Explain, using examples as appropriate. {3} Yes, if the streams have no numbers in common, both-streams will not return. An example would be even and odd number generated by(every-nth 0 2) and (every-nth 1 2).
set! is a special form because (set! x y) must not evaluate its first argument. Set-car! is an ordinary function because it must evaluate x, which presumably evaluates to a pair, and it changes the car of that pair.
6A. Enhance the program in a minimal fashion so that he can only take money out if he remembers the correct PIN. What is a command to extract $10?
(HA 'withdraw) 100 PIN) {1}
6B. Enhance the program below so that he can change the PIN. What is a command to change the PIN to 1234?
(HA 'changePIN) PIN 1234) {1}
6C. Perhaps because of a design error, anyone who has (a pointer to) Henry’s account can find out the balance without knowing Henry’s PIN. (Hint: compare the deposit and withdraw programs). What command would you use in the given program to find out Henry’s balance?
((HA 'deposit) 0) {1}
6D. Change the program to protect Henry’s privacy, but add a feature so that Henry’s benefactors (perhaps his parents) can deposit money, without knowing his PIN, or finding out his balance. Write such a program, give-money so that ( give-money HA 100) where HA is Henry’s account, will deposit 100 dollars in his account. see below
6E. Finally, change the program so that it causes an error to deposit or withdraw a negative amount of money. see below
Check here if you have tried to do this problem previously either during a discussion section, review session, or with friends _____. {0, just curious}| .... (define (lookup-variable-value var env0 env1) (let ((match (or (assq var env0) (assq var env1)))) (if match (cdr match) (error "Undefined variable: " var))))
and by recalling this part of eval
((eq? kind 'symbol) (lookup-variable-value exp inner-env outer-env))
SOLUTION: There are a bunch of ways of doing this, including putting set! in the special forms list, and then doing (eq? kind 'set!) as a test in eval-1.
;;A kind of minimal change to eval-1, ignoring most abstractions etc would be …. ((eq? (car exp) 'set!)(set-cdr! ;{2} (lookup-variable-location (operand 1 exp) ;{2} env0 env1) (eval-1 (operand 2 exp) env0 env1)) ;;{2} ....
;;where (define (lookup-variable-location var env0 env1) ;;{1,1} ;; return (name. val) or bomb. (or (assq var env0) (assq var env1) (error "Undefined variable: " var))) ; don’t add it to env.
;;; many other (more elaborate) versions possible. failure to use mutation is a bad sign. (max 4 points). Many people used the punt option, making this easier to grade than expected.