1

I am trying to use tail recursion but I am stuck. Please help

(define (sum-divis-five lst)
    (helper-function lst 0))

(define (helper-function lst answer)
    (cond[(empty? lst) answer]
         [else (if (= 0 (remainder (car lst) 5))
                   ????)
                   (helper-function (cdr lst) answer)]))
1
  • Start with writing a function that tail-recursively adds all numbers in a list and the answer will appear to you.
    – molbdnilo
    Jul 16, 2018 at 18:05

2 Answers 2

1

Filling in

(define (sum-divis-five lst)
    (helper-function lst 0))

(define (helper-function lst answer)
    (cond ((empty? lst) answer)
          ((= 0 (remainder (car lst) 5)) (helper-function (cdr lst) (+ (car lst) answer)))
          (else (helper-function (cdr lst) answer))))

With letrec

One can do it using letrec inside the function, too

(define (sum-divis-five L)
  (letrec ((i-sum-divis-five (lambda (L acc)
                               (cond ((empty? L) acc)
                                     ((= 0 (remainder (car L) 5))
                                      (i-sum-divis-five (cdr L)
                                                        (+ (car L) acc)))
                                     (else (i-sum-divis-five (cdr L) acc))))))
    (i-sum-divis-five L 0)))

More general

Or generalized:

(define (sum-divisibles L n)
  (letrec ((i-sum-divs (lambda (L n acc)
                         (cond ((empty? L) acc)
                               ((= 0 (remainder (car L) n))
                                (i-sum-divs (cdr L) n (+ (car L) acc)))
                               (else (i-sum-divs (cdr L) n acc))))))
    (i-sum-divs L n 0)))

With:

(sum-divisibles '(1 2 3 4 5 10 15 3) 5)
;; 30

With special case 5

(define (sum-divis-five L)
  (sum-divisibles L 5))

(sum-divis-five '(1 2 3 4 5 10 15 3))
;; 30
0
0

If you name your variables, you will get a much better overview of what you are dealing with. Try also to divide the problem into smaller problems.

Approach You want to filter out all the numbers that are not divisible by 5, then sum all the numbers in the filtered list.

Example

Input: 1 2 3 4 5 6 7 10 12 15 3 5

Filter: 5 10 15 5

Sum: 35

Code (auxiliary)

(define (is-divisible-by? dividend divisor)
  (zero? (remainder dividend divisor)))

(define (is-divisible-by-5? dividend)
  (is-divisible-by? 5))

Code (non-tail-recursive)

(define (filter predicate list)
  (if (empty? list)
      '()
      (let ([element (car list)]
            [rest (cdr list)])
        (if (predicate element)
            (cons element (filter predicate rest))
            (filter predicate rest)))))

(define (sum list)
  (if (empty? list)
      0
      (let ([element (car list)]
            [rest (cdr list)])
        (+ element (sum rest)))))

Code (tail-recursive)

(define (filter predicate list result)
  (if (empty? list)
      result
      (let ([element (car list)]
            [rest (cdr list)])
        (if (predicate element)
            (filter predicate rest (cons element result))
            (filter predicate rest result)))))

(define (sum list result)
  (if (empty? list)
      result
      (let ([element (car list)]
            [rest (cdr list)])
        (sum rest (+ element result)))))

Calling

(define (solution list)
  (sum (filter is-divisible-by-5? list '()) 0))

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.