如何解决Haskell类型错误

时间:2011-09-23 05:42:33

标签: haskell typeerror

我正在尝试学习Haskell,我正在编写解压缩函数(是的,我知道它是内置的,这是练习)但我的递归行有问题。我有:

-- unzip turns a list of two element tuples into two lists
unzip' [] = ([],[])
unzip' [(x,y):ls] = 
    let 
        (a, b) = unzip' ls
    in
        ([x] ++ a, [y] ++ b)

但我收到错误:

Couldn't match expected type `(t, t1)'
           against inferred type `[(t, t1)]'
      Expected type: [(t, t1)] -> (t2, t3)
      Inferred type: [[(t, t1)]] -> ([a], [a1])
    In the expression: unzip' ls
    In a pattern binding: (a, b) = unzip' ls

我不明白如何解压缩递归调用的结果。任何人都可以解释如何从返回的元组中解压缩两个列表吗?

1 个答案:

答案 0 :(得分:6)

我不知道这是否解决了它,但它在我看来

unzip' [(x,y):ls] =

应该是:

unzip' ((x,y):ls) =