调用方案函数时返回#<undef>
Getting #<undef> back when calling a scheme function
我在Scheme中有如下代码:
(define (processExpression lst)
(define operand (car lst))
(define operator1 (cadr lst))
(define operator2 (caddr lst))
(if (list? operand)
(begin
(display "Invalid syntax! Erroring out!")
(quit)
)
)
(if (and (number? operator1) (number? operator2))
;The list was of the form (operand c1 c2)
(simplePrefix lst)
)
(if (and (list? operator1) (number? operator2))
(begin
(list operand operator2 (processExpression operator1))
)
)
)
(define (simplePrefix lst)
(let (
(operand (car lst))
(operator1 (cadr lst))
(operator2 (caddr lst)))
(list operator1 operand operator2)
)
)
(display "Please enter a valid s-expression")
(let ((input (read)))
(display (simplePrefix input))
)
此代码采用 s-expression 并根据某些规则对其进行转换。一条规则应该是表达式 (+ (+ 1 2) 2) 应该 return (+ 2 (1 + 2)).
目前,当我用那个表达式调用这段代码时,我得到了结果“#undef>”,但我不知道为什么。代码应该在表达式的 (+ 1 2) 部分和 return (1 + 2) 上调用 simplePrefix 函数,但它没有。有人明白为什么吗?
价值
(if condition
something)
如果 condition 为假,则 未定义,因为 if 中没有 else 部分形式。在 Scheme 的 R5RS 版本中,这样声明(强调):
4.1.5 Conditionals
syntax: (if <test> <consequent> <alternate>)
syntax: (if <test> <consequent>)
Syntax: <Test>
, <consequent>
, and <alternate>
may be arbitrary expressions.
Semantics: An if expression is evaluated as follows: first, <test> is evaluated. If it yields a true value (see section
6.3.1), then <consequent> is evaluated and its value(s) is(are) returned. Otherwise <alternate> is evaluated and its value(s)
is(are) returned. If <test> yields a false value and no
<alternate> is specified, then the result of the expression is
unspecified.
(if (> 3 2) 'yes 'no) ===> yes
(if (> 2 3) 'yes 'no) ===> no
(if (> 3 2)
(- 3 2)
(+ 3 2)) ===> 1
一些类似 Scheme 的语言(例如 Racket)实际上会在您尝试这样做时警告您:
Welcome to Racket v6.1.
> (if #t
(display "hello"))
stdin::1: if: missing an "else" expression
in: (if #t (display "hello"))
context...:
在Common Lisp中,else部分是可选的,如果没有提供则定义为nil:
This is SBCL 1.2.4.58-96f645d, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.
* (if t
(print 'hello))
HELLO
HELLO
* (if nil
(print 'hello))
NIL
表达式
(if condition result)
不等同于
if condition
return result
其他语言。
它没有 return 一个值,它有一个值。
如果条件为假,则该值未定义。
如果它后面跟着序列中的其他表达式,则忽略该值并计算后面的表达式。
函数应用的计算结果是函数体中最后一个表达式的值
(define (foo)
(if (= 1 1)
"one is one")
"I am still here")
> (foo)
"I am still here"
这两个加在一起导致了您遇到的错误。
我在Scheme中有如下代码:
(define (processExpression lst)
(define operand (car lst))
(define operator1 (cadr lst))
(define operator2 (caddr lst))
(if (list? operand)
(begin
(display "Invalid syntax! Erroring out!")
(quit)
)
)
(if (and (number? operator1) (number? operator2))
;The list was of the form (operand c1 c2)
(simplePrefix lst)
)
(if (and (list? operator1) (number? operator2))
(begin
(list operand operator2 (processExpression operator1))
)
)
)
(define (simplePrefix lst)
(let (
(operand (car lst))
(operator1 (cadr lst))
(operator2 (caddr lst)))
(list operator1 operand operator2)
)
)
(display "Please enter a valid s-expression")
(let ((input (read)))
(display (simplePrefix input))
)
此代码采用 s-expression 并根据某些规则对其进行转换。一条规则应该是表达式 (+ (+ 1 2) 2) 应该 return (+ 2 (1 + 2)).
目前,当我用那个表达式调用这段代码时,我得到了结果“#undef>”,但我不知道为什么。代码应该在表达式的 (+ 1 2) 部分和 return (1 + 2) 上调用 simplePrefix 函数,但它没有。有人明白为什么吗?
价值
(if condition
something)
如果 condition 为假,则 未定义,因为 if 中没有 else 部分形式。在 Scheme 的 R5RS 版本中,这样声明(强调):
4.1.5 Conditionals
syntax:
(if <test> <consequent> <alternate>)
syntax:(if <test> <consequent>)
Syntax:<Test>
,<consequent>
, and<alternate>
may be arbitrary expressions.Semantics: An if expression is evaluated as follows: first, <test> is evaluated. If it yields a true value (see section 6.3.1), then <consequent> is evaluated and its value(s) is(are) returned. Otherwise <alternate> is evaluated and its value(s) is(are) returned. If <test> yields a false value and no <alternate> is specified, then the result of the expression is unspecified.
(if (> 3 2) 'yes 'no) ===> yes (if (> 2 3) 'yes 'no) ===> no (if (> 3 2) (- 3 2) (+ 3 2)) ===> 1
一些类似 Scheme 的语言(例如 Racket)实际上会在您尝试这样做时警告您:
Welcome to Racket v6.1.
> (if #t
(display "hello"))
stdin::1: if: missing an "else" expression
in: (if #t (display "hello"))
context...:
在Common Lisp中,else部分是可选的,如果没有提供则定义为nil:
This is SBCL 1.2.4.58-96f645d, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.
* (if t
(print 'hello))
HELLO
HELLO
* (if nil
(print 'hello))
NIL
表达式
(if condition result)
不等同于
if condition
return result
其他语言。
它没有 return 一个值,它有一个值。
如果条件为假,则该值未定义。
如果它后面跟着序列中的其他表达式,则忽略该值并计算后面的表达式。
函数应用的计算结果是函数体中最后一个表达式的值
(define (foo)
(if (= 1 1)
"one is one")
"I am still here")
> (foo)
"I am still here"
这两个加在一起导致了您遇到的错误。