Here's one solution. We have a structural decomposition on move1, which calls one of two help functions. Each help function is a structural decomposition on move2.
;; STRATEGY: struct decomp on move1 (define (outcome move1 move2) (cond [(string=? move1 "betray") (outcome-after-betray move2)] [(string=? move1 "don't betray") (outcome-after-dont-betray move2)])) ;; outcome-after-betray : Move -> Number ;; GIVEN: player2's move ;; RETURNS: the outcome if player1 moved "betray" ;; STRATEGY: struct decomp on move2 (define (outcome-after-betray move2) (cond [(string=? move2 "betray") -3] [(string=? move2 "don't betray") 0])) ;; outcome-after-dont-betray : Move -> Number ;; GIVEN: player2's move ;; RETURNS: the outcome if player1 moved "don't betray" ;; STRATEGY: struct decomp on move2 (define (outcome-after-dont-betray move2) (cond [(string=? move2 "betray") -12] [(string=? move2 "don't betray") -1]))
Notice how the information we have about the situation in which we call the function is shown twice:
We'll see much more about expressing context information in later modules.
Last modified: Tue Aug 5 16:07:28 Eastern Daylight Time 2014