本文介绍了合并Haskell中的两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不知道如何在Haskell中以如下方式合并两个列表: INPUT:[1,2,3,4,5] [11,12,13,14]
输出:[1,11,2,12,3,13,4,14, 5]
解决方案
merge :: [a] - > [a] - > [a]
合并xs [] = xs
merge [] ys = ys
merge(x:xs)(y:ys)= x:y:合并xs ys
Can't figure out how to merge two lists in the following way in Haskell:
INPUT: [1,2,3,4,5] [11,12,13,14]
OUTPUT: [1,11,2,12,3,13,4,14,5]
解决方案
merge :: [a] -> [a] -> [a]
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys) = x : y : merge xs ys
这篇关于合并Haskell中的两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!