racket Recursion Using let-rec


Example

#lang racket
(letrec ([sum-of-list (λ (l)
                        (if (null? l)
                            0
                            (+ (car l) (sum-of-list (cdr l)))))])
  (sum-of-list '(1 2 3 4 5)))
;; => 15

It is possible to write mutually recursive functions with letrec:

#lang racket
(letrec ([even? (λ (n) (if (= n 0) #t (odd?  (sub1 n))))]
         [odd?  (λ (n) (if (= n 0) #f (even? (sub1 n))))])
  (list (even? 3)
        (odd? 5)))
;; => '(#f #t)