繁体   English   中英

在Common Lisp中的2个列表上递归应用函数

[英]Apply function recursively across 2 lists in Common Lisp

我有一个用通用LISP编写的函数,该函数将2个多项式的第一项相乘,从而完全按我的意愿工作(使用我编写的其他函数):

(defun firsttermmultiply (p1 p2)
    (let ((t1p1(car p1))
          (t1p2(car p2))
          (rem1(cdr p1))
          (rem2(cdr p2)))
        (cons (coeff t1p1 t1p2) (cutfront t1p1 t1p2)) 
    )
)

p1和p2是多项式,我想遍历两个列表,以便有一个较长的列表,其中将p1和p2中的所有术语应用于该行:

(cons (coeff t1p1 t1p2) (cutfront t1p1 t1p2)) 

我知道这需要使用rem1和rem2作为单独的递归行的参数,但是我无法理解该结构。

我必须从功能上做到这一点,所以我不能使用循环结构,而只能使用递归。

是其中之一吗? 假设这些多边形的顺序相同。

(defun firsttermmultiply (p1 p2)
  (if (or (null p1) (null p2))
      nil
      (let ((t1p1(car p1))
            (t1p2(car p2))
            (rem1(cdr p1))
            (rem2(cdr p2)))

        (cons (coeff t1p1 t1p2) (firsttermmultiply rem1 rem2)))))


(defun firsttermmultiply (p1 p2)
  (if (or (null p1) (null p2))
      nil
      (let ((t1p1(car p1))
            (t1p2(car p2))
            (rem1(cdr p1))
            (rem2(cdr p2)))

        (cons (cons (coeff t1p1 t1p2) (cutfront t1p1 t1p2))
              (firsttermmultiply rem1 rem2)))))


(defun firsttermmultiply (p1 p2)
  (if (or (null p1) (null p2))
      nil
      (let ((t1p1(car p1))
            (t1p2(car p2))
            (rem1(cdr p1))
            (rem2(cdr p2)))

        (cons (coeff t1p1 t1p2)
              (cons (cutfront t1p1 t1p2)
                    (firsttermmultiply rem1 rem2))))))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM