Permalink
Browse files

Add missing solutions to 2.2.1

  • Loading branch information...
1 parent ec74630 commit 75d0fe3eb99fd5042b9f8f086f3fc163fa301343 @fastred committed Dec 29, 2012
Showing with 60 additions and 0 deletions.
  1. +22 −0 2_19.scm
  2. +4 −0 2_20.scm
  3. +34 −0 2_22.scm
View
@@ -0,0 +1,22 @@
+(define (cc amount coin-values)
+ (cond ((= amount 0) 1)
+ ((or (< amount 0) (no-more? coin-values)) 0)
+ (else
+ (+ (cc amount
+ (except-first-denomination coin-values))
+ (cc (- amount
+ (first-denomination coin-values))
+ coin-values)))))
+
+(define no-more? null? )
+(define except-first-denomination cdr)
+(define first-denomination car)
+(define us-coins (list 50 25 10 5 1))
+(define uk-coins (list 100 50 20 10 5 2 1 0.5))
+
+;(cc 100 us-coins)
+;(cc 50 uk-coins)
+;
+; Reversing the order of coins doesn't affect a value returned by cc, but it
+; changes the structure of a recursion tree.
+
View
@@ -0,0 +1,4 @@
+(define (same-parity a . b)
+ (let ((comparator (if (even? a) even? odd?)))
+ (append (list a) (filter comparator b))))
+
View
@@ -0,0 +1,34 @@
+(define (square-list items)
+ (define (iter things answer)
+ (if (null? things)
+ answer
+ (iter (cdr things)
+ (cons (square (car things))
+ answer))))
+ (iter items '()))
+
+(square-list (list 1 2 3 4))
+; Answer list is reversed because newest element is a first argument to cons,
+; so the list is build this way:
+; (1)
+; (4 1)
+; (9 4 1)
+; (16 9 4 1)
+
+(define (square-list items)
+ (define (iter things answer)
+ (if (null? things)
+ answer
+ (iter (cdr things)
+ (cons answer
+ (square (car things))))))
+ (iter items '()))
+
+(square-list (list 1 2 3 4))
+;This procedure also doesn't work because answer is build as showed below:
+;(() 1)
+;((() 1) 4)
+;(((() 1) 4) 9)
+;((((() 1) 4) 9) 16)
+
+

0 comments on commit 75d0fe3

Please sign in to comment.