方案:未绑定变量:rev

Scheme: Unbound variable: rev

我编写了以下 Scheme 代码:

(define (last-pair list1)
    (if (null? cdr list1)
        car list1
        (last-pair (cdr list1))))

(define (rev list1)
    (if (null? list1)
        list1
        (append (rev (cdr list1)) (list (car list1)))))

在名为 test.scm.

的文件中

在终端,我 运行:

加载"test.scm"

那我试试:

(last-pair (list 1 2 3))

;Value 15: (3)

这是正确的。

那我试试:

(rev (list 1 2 3))

;Unbound variable: rev
;To continue, call RESTART with an option number:
; (RESTART 4) => Specify a value to use instead of rev.
; (RESTART 3) => Define rev to a given value.
; (RESTART 2) => Return to read-eval-print level 2.
; (RESTART 1) => Return to read-eval-print level 1.

似乎它正在正确加载我的文件,因为它加载了 last-pair,但我不确定为什么我会收到上述错误...

谢谢!

你需要写

(load "test.scm")

在回复中。只是 load "test.scm" 可能不会加载文件。

我认为有一个内置的 last-pair 这让人感到困惑,你的 last-pair 示例有效而 rev 示例无效。