2017-09-14 10 views
-2

私は再帰関数を実装しようとしている(和CERO)それは整数のリスト与えられ、それが0スキーム - 数字のSecuenceリストでその合計0

を合計連続した番号のすべての可能なsecuencesを印刷します実施例1:

(合計ゼロ「(4 2 -3 -1 0 4))

=>(-3 -1 0 4)

(0)

例2:

(合計ゼロ「(3 4 -7 3 1 3 1 -4 -2 -2))

=>(3 4 -7)

(4 -7 3)

(-7 3 1 3)

(3 1-4)

(3 1 3 1 -4 -2 -2)

(3 4 -7 3 1 3 1 -4 -2 -2)

答えて

0

次関数が1の引数を有するヘルパーを有する必要があります。利用可能な数字のリスト、2.現在の数字、3.これまでのゼロの合計の結果のリスト。ここで私はあなたの宿題をやってから私たちを維持するために取り残さいくつかの部分で、それを行っているだろうかです:

(define (sum-zero lst) 
    ;; insert e such that the resulting list i ssorted 
    ;; (insert 3 '(1 3 4)) ; ==> (1 3 3 4) 
    (define (insert e lst) 
    <implement>) 

    ;; main logic 
    (define (helper lst acc res) 
    (if (null? lst) 
     res 
     (let* ((new-acc (insert (car lst) acc)) 
       (res (if <should add new-acc to res> 
         (cons new-acc res) 
         res))) 
      ;; call the helper skipping the current element in the result 
      ;; and use that as the result on the secon call the includes it 
      (helper (cdr lst) 
        new-acc     
        (helper (cdr lst) acc res))))) 

    ;; notice() is already in the results 
    (helper lst '() '(()))) 

テスト、それは簡単です。しかし、私はそれが正しいと信じています。

(sum-zero '(3 4 -7 3 1 3 1 -4 -2 -2)) 
; ==> ((-7 -4 -2 -2 1 1 3 3 3 4) 
;  (-7 -4 -2 3 3 3 4) 
;  (-7 -2 -2 1 1 3 3 3) 
;  (-7 -4 1 1 3 3 3) 
;  (-7 -2 3 3 3) 
;  (-7 -2 -2 1 3 3 4) 
;  (-7 -4 1 3 3 4) 
;  (-7 -2 1 1 3 4) 
;  (-7 3 4) 
;  (-4 -2 1 1 4) 
;  (-4 -2 -2 1 3 4) 
;  (-2 -2 4) 
;  (-4 4) 
;  (-7 1 3 3) 
;  (-4 -2 -2 1 1 3 3) 
;  (-4 -2 3 3) 
;  (-2 1 1) 
;  (-2 -2 1 3) 
;  (-4 1 3) 
;  ()) 
関連する問題