我不能使用高阶函数。我看不出如何做到这一点。我对哈斯克尔很新。它也必须是递归的。Haskell:将偶数和奇数元素分解为元组
split :: [Int] -> ([Int],[Int])
split xs =
我给了这个开始。我真的不知道从哪里开始解决这个问题。
例子:
split []
([],[])
split [1]
([1],[])
split [1,2,3,4,5,6,7,8,9,10]
([1,3,5,7,9],[2,4,6,8,10])
任何帮助,将不胜感激。
编辑:它的偶数和奇数位置。
所以
分裂[3,6,8,9,10]将 ([3,8,10],[6,9])
行,所以我想出了这个。它不漂亮,但它似乎工作正常。
split :: [Int] -> ([Int],[Int])
split [] = ([],[])
split [xs] = ([xs],[])
split xs = (oddlist xs, evenlist xs)
oddlist :: [Int] -> ([Int])
oddlist xs | length xs <= 2 = [head(xs)]
| otherwise = [head(xs)] ++ oddlist(tail(tail(xs)))
evenlist :: [Int] -> ([Int])
evenlist xs | length xs <= 3 = [head(tail(xs))]
| otherwise = [head(tail(xs))] ++ evenlist(tail(tail(xs)))
你的例子是有点暧昧,你的意思是,即使在本身是偶数或奇数元素和奇数位或整数?一个更好的例子可能是:'split [1,3,2,5,8]''>([1,3,5],[2,8])' – 2010-09-14 09:53:47
哦,对不起,偶数和奇数位置的元素。 – Matt 2010-09-14 10:18:42
@Tom嘿谢谢。没有注意到。 – Matt 2010-09-14 10:44:07