From 562a9a52d599d9a05f871404050968a5fd282640 Mon Sep 17 00:00:00 2001 From: elioat Date: Wed, 23 Aug 2023 07:52:19 -0400 Subject: * --- .../~bh/61a-pages/Lectures/2.1/total.scm | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 js/games/nluqo.github.io/~bh/61a-pages/Lectures/2.1/total.scm (limited to 'js/games/nluqo.github.io/~bh/61a-pages/Lectures/2.1/total.scm') diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lectures/2.1/total.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lectures/2.1/total.scm new file mode 100644 index 0000000..2cf3505 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lectures/2.1/total.scm @@ -0,0 +1,60 @@ +; first version -- no data abstraction + +(define (total-hand hand) + (if (empty? hand) + 0 + (+ (butlast (last hand)) + (total-hand (butlast hand)) ))) + + +(total-hand '(3h 10c 4d)) + + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +; second version -- add selectors + +(define (total-hand hand) + (if (empty? hand) + 0 + (+ (card-rank (one-card hand)) + (total-hand (remaining-cards hand)) ))) + +(define card-rank butlast) +(define card-suit last) + +(define one-card last) +(define remaining-cards butlast) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +; third version -- add constructors + +(define (make-card rank suit) + (word rank (first suit)) ) + +(define make-hand se) + + +(total-hand (make-hand (make-card 3 'heart) + (make-card 10 'club) + (make-card 4 'diamond) )) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +; fourth version -- change implementation + +(define (make-card rank suit) + (cond ((equal? suit 'heart) rank) + ((equal? suit 'spade) (+ rank 13)) + ((equal? suit 'diamond) (+ rank 26)) + ((equal? suit 'club) (+ rank 39)) + (else (error "say what?")) )) + +(define (card-rank card) + (remainder card 13)) + +(define (card-suit card) + (nth (quotient card 13) '(heart spade diamond club))) + -- cgit 1.4.1-2-gfad0