Files
psil/clitests/comb.test.psil
2024-01-04 12:53:24 +01:00

36 lines
703 B
Plaintext

(define (equal? a b)
(if (nil? a) (if (nil? b) 1 0)
(if (atom a)
(if (atom b) (= a b) 0)
(if (atom b) 0 (if (equal? (car a) (car b)) (equal? (cdr a) (cdr b)) 0))
)
)
)
(define (empty? l)
(equal? l (nil))
)
(define (comb-impl list cur n)
(if (empty? list)
(if (= cur n) 1 0)
(+
(comb-impl (cdr list) cur n)
(comb-impl (cdr list) (+ cur (car list)) n)
)
)
)
(define (comb list n)
(comb-impl list 0 n))
(comb (quote(1)) 1)
(comb (quote(1 1)) 1)
(comb (quote(1 1)) 2)
(comb (quote(1 1 1 2)) 3)
(comb (quote(1 2 1 2)) 3)
(comb (quote(1 3 4 2)) 3)
(comb (quote(1 2 3 4 5)) 15)
(comb (quote(1 2 3 4 5)) 10)
(comb (quote(1 2 3 4 5)) 5)