1

I am trying to create a scheme recursive function deep_count that counts the sum of the numbers even if it's nested in sub lists.

(define deep_count
  (lambda (xs)
    (cond 
      ((empty? xs) 0)
      ((list? (first xs)) (+ 
                           (deep_count (first xs)) 
                           (deep_count (rest xs))))
      (else (+ 1 (deep_count (rest xs)))))))

(deep_count '(1 2 3 (4 5 6) ((7 8 9) 10 (11 (12 13)))))

But I am getting 13 instead of 91. Whats wrong here?

EDIT: never mind, I know why.

1 Answer 1

2

There's a small mistake at the end. Change this:

(+ 1 (deep_count (rest xs)))

... For this, and you're all set:

(+ (first xs) (deep_count (rest xs)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.