Functional programming SICP练习2.19-如何扩展?

Functional programming SICP练习2.19-如何扩展?,functional-programming,lisp,scheme,sicp,Functional Programming,Lisp,Scheme,Sicp,我只是通过SICP探索函数式编程,我想知道如何扩展练习2.19来做一些更有用的事情(这似乎需要副作用) 这个练习涉及到一个程序,该程序计算一个人可以用给定的硬币面额列表对给定金额(以便士为单位)进行兑换的方式的数量。解决方案非常简单(cc代表“计数变化”): 这就是我所遇到的问题:我想修改我的方法,这样就不需要返回一个整数结果=#进行更改的方式,而只需在计算过程中打印每种方式,我希望它返回一个解决方案列表(列表列表,其长度=#进行更改的方式)。然而,我不知道如何做到这一点。用命令式/OO语言很容

我只是通过SICP探索函数式编程,我想知道如何扩展练习2.19来做一些更有用的事情(这似乎需要副作用)

这个练习涉及到一个程序,该程序计算一个人可以用给定的硬币面额列表对给定金额(以便士为单位)进行兑换的方式的数量。解决方案非常简单(cc代表“计数变化”):

这就是我所遇到的问题:我想修改我的方法,这样就不需要返回一个整数结果=#进行更改的方式,而只需在计算过程中打印每种方式,我希望它返回一个解决方案列表(列表列表,其长度=#进行更改的方式)。然而,我不知道如何做到这一点。用命令式/OO语言很容易做到,但我不知道如何在功能上做到这一点

有人知道如何做到这一点吗?对于一个有经验的函数编码器来说,这似乎是一件非常容易的事情。。请帮助满足我的好奇心,并为自己网罗一些编码业力:)


谢谢

因为解决问题比阅读解决方案有趣得多:如果
cc
的结果不是做出改变的方法的数量,而是它们的列表呢?谢谢你的提示!你是对的,解决问题比昨晚睡前写的问题有趣得多,第二天早上我睁开眼睛,心里想着解决办法,希望没有人回答这个问题。。。谢谢你抽出时间。你的打字比我的更干净优雅。有一些业力:)
(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 (first-denomination coinTypes) (car coinTypes))
(define (except-first-denomination coinTypes) (cdr coinTypes))
(define (no-more? coinTypes) (null? coinTypes))
(define (count-change amount coinTypes)
    (define (cc amount coinTypes currChangeList)
        (cond ((= amount 0) 
                    (display (reverse currChangeList)) 
                    (newline) 
                    1)
              ((or (negative? amount) (null? coinTypes)) 
                    0)
              (else (+ (cc amount (cdr coinTypes) currChangeList)
                       (cc (- amount (car coinTypes)) coinTypes (cons (car coinTypes) currChangeList))))))
    (cc amount coinTypes ()))
(define (count-change amount coinTypes)
    (define (cc amount coinTypes currChangeList)
        (cond ((= amount 0) 
               (list (reverse currChangeList)))
              ((or (negative? amount) (null? coinTypes)) 
               '())
              (else
                (append
                   (cc amount (cdr coinTypes) currChangeList)
                   (cc (- amount (car coinTypes))
                       coinTypes
                       (cons (car coinTypes) currChangeList))))))
    (cc amount coinTypes '()))