在lisp函数中使用带有缺点的嵌套汽车

Ali*_*sha 2 lisp clisp list common-lisp cons

我正在制作一个递归的lisp函数,它接受两个列表并生成索引对的子列表

例如:投入(ABCD)和(1 2 3 4)并获得((1 A)(2 B)(3 C)(4 D))

但是,我在使用汽车和缺点制作上述子列表时遇到了麻烦.这是我的代码:

(DEFUN zipper (a b)
    (if (= (OR (list-length a) (list-length b)) 0)
        (setq c NIL)
        (progn (zipper (cdr a) (cdr b))
        (cons '((car a) (car b)) c))
    )
)
Run Code Online (Sandbox Code Playgroud)

我玩了一下,似乎用汽车创建列表大部分时间都不起作用.另外,我正在使用CLISP.有任何想法吗?

谢谢!

Rai*_*wig 5

总体思路进入了正确的方向.但是有很多问题.我们来看一下.

(DEFUN zipper (a b)
    (if (= (OR (list-length a) (list-length b)) 0)
        (setq c NIL)
        (progn (zipper (cdr a) (cdr b))
        (cons '((car a) (car b)) c))
    )
)
Run Code Online (Sandbox Code Playgroud)

第一个缩进:

(DEFUN zipper (a b)
  (if (= (OR (list-length a) (list-length b)) 0)
      (setq c NIL)
      (progn (zipper (cdr a) (cdr b))
        (cons '((car a) (car b)) c))                 ; <--
    )
  )
Run Code Online (Sandbox Code Playgroud)

下一个悬空括号:

(DEFUN zipper (a b)
  (if (= (OR (list-length a) (list-length b)) 0)
      (setq c NIL)
      (progn (zipper (cdr a) (cdr b))
        (cons '((car a) (car b)) c))))
Run Code Online (Sandbox Code Playgroud)

返回IF中的空列表以获取真实案例:

(defun zipper (a b)
  (if (= (OR (list-length a) (list-length b)) 0)
      nil
      (progn (zipper (cdr a) (cdr b))
        (cons '((car a) (car b)) c))))
Run Code Online (Sandbox Code Playgroud)

现在对错误案例的结果有所了解:

(defun zipper (a b)
  (if (= (OR (list-length a) (list-length b)) 0)
      nil
      (cons '((car a) (car b))
            (zipper (cdr a) (cdr b)))))
Run Code Online (Sandbox Code Playgroud)

还有什么需要做的?

  • 通过'rsm'查看评论:通过调用替换引号list.
  • 不要用list-length.请null改用.它检查列表是否为空.list-length会在每次调用时遍历输入的整个列表 - >效率低下.