; ======================================================================
;
; Structure and Interpretation of Computer Programs
; (trial answer to excercises)
;
; 计算机程序的构造和解释(习题试解)
;
; created: code17 08/19/05
; modified:
; (保持内容完整不变前提下,可以任意转载)
; ======================================================================
;; SICP No.2.41
(define (sum-s-triple n s)
(define (sum-s? x) (= (accumulate + 0 x) s))
(filter sum-s?
(flatmap
(lambda (i) (flatmap
(lambda (j) (map
(lambda (k) (list i j k))
(enumerate-interval 1 (- j 1))))
(enumerate-interval 1 (- i 1))))
(enumerate-interval 1 n))))
;; Test-it:
;; Welcome to MzScheme version 209, Copyright (c) 2004 PLT Scheme, Inc.
;; > (sum-s-triple 7 10)
;; ((5 3 2) (5 4 1) (6 3 1) (7 2 1))
;; > (sum-s-triple 7 12)
;; ((5 4 3) (6 4 2) (6 5 1) (7 3 2) (7 4 1))