let 生成一个在 let 表单期间存在的局部变量。因此
(let ((new-list (append my-list '(5))))
(display new-list) ; prints the new list
) ; ends the let, new-list no longer exist
new-list ; error since new-list is not bound
您的错误消息告诉您,let 正文中没有表达式,这是必需的。因为我在我的代码中添加了一个,所以它是允许的。但是现在,当您尝试评估 new-list 时会出现错误,因为它在 let 表单之外不存在。
这是 Algol 的等价物(特别是在 JS 中)
const myList = [1,2,3,4];
{ // this makes a new scope
const newList = myList.concat([5]);
// at the end of the scope newList stops existing
}
newList;
// throws RefereceError since newList doesn't exist outside the block it was created.
由于您已经尝试过使用define,您可以改为这样做,它将在全局或函数范围内创建一个变量:
(define my-list '(1 2 3 4))
(define new-list (append my-list (list 5)))
new-list ; ==> (1 2 3 4 5)