V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
scalaer
V2EX  ›  程序员

求 v 友教下怎么 debug lisp 程序

  •  
  •   scalaer · 2019-05-25 20:51:23 +08:00 · 1215 次点击
    这是一个创建于 1805 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近在看 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.
    
    3 条回复    2019-05-25 21:39:40 +08:00
    xrlin
        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)))
    ))```
    xrlin
        2
    xrlin  
       2019-05-25 21:15:17 +08:00
    括号表达式内会进行求值,scheme 的 callable function 放在括号第一个参数,不像其它语言是括号在方法名后面。
    scalaer
        3
    scalaer  
    OP
       2019-05-25 21:39:40 +08:00
    @xrlin 多谢解答🙏
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1407 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 16:42 · PVG 00:42 · LAX 09:42 · JFK 12:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.