Reputation: 588
The following procedure random-choice contains a procedure called choice:
(define (random-choice strategy-1 strategy-2)
(choice strategy-1
strategy-2
(lambda (ship-state) (= (random 2) 0))))
How do I create the procedure choice that chooses the two strategies at random? Also can you explain how lambda can be passed as an argument from procedure to procedure?
Upvotes: 1
Views: 210
Reputation: 236150
Here's an example of how you would choose a value at random, between two possible values:
(define (choice s1 s2)
(if (zero? (random 2)) s1 s2))
The procedure returns the randomly chosen value, it's up to you how to use it. And a lambda
can be passed as an argument as any other value, there isn't anything different about it. For example:
((choice (lambda (x) (first x))
(lambda (x) (last x)))
'(1 2 3 4 5))
The above will select randomly between a strategy for picking either the first or the last element in a list, and then it applies the selection to a list. Run it several times, you'll see that sometimes 1
is returned, and sometimes 5
is returned.
Upvotes: 1