It is common practice to use higher order functions instead of recursion, if there is a higher order function which expresses the right recursion pattern. In our case, sum-of-numbers
can be defined using foldl
:
#lang racket
(define (sum-of-numbers l)
(foldl + 0 l))
(sum-of-numbers '(1 2 3 4 5)) ;; => 15
It is possible to call foldl
directly on the list:
#lang racket
(foldl + 0 '(1 2 3 4 5)) ;; => 15