Lisp函数接受两个列表并返回它们之间的乘积

时间:2018-12-14 03:06:31

标签: list lisp

这是为了做作业,我很困惑从哪里开始。 例如: 列表一是(1 2 3),列表二是(3 4 6) 返回29是因为(1 * 3)+(2 * 4)+(3 * 6)

1 个答案:

答案 0 :(得分:1)

不会破坏答案,但这是您可能期望得到的提示:

(defun sum-lists% (x y z)  ...)
(defun sum-lists (x y) ... )

跟踪两个功能:

(trace sum-lists sum-lists%)

您的示例:

(sum-lists '(1 2 3) '(3 4 6))

  0: (SUM-LISTS (1 2 3) (3 4 6))
    1: (SUM-LISTS% (1 2 3) (3 4 6) 0)
      2: (SUM-LISTS% (2 3) (4 6) 3)
        3: (SUM-LISTS% (3) (6) 11)
          4: (SUM-LISTS% NIL NIL 29)
          4: SUM-LISTS% returned 29
        3: SUM-LISTS% returned 29
      2: SUM-LISTS% returned 29
    1: SUM-LISTS% returned 29
  0: SUM-LISTS returned 29

如果您要处理一些特殊情况,还可以:

 (sum-lists '(1 2 3 4) '(3 4))

  0: (SUM-LISTS (1 2 3 4) (3 4))
    1: (SUM-LISTS% (1 2 3 4) (3 4) 0)
      2: (SUM-LISTS% (2 3 4) (4) 3)
        3: (SUM-LISTS% (3 4) NIL 11)
          4: (SUM-LISTS% (4) NIL 14)
            5: (SUM-LISTS% NIL NIL 18)
            5: SUM-LISTS% returned 18
          4: SUM-LISTS% returned 18
        3: SUM-LISTS% returned 18
      2: SUM-LISTS% returned 18
    1: SUM-LISTS% returned 18
  0: SUM-LISTS returned 18