邮编与默认值,而不是下降值? [英] Zip with default value instead of dropping values?
问题描述
我正在寻找一个haskell中的函数来压缩两个可能长度不同的列表。
我可以找到的所有zip函数都只是删除比另一个长的列表的所有值。
例如:
在我的练习中,我有两个示例列表。
如果第一个比第二个短我必须用0填满。否则,我必须使用1。
我不允许使用任何递归。我只需要使用更高阶的函数。
是否有任何函数可以使用?
到目前为止我还找不到任何解决方案。
您可以在每个列表中追加0或1的列表,然后从结果压缩列表中获取所需的数字:
zipWithDefault :: a - > b - > [a] - > [b] - > [(a,b)]
zipWithDefault da db la lb = let len = max(长度la)(长度lb)
la'= la ++(重复da)
lb'= lb ++(repeat db)
in len len $ zip la'lb'
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.
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.
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'
这篇关于邮编与默认值,而不是下降值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!