haskell - 如何解决 Haskell 类型错误

标签 haskell typeerror

我正在尝试学习 Haskell,并且正在编写 unzip 函数(是的,我知道它是内置的,这是练习),但我的递归行遇到了问题。我有:

-- 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

我不明白如何解压递归调用的结果。谁能解释如何从返回的元组中解压两个列表?

最佳答案

我不知道这是否能解决问题,但在我看来是这样的

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

应该是:

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

关于haskell - 如何解决 Haskell 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7524767/

相关文章:

Python 类型错误

Haskell,获取所有枚举值的列表而不命名它们

list - 使用列表理解将元组列表转换为其元素列表

haskell - 如何使用 hpack 拥有多个库?

haskell - putStr 和 putStrLn 弄乱了输出

python - "TypeError: ' 设置 ' object is not callable"

haskell - 从自定义类型创建随机数据

python - TypeError: a bytes-like object is required, not 'str' – 在Python中保存JSON数据

python - Seaborn TypeError : Cannot cast array data from dtype ('int64' ) to dtype ('int32' ) according to the rule 'safe'

F# 多项式微分器