1
我有一个元组列表,元组的第一个元素可以是'a','b','c'。我想从该元组列表中创建3个列表的列表。第一个列表必须有'a'元组,第二个'b'元组和第三个'c'元组。下面是代码:将列表转换为列表清单
group mylist = group2 mylist [[],[],[]]
group2 mylist xs = if mylist == [] then xs
else if getFirst (head mylist) == 'a' then group2 (tail mylist) ([head mylist]++[head xs])++ (tail xs)
else if getFirst (head mylist) == 'b' then group2 (tail mylist) [head xs] ++ ((head mylist):[head (tail xs)]) ++ (tail xs)
else if getFirst (head mylist) == 'c' then group2 (tail mylist) (init xs) ++ ((head mylist):[last xs])
else xs
我得到这个错误:
Couldn't match expected type `(Char, t0, t1)' with actual type `[a0]' In the expression: [] In the second argument of `group2', namely `[[], [], []]' In the expression: group2 mylist [[], [], []] Failed, modules loaded: none.
样品输入:
[(c,1,2),(a,2,4),(c,5,6),(b,7,8),(a,9,1),(b,3,5),(c,2,5),(a,6,2),(c,1,8),(b,7,3),(a,4,4)]
输出:
[[(a,2,4),(a,9,1),(a,6,2),(a,4,4)],[(b,7,8),(b,3,5),(b,7,3)],[(c,1,2),(c,5,6),(c,2,5),(c,1,8)]]
我该如何解决这个问题?
你介意给一些样品的输入和输出,让您更清楚的问题? –
@LeeDuhem我把一个输入和输出。谢谢。 – jason
你可能想阅读[这篇文章关于'if语句等]的风格建议](http://stackoverflow.com/questions/16572446/why-are-if-expressions-frowned-upon-in-haskell)。简而言之:使用'head'几乎总是一个坏主意。 – leftaroundabout