添加两个列表 [英] Adding two lists

查看:108
本文介绍了添加两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加两个列表:(1 2 3)和(5 3 4)应该产生(6 5 7).
该函数应在相应位置添加元素,因此即使我有(9 1 2)+(5 2 6),它也应屈服(14 3 8).
我的功能

I want to add two lists : (1 2 3) and (5 3 4) should yield (6 5 7).
The function should add the elements on the corresponding position, so even if I would have (9 1 2) + ( 5 2 6) , it should yield (14 3 8).
My function

(defun add(l r)
    (setf return-value '())
        (loop for i from 0 to (- (length l) 1)
            do (setf return-value (cons (+(nth i l)(nth i r)) return-value))
        )
    (reverse return-value)
)

我如何创建一个类似的函数来减去列表?

How could I create a simmilar function which would subtract the lists ?

推荐答案

多项式算术

这似乎是对上一个问题将数字列表分解为数字的后续操作.那里,您有一个数字列表,它们是形式为

Polynomial arithmetic

This seems like a follow up to your previous question, Decompose a list of numbers into digits,. There, you had a list of digits numbers, and they were the ai of a polynomial of the form

i = 0… 10 i a i

i=0… 10iai

其中列表中的 n 数字是a 0 到a n-1 的值,其后的所有内容都假定为为零.您正在寻求一种规范化值的方法,以使每个a i 都在[0,9]范围内. 我对这个问题的回答显示了几种解决方法.

where the n numbers in your list are the values of a0 to an-1, and everything after that is presumed to be zero. You were asking for a way to normalize the values such that each ai was in the range [0,9]. My answer to that question showed a few ways to do that.

现在,一旦您将数字视为这种形式的多项式,就很容易看到您可以简单地逐段加减系数来获得正确的和或差系数(如果尚未归一化).例如

Now, once you view numbers as a polynomial of this form, it's easy to see that you can simply piecewise add and subtract coefficients to the get the right, if not yet normalized, coefficients of the sum or difference. E.g.,

378 = 8 + 7× 10 + 3× 100→ (8 7 3)
519 = 9 + 1× 10 + 5× 100→ (9 1 5)

378 = 8 + 7×10 + 3×100→ (8 7 3)
519 = 9 + 1×10 + 5×100→ (9 1 5)

总和就是

(8 + 9)+(7 + 1)× 10 +(3 + 5)× 100→ (mapcar '+ x y) (17 8 8)→ (7 9 8)

(8+9) + (7+1)×10 + (3+5)×100 →(mapcar '+ x y) (17 8 8) →(number->digits (digits->number …)) (7 9 8)

区别只是

(8-9)+(7-1)× 10 +(3-5)× 100→ (mapcar '- x y) (-1 6 -2)→ ??? ???

(8-9) + (7-1)×10 + (3-5)×100 →(mapcar '- x y) (-1 6 -2) →??? ???

我们这里没有适当的规范化过程.上一个问题中提供的那个在这里不起作用.但是,就系数而言,数字列表仍然是正确的,并且digits->number过程生成正确的值-141.

What we don't have here is an appropriate normalization procedure. The one provided in the previous question doesn't work here. However, the list of digits is still correct, insofar as coefficients go, and the digits->number procedure produces the correct value of -141.

因此,尽管您需要重新考虑显示负数数字列表的含义,但只要两个列表都具有相同的长度.如果它们的长度不同,则需要将较短的1填充为零.重新实现支持这种操作的mapcar在这里可能会有用.

So, while you'll need to rethink what it means to show a list of digits for a negative number, you can do the correct type of addition and subtraction on your lists with the following, as long as both lists have the same length. If they don't have the same length, you'll need to pad the shorter one with zeros. A reimplementation of mapcar that supports this sort of operation might be useful here.

(defun sum (x y)
  (mapcar '+ x y))

(defun difference (x y)
  (mapcar '- x y))

这篇关于添加两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆