haskell - 在haskell中添加两个列表的元素
问题描述
add_lists list1 list2:返回每个列表中相同位置的元素之和作为一个新列表。如果一个列表比另一个列表短,则假设较短的列表在“缺失的地方”中有零。这是我的haskell代码:
add_lists list1 list2= if length.list1 < length.list2
then list2 ++ [i] && zipWith(+)list1 list2
else zipWith (+) list1 list2
where [i|i=0,i=length.list2-length.list1]
我不断收到错误
解决方案
在这里使用显式递归可能是最简单的。
add_lists [] list2 = list2
add_lists list1 [] = list1
add_lists (x:xs) (y:ys) = x + y : add_lists xs ys
推荐阅读
- r - 如何解决R中的内存大小问题?
- ssl - 是否可以为内部站点生成 SSL 证书?
- python - In Python, how do I change/insert code of a method by other objects/functions/methods
- python - 使用 numpy 求解二阶 ode
- vba - 粗体和下划线只是字符串的一部分
- vba - 发送电子邮件 Send-MailMessage - PowerShell
- python - GEKKO 中的 FOPDT 方程组 - 使用多个输入
- git - 在 Windows 上运行 git rebase 命令时删除符号链接 .git/logs
- sql - 如何使多个表的表左外连接
- jquery-select2 - 当我点击 select2 我的页面向右和底部滚动