汽车:6不是列表错误

时间:2017-11-19 16:50:51

标签: common-lisp clisp

我正在尝试在CLisp中编写一个程序,它完美地改变了两个列表。以下是我的计划。当我将输入作为(shuffle '(1 2 3) '(4 5 6))提供给程序时。它给出了错误car:6 is not a list

(defun len (list)
  (if list
    (1+ (len (cdr list)))
    0))

(defun my-append (A1 A2)
    (cond
        ((null A1) A2)
        (t (cons A1 A2)))
    )

(defun append (A1 A2)
    (cond
        ((null A2) A1)
        (t (cons (car A1) (append (cdr A1) A2))))
)

(defun shuffle (A1 A2)
    (cond
        ((/= (len A2) (len A1)) (princ "lengths of the lists are not equal"))
        ((and (null A1) (null A2)) (princ "lists shuffled"))
        (t (append
            (my-append (car A1) (car A2)) 
            (shuffle (cdr A1) (cdr A2))
           )
        )
    )
)
有人能帮助我吗?

2 个答案:

答案 0 :(得分:4)

让我们手动执行(shuffle '(3) '(6))

  1. (shuffle '(3) '(6)) -
  2. (append (my-append 3 6) (shuffle '() '()) -
  3. myappend返回(cons 3 6),因此我们有(append (cons 3 6) nil) -
  4. (cons 3 (append 6 nil)) -
  5. (cons 3 (cons (car 6) nil)) - (car 6)无效,因为事实上6不是列表。

答案 1 :(得分:2)

stackoverflow社区可能提供的这个特定问题的任何特定答案只会帮助您解决这个问题,但不能帮助您解决下一个问题。

您应该问的真正问题是:如何使用Common Lisp调试器?

由于您使用的是clisp,请熟悉clisp的调试器。

  1. 尝试?获取命令列表。
  2. 使用:bt获取堆栈跟踪。
  3. 在这种情况下:

    Break 1 [6]> :bt
    <1/289> #<SYSTEM-FUNCTION SHOW-STACK> 3
    <2/282> #<COMPILED-FUNCTION SYSTEM::PRINT-BACKTRACE>
    <3/276> #<COMPILED-FUNCTION SYSTEM::DEBUG-BACKTRACE>
    <4/267> #<SYSTEM-FUNCTION SYSTEM::READ-EVAL-PRINT> 2
    <5/264> #<COMPILED-FUNCTION SYSTEM::BREAK-LOOP-2-3>
    <6/260> #<SYSTEM-FUNCTION SYSTEM::SAME-ENV-AS> 2
    <7/246> #<COMPILED-FUNCTION SYSTEM::BREAK-LOOP-2>
    <8/244> #<SYSTEM-FUNCTION SYSTEM::DRIVER>
    <9/204> #<COMPILED-FUNCTION SYSTEM::BREAK-LOOP>
    <10/201> #<SYSTEM-FUNCTION INVOKE-DEBUGGER> 1
    <11/191> #<SYSTEM-FUNCTION CAR>
    [190] EVAL frame for form (CAR A1)
    [186] EVAL frame for form (CONS (CAR A1) (APPEND (CDR A1) A2))
    <12/183> #<SPECIAL-OPERATOR COND>
    [182] EVAL frame for form (COND ((NULL A2) A1) (T (CONS (CAR A1) (APPEND (CDR A1) A2))))
    [165] APPLY frame for call (APPEND '6 '"lists shuffled")
    <13/160> 
    #<FUNCTION APPEND (A1 A2) (DECLARE (SYSTEM::IN-DEFUN APPEND))
      (BLOCK APPEND
       (COND ((NULL A2) A1) (T (CONS (CAR A1) (APPEND (CDR A1) A2)))))> 2
    [159] EVAL frame for form (APPEND (CDR A1) A2)
    [154] EVAL frame for form (CONS (CAR A1) (APPEND (CDR A1) A2))
    <14/151> #<SPECIAL-OPERATOR COND>
    [150] EVAL frame for form (COND ((NULL A2) A1) (T (CONS (CAR A1) (APPEND (CDR A1) A2))))
    [133] APPLY frame for call (APPEND '(3 . 6) '"lists shuffled")
    <15/128> 
    #<FUNCTION APPEND (A1 A2) (DECLARE (SYSTEM::IN-DEFUN APPEND))
      (BLOCK APPEND
       (COND ((NULL A2) A1) (T (CONS (CAR A1) (APPEND (CDR A1) A2)))))> 2
    [127] EVAL frame for form (APPEND (MY-APPEND (CAR A1) (CAR A2)) (SHUFFLE (CDR A1) (CDR A2)))
    <16/124> #<SPECIAL-OPERATOR COND>
    [123] EVAL frame for form 
    (COND ((/= (LEN A2) (LEN A1)) (PRINC "lengths of the lists are not equal"))
     ((AND (NULL A1) (NULL A2)) (PRINC "lists shuffled"))
     (T (APPEND (MY-APPEND (CAR A1) (CAR A2)) (SHUFFLE (CDR A1) (CDR A2)))))
    [106] APPLY frame for call (SHUFFLE '(3) '(6))
    <17/101> 
    #<FUNCTION SHUFFLE (A1 A2) (DECLARE (SYSTEM::IN-DEFUN SHUFFLE))
      (BLOCK SHUFFLE
       (COND
        ((/= (LEN A2) (LEN A1)) (PRINC "lengths of the lists are not equal"))
        ((AND (NULL A1) (NULL A2)) (PRINC "lists shuffled"))
        (T
         (APPEND (MY-APPEND (CAR A1) (CAR A2)) (SHUFFLE (CDR A1) (CDR A2))))))> 2
    [100] EVAL frame for form (SHUFFLE (CDR A1) (CDR A2))
    [94] EVAL frame for form (APPEND (MY-APPEND (CAR A1) (CAR A2)) (SHUFFLE (CDR A1) (CDR A2)))
    <18/91> #<SPECIAL-OPERATOR COND>
    [90] EVAL frame for form 
    (COND ((/= (LEN A2) (LEN A1)) (PRINC "lengths of the lists are not equal"))
     ((AND (NULL A1) (NULL A2)) (PRINC "lists shuffled"))
     (T (APPEND (MY-APPEND (CAR A1) (CAR A2)) (SHUFFLE (CDR A1) (CDR A2)))))
    [73] APPLY frame for call (SHUFFLE '(2 3) '(5 6))
    <19/68> 
    #<FUNCTION SHUFFLE (A1 A2) (DECLARE (SYSTEM::IN-DEFUN SHUFFLE))
      (BLOCK SHUFFLE
       (COND
        ((/= (LEN A2) (LEN A1)) (PRINC "lengths of the lists are not equal"))
        ((AND (NULL A1) (NULL A2)) (PRINC "lists shuffled"))
        (T
         (APPEND (MY-APPEND (CAR A1) (CAR A2)) (SHUFFLE (CDR A1) (CDR A2))))))> 2
    [67] EVAL frame for form (SHUFFLE (CDR A1) (CDR A2))
    [61] EVAL frame for form (APPEND (MY-APPEND (CAR A1) (CAR A2)) (SHUFFLE (CDR A1) (CDR A2)))
    <20/58> #<SPECIAL-OPERATOR COND>
    [57] EVAL frame for form 
    (COND ((/= (LEN A2) (LEN A1)) (PRINC "lengths of the lists are not equal"))
     ((AND (NULL A1) (NULL A2)) (PRINC "lists shuffled"))
     (T (APPEND (MY-APPEND (CAR A1) (CAR A2)) (SHUFFLE (CDR A1) (CDR A2)))))
    [40] APPLY frame for call (SHUFFLE '(1 2 3) '(4 5 6))
    <21/35> 
    #<FUNCTION SHUFFLE (A1 A2) (DECLARE (SYSTEM::IN-DEFUN SHUFFLE))
      (BLOCK SHUFFLE
       (COND
        ((/= (LEN A2) (LEN A1)) (PRINC "lengths of the lists are not equal"))
        ((AND (NULL A1) (NULL A2)) (PRINC "lists shuffled"))
        (T
         (APPEND (MY-APPEND (CAR A1) (CAR A2)) (SHUFFLE (CDR A1) (CDR A2))))))> 2
    [34] EVAL frame for form (SHUFFLE '(1 2 3) '(4 5 6))
    Printed 21 frames
    

    慢慢阅读。你可以忽略前10行;它们是clisp调试器本身的工件。但其余部分将让您了解您的计划正在做什么。