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?
Here’s an example of how you would choose a value at random, between two possible values:
The procedure returns the randomly chosen value, it’s up to you how to use it. And a
lambdacan be passed as an argument as any other value, there isn’t anything different about it. For example: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
1is returned, and sometimes5is returned.