【发布时间】:2016-05-21 03:44:31
【问题描述】:
我知道我无法在表达式inside 中定义一个函数,但是在同一嵌套函数中定义一个表达式after 时出现错误。我将 R5RS 与 DrScheme 一起使用。
例如,这会抛出一个错误,'define: not allowed in an expression context':
(define (cube x)
(if (= x 0) 0) ;just for example, no reason to do this
(define (square x) (* x x))
(* x (square x)))
【问题讨论】:
-
实际上,你可以在表达式中定义一个函数,只是不能使用
define语法,它只能在某些上下文中使用,而是使用let,例如:(+ 2 (let ((square (lambda(x) (* x x)))) (square 4))) -
这个问题的重点是它 not 在表达式中。使用 lambda 语法会出现相同的错误。有几个很好的答案可以解释这里发生了什么。
标签: scheme