recursion - Counting Change in Scheme using a list of denominations -


i writing function count number of ways make change in scheme given list of denominations , amount. code follows, not work intended. should using cons instead of + operator? should third line down's base case empty list?

(define (change k l)  (cond ((= k 0) 1)      ((or (< k 0) (null? l)) 0)      (else (+ (change k (cdr l))               (change (- k (car l))                       (cdr l))))))  

test:

(change 11 (list 1 5 10 25)) 

if returned value number forget cons , '() building output, , use car, cdr, null? processing input. other that, aware there's small mistake in last line of code, here's fixed version:

(define (change k l)   (cond ((= k 0) 1)         ((or (< k 0) (null? l)) 0)         (else          (+ (change k (cdr l))             (change (- k (car l)) l))))) ; don't (cdr l) here 

now works expected:

(change 11 (list 1 5 10 25)) => 4 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -