diff options
| author | rsiddharth <s@ricketyspace.net> | 2016-11-26 02:00:46 +0000 | 
|---|---|---|
| committer | rsiddharth <s@ricketyspace.net> | 2016-11-26 02:00:46 +0000 | 
| commit | 05406f3d496743112af75ced3fa2e566121f844b (patch) | |
| tree | 49e08f1a938c5caf76068c1c23ab240f9fa5e7ae /one | |
| parent | 5bf0b73c2aff8613c3db3188356d4735ca6506a3 (diff) | |
update one/fourteen.scm
Diffstat (limited to 'one')
| -rw-r--r-- | one/fourteen.scm | 54 | 
1 files changed, 34 insertions, 20 deletions
| diff --git a/one/fourteen.scm b/one/fourteen.scm index 47156c7..4186238 100644 --- a/one/fourteen.scm +++ b/one/fourteen.scm @@ -2,28 +2,42 @@  ;;;; International. See  ;;;; <https://creativecommons.org/licenses/by-sa/4.0/>. -;;; source from section 1.2.2 +;;; adapted from section 1.2.2 +;;; intolerable scheme drivel; ignore it.  (define-module (one fourteen) -  #:export (count-change -            cc -            first-denomination)) +  #:export (get-cc +            cc-range +            cc-diff-list)) -(define (count-change amount) -  (cc amount 5)) +(define (get-cc) +  (let ((no-of-steps 0)) +    (define (cc amount kinds-of-coins) +      (set! no-of-steps (1+ no-of-steps)) +      (cond ((= amount 0) 1) +            ((or (< amount 0) (= kinds-of-coins 0)) 0) +            (else (+ (cc amount +                         (- kinds-of-coins 1)) +                     (cc (- amount +                            (first-denomination kinds-of-coins)) +                         kinds-of-coins))))) +    (define (first-denomination kinds-of-coins) +      (cond ((= kinds-of-coins 1) 1) +            ((= kinds-of-coins 2) 5) +            ((= kinds-of-coins 3) 10) +            ((= kinds-of-coins 4) 25) +            ((= kinds-of-coins 5) 50))) +    (lambda (amount) +      (cc amount 5) +      no-of-steps))) -(define (cc amount kinds-of-coins) -  (cond ((= amount 0) 1) -        ((or (< amount 0) (= kinds-of-coins 0)) 0) -        (else (+ (cc amount -                     (- kinds-of-coins 1)) -                 (cc (- amount -                        (first-denomination kinds-of-coins)) -                     kinds-of-coins))))) +(define (cc-range amount limit incr) +  (cond ((> amount limit) '()) +        ((<= amount limit) +         (cons ((get-cc) amount) +              (cc-range (+ amount incr) limit incr))))) -(define (first-denomination kinds-of-coins) -  (cond ((= kinds-of-coins 1) 1) -        ((= kinds-of-coins 2) 5) -        ((= kinds-of-coins 3) 10) -        ((= kinds-of-coins 4) 25) -        ((= kinds-of-coins 5) 50))) +(define (cc-diff-list l) +  (cond ((= (length l) 1) '()) +        ((> (length l) 1) (cons (+ (* -1 (car l)) (cadr l)) +                                (cc-diff-list (cdr l)))))) | 
