求 v 友教下怎么 debug lisp 程序

2019-05-25 20:51:23 +08:00
 scalaer

最近在看 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.
1272 次点击
所在节点    程序员
3 条回复
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)))
))```
xrlin
2019-05-25 21:15:17 +08:00
括号表达式内会进行求值,scheme 的 callable function 放在括号第一个参数,不像其它语言是括号在方法名后面。
scalaer
2019-05-25 21:39:40 +08:00
@xrlin 多谢解答🙏

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/567657

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX