Recursive sum function in scheme -
i want add consecutive values in stream , return list. like, stream : (1,2,3,4) output: (3,5,7) have code here giving me error car: contract violation expected: pair? given: '()
i have tried using head , tail separately , working fine! whats wrong here ?
(define (sum-primes prime-stream) (if (empty-stream? prime-stream) '() (cons (+ (head prime-stream) (head (tail prime-stream))) (sum-primes (tail prime-stream)))))
(+ (head prime-stream) (head (tail prime-stream))
the tail might '() , in case, (head (tail)) amount (head'())
you need replace nil condition this, cheking tail of tail instead , returning final sum consed '()
something like
(if (empty-stream? (cdr (cdr p))) (+(car p) (car (cdr p))) ...
Comments
Post a Comment