racket Recursion Using a named let


Example

A normal let form binds each value to its corresponding identifier, before executing the body. With a "named let", the body can then recursively be re-executed, passing a new value for each identifier.

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

It is common to use rec as the name for the let, which gives:

#lang racket
(let rec ([l '(1 2 3 4 5)])
  (if (null? l)
      0
      (+ (car l) (rec (cdr l)))))
;; => 15