最近在看 sicp, 发现 lisp 挺有意思的.
刚刚试着写 leetcode 第二题的答案, 求 v 友帮忙看下那里有问题, 顺便教下怎么 debug
(define (extract p)
(if (null? p)
0
(car(p))))
(define (add x y) (
(+ (extract x)
(extract y)
)
))
(define (add2list l1 l2)
(if (and (null? l1) (null? l2))
()
(cons(
(add l1 l2)
(add2list (cdr l1) (cdr l2))))
))
用的mit-schema解释器
2 error> (add2list (list 1 2 3) (list 2 3 4))
;The object (4) is not applicable.
;To continue, call RESTART with an option number:
; (RESTART 3) => Specify a procedure to use in its place.
; (RESTART 2) => Return to read-eval-print level 2.
; (RESTART 1) => Return to read-eval-print level 1.
输入(debug)
3 error> (debug)
There are 26 subproblems on the stack.
Subproblem level: 0 (this is the lowest subproblem level)
Expression (from stack):
('(4))
There is no current environment.
The execution history for this subproblem contains 1 reduction.
You are now in the debugger. Type q to quit, ? for commands.
1
xrlin 2019-05-25 21:13:17 +08:00
```scheme
(define (extract p) (if (null? p) 0 (car p))) (define (add x y) (+ (extract x) (extract y) ) ) (define (add2list l1 l2) (if (and (null? l1) (null? l2)) () (cons (add l1 l2) (add2list (cdr l1) (cdr l2))) ))``` |
2
xrlin 2019-05-25 21:15:17 +08:00
括号表达式内会进行求值,scheme 的 callable function 放在括号第一个参数,不像其它语言是括号在方法名后面。
|