将变量与 Lisp 中的字符进行比较
Comparing a variable to a character in Lisp
我是 lisp 编程的新手,我正在尝试比较一个变量,比如说 op 和字符 +、-、*、/。但是我写的每一种方式都没有显示出正确的结果。我该如何重写以下代码:
(defun evaluate(op o1 o2)
(cond
( (string= op `+) (+ o1 o2))
( (string= op `-) (- o1 o2))
( (string= op `*) (* o1 o2))
( (string= op `/) (/ o1 o2))
)
)
输入是:
(print (calculate `(- * + 4 3 2 5)))
所以我在我的函数中得到了运算符字符。不要在意数字。
可以肯定的是这里是计算函数。
(defun calculate (l)
(if (and (numberp (car l)) (not (null l)) )
(car l)
( evaluate (car l) (calculate (cdr l) )
(calculate (cdr l) ) )
)
)
calculate
的写法,这个求值(- (* (+ 4 4) (+ 4 4)) (* (+ 4 4) (+ 4 4)))
,也就是0,不知道你期待什么,我们来看看tracing(SO>
是repl提示):
SO> (trace evaluate calculate)
(EVALUATE CALCULATE)
SO> (calculate '(- * + 4 3 2 5))
0: (SO::CALCULATE (- * + 4 3 2 5))
1: (SO::CALCULATE (* + 4 3 2 5))
2: (SO::CALCULATE (+ 4 3 2 5))
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::EVALUATE + 4 4)
3: EVALUATE returned 8
2: CALCULATE returned 8
2: (SO::CALCULATE (+ 4 3 2 5))
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::EVALUATE + 4 4)
3: EVALUATE returned 8
2: CALCULATE returned 8
2: (SO::EVALUATE * 8 8)
2: EVALUATE returned 64
1: CALCULATE returned 64
1: (SO::CALCULATE (* + 4 3 2 5))
2: (SO::CALCULATE (+ 4 3 2 5))
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::EVALUATE + 4 4)
3: EVALUATE returned 8
2: CALCULATE returned 8
2: (SO::CALCULATE (+ 4 3 2 5))
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::EVALUATE + 4 4)
3: EVALUATE returned 8
2: CALCULATE returned 8
2: (SO::EVALUATE * 8 8)
2: EVALUATE returned 64
1: CALCULATE returned 64
1: (SO::EVALUATE - 64 64)
1: EVALUATE returned 0
0: CALCULATE returned 0
0
最后一个0就是结果。在这些数字中,只有第一个被触及,我猜你也打算使用其他数字。
指出 calculate
中对 (calculate (cdr l))
的两次调用将始终产生相同的结果应该会有所帮助。
旁注:
- 使用标准格式(参见 http://www.gigamonkeys.com/book/syntax-and-semantics.html#formatting-lisp-code)
- 使用撇号
'
,而不是反引号。后者有不同的用途。
- 理解符号和字符串的区别
我是 lisp 编程的新手,我正在尝试比较一个变量,比如说 op 和字符 +、-、*、/。但是我写的每一种方式都没有显示出正确的结果。我该如何重写以下代码:
(defun evaluate(op o1 o2)
(cond
( (string= op `+) (+ o1 o2))
( (string= op `-) (- o1 o2))
( (string= op `*) (* o1 o2))
( (string= op `/) (/ o1 o2))
)
)
输入是:
(print (calculate `(- * + 4 3 2 5)))
所以我在我的函数中得到了运算符字符。不要在意数字。 可以肯定的是这里是计算函数。
(defun calculate (l)
(if (and (numberp (car l)) (not (null l)) )
(car l)
( evaluate (car l) (calculate (cdr l) )
(calculate (cdr l) ) )
)
)
calculate
的写法,这个求值(- (* (+ 4 4) (+ 4 4)) (* (+ 4 4) (+ 4 4)))
,也就是0,不知道你期待什么,我们来看看tracing(SO>
是repl提示):
SO> (trace evaluate calculate)
(EVALUATE CALCULATE)
SO> (calculate '(- * + 4 3 2 5))
0: (SO::CALCULATE (- * + 4 3 2 5))
1: (SO::CALCULATE (* + 4 3 2 5))
2: (SO::CALCULATE (+ 4 3 2 5))
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::EVALUATE + 4 4)
3: EVALUATE returned 8
2: CALCULATE returned 8
2: (SO::CALCULATE (+ 4 3 2 5))
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::EVALUATE + 4 4)
3: EVALUATE returned 8
2: CALCULATE returned 8
2: (SO::EVALUATE * 8 8)
2: EVALUATE returned 64
1: CALCULATE returned 64
1: (SO::CALCULATE (* + 4 3 2 5))
2: (SO::CALCULATE (+ 4 3 2 5))
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::EVALUATE + 4 4)
3: EVALUATE returned 8
2: CALCULATE returned 8
2: (SO::CALCULATE (+ 4 3 2 5))
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::CALCULATE (4 3 2 5))
3: CALCULATE returned 4
3: (SO::EVALUATE + 4 4)
3: EVALUATE returned 8
2: CALCULATE returned 8
2: (SO::EVALUATE * 8 8)
2: EVALUATE returned 64
1: CALCULATE returned 64
1: (SO::EVALUATE - 64 64)
1: EVALUATE returned 0
0: CALCULATE returned 0
0
最后一个0就是结果。在这些数字中,只有第一个被触及,我猜你也打算使用其他数字。
指出 calculate
中对 (calculate (cdr l))
的两次调用将始终产生相同的结果应该会有所帮助。
旁注:
- 使用标准格式(参见 http://www.gigamonkeys.com/book/syntax-and-semantics.html#formatting-lisp-code)
- 使用撇号
'
,而不是反引号。后者有不同的用途。 - 理解符号和字符串的区别