使用默认值压缩而不是删除值?

编程入门 行业动态 更新时间:2024-10-25 00:37:01
本文介绍了使用默认值压缩而不是删除值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在 Haskell 中寻找一个函数来压缩两个长度可能不同的列表.我能找到的所有 zip 函数只是删除比另一个长的列表的所有值.

I'm looking for a function in haskell to zip two lists that may vary in length. All zip functions I could find just drop all values of a lists that is longer than the other.

例如:在我的练习中,我有两个示例列表.如果第一个比第二个短,我必须使用 0 填充.否则我必须使用 1.我不允许使用任何递归.我只需要使用高阶函数.

For example: In my exercise I have two example lists. If the first one is shorter than the second one I have to fill up using 0's. Otherwise I have to use 1's. I'm not allowed to use any recursion. I just have to use higher order functions.

有什么我可以使用的功能吗?到目前为止,我真的找不到任何解决方案.

Is there any function I can use? I really could not find any solution so far.

推荐答案

您可以将 0 或 1 的 inifinte 列表附加到每个列表中,然后从结果压缩列表中获取您需要的数字:

You can append an inifinte list of 0 or 1 to each list and then take the number you need from the result zipped list:

zipWithDefault :: a -> b -> [a] -> [b] -> [(a,b)] zipWithDefault da db la lb = let len = max (length la) (length lb) la' = la ++ (repeat da) lb' = lb ++ (repeat db) in take len $ zip la' lb'

更多推荐

使用默认值压缩而不是删除值?

本文发布于:2023-11-17 03:18:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:默认值   而不是

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!