使用 rec

#lang racket
(require mzlib/etc)
((rec sum-of-list
   (λ (l)
     (if (null? l)
         0
         (+ (car l) (sum-of-list (cdr l))))))
 '(1 2 3 4 5))
;; => 15

;; Outside of the rec form, sum-of-list gives an error:
;; sum-of-list: undefined;
;;  cannot reference an identifier before its definition

这类似于 define,但 sum-of-list 标识符在 rec 表单之外是不可见的。

为避免使用明确的λ,可以用 (sum-of-list args ...) 替换 sum-of-list

#lang racket
(require mzlib/etc)
((rec (sum-of-list l)
   (if (null? l)
       0
       (+ (car l) (sum-of-list (cdr l)))))
 '(1 2 3 4 5))
;; => 15