0% found this document useful (0 votes)
24 views4 pages

Discussion 2 Notes

This document discusses procedures as data and procedures as return values. It provides examples of procedures being passed as arguments to other procedures and procedures being returned by other procedures. It also presents a series of questions that define procedures to solve problems involving hiding, seeking, and transforming elements in sentences.

Uploaded by

CindyLi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Discussion 2 Notes

This document discusses procedures as data and procedures as return values. It provides examples of procedures being passed as arguments to other procedures and procedures being returned by other procedures. It also presents a series of questions that define procedures to solve problems involving hiding, seeking, and transforming elements in sentences.

Uploaded by

CindyLi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1

CS61AS Notes 02 Procedure As Data


QUESTIONS: What do the following evaluate to?

(lambda (x) (* x 2))


((lambda (y) (* (+ y 2) 8)) 10)


((lambda (b) (* 10 ((lambda (c) (* c b)) b))) ((lambda (e) (+ e 5)) 5))


((lambda (n) (+ n 10))
((lambda (m) (m ((lambda(p) (* p 5)) 7))) (lambda (q) (+ q q))))

Now well be using procedures as arguments to other procedures!

QUESTIONS

1. What does this guy evaluate to?
((lambda(x) (x x)) (lambda(y) 4))



2. What about his new best friend?
((lambda(y z) (z y)) * (lambda(a) (a 3 5)))



3. Write a procedure, foo, that, given the call below, will evaluate to 10.
((foo foo foo) foo 10)




4. Write a procedure, bar, that, given the call below, will evaluate to 10 as well.
(bar (bar (bar 10 bar) bar) bar)




5. Okay, now, a really easy one: What does the following evaluate to?
((lambda(f x) (f f x))
(lambda(k n)
(if (< n 2)
1
(* n (k k (- n 1)))))
4)



6. Something easy: write first-satisfies that takes in a predicate procedure and a sentence, and
returns the first element that satisfies the predicate test. Return #f if none satisfies the predicate test.
For example, (first-satisfies even? (1 2 3 4 5)) returns 2, and (first-
satisfies number? (a clockwork orange)) returns #f.

2
(define (first-satisfies pred? sent)




Now for procedures as return values

QUESTIONS

1. In lecture, you were introduced to the procedure keep, which takes in a predicate procedure and a
sentence, and throws away all words of the sentence that doesnt satisfy the predicate. The code for
keep was:

(define (keep pred? sent)
(cond ((empty? sent) ())
((pred? (first sent))
(sentence (first sent) (keep pred? (bf sent))))
(else (keep pred? (bf sent)))))

Recall that Brian said to keep numbers less than 6, this wouldnt work:
(keep (< 6) (4 5 6 7 8))

a. Why doesnt the above work?



b. Of course, this being Berkeley, and us being rebels, were going to promptly prove the authority
figure the Professor himself wrong. And just like some rebels, well do so by cheating. Lets
do a simpler version; suppose wed like this to do what we intended:
(keep (lessthan 6) (4 5 6 7 8))

Define procedure lessthan to make this legal.



2. Write a procedure exponents function, (f-expt func power) that returns a procedure which is
equivalent to func applied power times. Assume func takes in only a single argument. For
example, ((f-expt square 3) 2) ==> 256, because (square (square (square 2)))
is 256.





3. Were going to play hide-and-go-seek. Lets say, a seeker is a procedure that takes in a sentence,
and seeks out a certain word in the sentence. It returns the word if the word is found, or #f
otherwise. For example, if we have a 4-seeker, a seeker that seeks out the number 4, then
(4-seeker (1 2 3 4 5)) ==> 4
(4-seeker (1 2 3)) ==> #f
A seeker-producer is a procedure that takes in an element x and returns a procedure (a
seeker) that takes in a sentence sent and returns x if the element x is in the sentence sent, and
#f otherwise.

a. Make a call to seeker-producer to find out if 4 is in the list (9 3 5 4 1 0). seeker-
producer is the only procedure you can use! What does it return?
3



b. Implement seeker-producer, using the handy-dandy procedure member?.

(define (seeker-producer x)




c. Implement seeker-producer, using an internal define, but not using member?.

(define (seeker-producer x)
(define (helper






d. Implement seeker-producer, not using internal defines or member?.

(define (seeker-producer x)





e. Of course, its not much of a game if we cant hide! A hider of a word is a procedure that takes
in a sentence and hides the word behind an asterisk if it exists. For example, if we have a 4-
hider, a hider that hides the number 4, then
(4-hider (1 2 3 4 5)) ==> (1 2 3 *4 5)

Write a procedure hider-producer that takes in an element y, and returns a procedure (a
hider) that takes in a sentence sent and returns the same sentence with element y hidden
behind an asterisk, if it exists.

Youll probably want to use every to help you.

(define (hider-producer x)




f. Oh no! Now a hider can fool your seeker! Consider this call:
(4-seeker (4-hider (1 2 3 4 5)))
==> #f (make sure you know why!)

Surely you will not be outdone by yourself. Write a procedure, super-seeker-producer
that takes in a procedure produced by seeker-producer (that is, a seeker), and returns a
super-seeker that will not be fooled by hider:
((super-seeker-producer 4-seeker) (4-hider (1 2 3 4 5))) ==> 4

You can use every if you want. You might also find these procedures useful:
(define (hidden? w) (equal? (first w) *))
4
(define (unhide w)
(if (hidden? w) (bf w) w))

(define (super-seeker-producer seeker)






g. Write super-hider-producer that takes in a hider and returns a super-hider so that it still
manages to hide the element from a super-seeker.
((super-hider-producer 4-hider) (3 4 5)) ==> (3 **4 5)
((super-seeker-producer 4-seeker)
((super-hider-producer 4-hider) (1 2 3 4 5))) ==> #f

The strategy will be to hide the element we want to hide TWICE. You can use every or
hidden? or first-satisfies or whatever else weve already defined.

(define (super-hider-producer hider)







h. Sure, we can write a super-super-seeker-producer, and follow that up with a super-
super-hider-producer, but this is getting annoying and this problem has long outstayed its
welcome. So instead, lets write the ultimate-hider-producer which takes in a SEEKER
(not a hider!), and returns an ultimate-hider that hides what the seeker seeks by REMOVING it
from the sentence. So,
((ultimate-hider-producer 4-seeker) (1 2 3 4 5)) ==> (1 2 3 5)

You can use every or hidden? or whatever weve already defined.

(define (ultimate-hider-producer seeker)







The moral? Hiders always win. Copping out is the best strategy.

You might also like