微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何求和两个列表的元素哈斯克尔

如何解决如何求和两个列表的元素哈斯克尔

我刚刚开始学习 Haskell,目前我正在探索列表的可能性。 我想总结两个列表,但不知何故出错了。

所以:

输入:sumTwoLists [2,5,7,9] [1,2,2](基本上是 25779 + 122)

输出:[2,9,1]

首先我把整个列表倒过来了,因为多位数字的加法必须从末尾开始:

reverseList :: [Int] -> [Int]
reverseList [] = []
reverseList (x:xs) = reverseList xs ++ [x]

它有效。然后我实现了一个add函数

add :: (Num a) => [a] -> [a] -> [a]
add _ [] = []
add [] _ = []
add (x:xs) (y:ys) = (x + y) : add xs ys

但是当一个列表比另一个更短时,就会出错。 (加 [2,2] = [3,9]) 所以 finction 也必须在较小数的末尾加 0 ([1,2] = [1,0]。)

在那之后,我尝试像这样实现 sumTwoLists 函数

sumTwoLists :: [Int] -> [Int] -> [Int]
sumTwoLists (x:xs) (y:ys) = reverseList ((reverseList (x:xs)) add (reverseList (y:ys)))

但是这段代码没有考虑元素不能大于9的事实。 我不想将元素转换为 Int 或 Integer,这就是为什么我不使用它们中的任何一个

我只是想反转列表,然后在最短列表中添加 0,然后将每个元素与另一个列表中的元素相加,如果结果 >9,则结果除以 10(mod? ) 且相邻数增加

如有任何帮助,我将不胜感激!

解决方法

用列表做这件事没有多大意义,因为它会引入很多额外的问题:

  1. 如果两个列表的长度不同,或者总和需要一个额外的元素,则用零填充;和
  2. 考虑到将两位数相加可能会导致值大于 9,从而引入进位

add 函数工作不正常,因为它从列表中的一个用完的那一刻停止,而且它没有考虑到数字可能“溢出”。因此,我们应该构造一个函数 add,它有一个额外的参数:进位:

add' :: Int -> [Int] -> [Int] -> [Int]
add' 0 [] [] = []
add' n [] [] = [n]
add' n xs [] = add' n xs [0]
add' n [] xs = add' n [0] xs
add' n (x:xs) (y:ys) = r : add' q xs ys
    where (q,r) = quotRem (x+y+n) 10

add 因此从零开始作为进位:

add :: [Int] -> [Int] -> [Int]
add = add' 0

如果我们这样计算反向列表的总和,我们得到:

Prelude> add [9,7,5,2] [2,2,1]
[1,9,2]

为了让它工作,你需要使用 add 作为中缀运算符,并且两个操作数可以是空或非空列表:

sumTwoLists :: [Int] -> [Int] -> [Int]
sumTwoLists xs ys = reverseList ((reverseList xs) `add` (reverseList ys))

或使用on :: (b -> b -> c) -> (a -> b) -> a -> a -> c

import Data.Function(on)

sumTwoLists :: [Int] -> [Int] -> [Int]
sumTwoLists = add `on` reverse

对于给定的样本输入,我们现在得到:

Prelude> sumTwoLists [2,9] [1,2]
[2,1]

最后,reverseList 已经存在于 Prelude 中:reverse :: [a] -> [a]。你实现的reverseList函数在O(n2)中运行,效率不是很高,可以使用累加器来获取线性时间。

,

这是基数为 10 的 adic 和。这是一个实现。

-- odometer (add one in base b)
odom :: Int -> [Int] -> [Int]
odom b (x : xs) | x<(b-1) = (x+1) : xs
                | xs==[] = [0,1]
                | otherwise = 0 : odom b xs

-- iterated odometer
odom_iter :: Int -> [Int] -> Int -> [Int]
odom_iter b t n | n == 0 = t
                | otherwise = odom b (odom_iter b t (n-1))

-- adic addition
sumadic :: Int -> [Int] -> [Int] -> [Int]
sumadic b (x:xs) (y:ys) | xs == [] = odom_iter b (y:ys) x
                        | ys == [] = odom_iter b (x:xs) y
                        | sumxy < b = (sumxy : (sumadic b xs ys))
                        | sumxy == b = (0 : (odom b (sumadic b xs ys)))
                        | otherwise = (1 : (odom b (sumadic b xs ys)))
                          where sumxy = x+y

这是通过反转列表来工作的:

> sumadic 10 [9,2]

因此,您只需将 sumadic 应用于反向列表并反向输出:

sumTwoLists x y = reverse $ sumadic 10 (reverse x) (reverse y)
,

此答案将使用 Haskell 的内置 reverse 而不是您的 reverseList,尽管它们可以互换。

让我们先看看你的 add 函数:

add :: (Num a) => [a] -> [a] -> [a]
-- look at these next 2 lines specfically
add _ [] = []
add [] _ = []
add (x:xs) (y:ys) = (x + y) : add xs ys

如果您将一个非空列表添加到一个空列表中,您当前的代码表示它是一个空列表,这显然不是真的(add [1,3] [] 应该是 [1,3])。因此,您可以首先通过返回另一个列表来修复您的基本情况:

add :: (Num a) => [a] -> [a] -> [a]
-- return the other list
add [] x = x
add x [] = x
add (x:xs) (y:ys) = (x + y) : add xs ys

如果您正在添加两个空列表,那么另一个列表就是空列表,因此您仍然可以正确返回空列表。现在,我们可以解决“这段代码没有考虑元素不能大于9”的部分。由于您的加法方法是模拟您在纸笔上进行加法的方式,因此请继续这样做。例如,如果结果是 12,则该数字为 2,并带有 1。请记住,mod 是除法的余数,因此 12 `mod` 10 (backticks in Haskell makes a function infix) 是 2,而 12 `div` 10,由于整数除法的性质向下取整,将为您提供每个第一个数字后的数字,即 1

话不多说,让我们写一些代码:

-- change Num to Integral because we need to work with integers
add :: (Integral a) => [a] -> [a] -> a -> [a]
--        we need to add a carry now ^
-- these base cases break down if carry is non-zero
add [] x c
    -- if carry is zero we're fine
    | c == 0    = x
    -- just add the carry in as a digit
    | otherwise = add [c] x 0
-- same applies here
add x [] c
    | c == 0    = x
    | otherwise = add x [c] 0
add (x:xs) (y:ys) c = dig : add xs ys rst
    where sum = x + y + c    -- find the sum of the digits plus the carry
          -- these two lines can also be written as (rst,dig) = sum `divMod` 10
          dig = sum `mod` 10 -- get the last digit
          rst = sum `div` 10 -- get the rest of the digits (the new carry)

现在,您的辅助函数可以使用 0 作为初始进位调用它:

addTwoLists :: (Num a) => [a] -> [a] -> [a]
addTwoLists x y = reverse $ add (reverse x) (reverse y) 0

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。