【发布时间】:2013-12-01 00:46:10
【问题描述】:
出于学习目的,我尝试将 2 个列表压缩在一起,前提是两个列表的长度匹配。 (必须是相同的长度)不幸的是,它拒绝编译。 我认为签名有问题..谢谢您的期待。 这是我的代码:
ziptogether :: (Ord a) => [a] -> [a] -> [a]
ziptogether [] [] = 0
ziptogether (x:xs) (y:ys)
| length(x:xs) == length(y:ys) = zip (x:xs) (y:xs)
| otherwise = error "not same length"
错误:
Could not deduce (a ~ (a, a))
from the context (Ord a)
bound by the type signature for
ziptogether :: Ord a => [a] -> [a] -> [a]
at new.hs:2:16-43
`a' is a rigid type variable bound by
the type signature for ziptogether :: Ord a => [a] -> [a] -> [a]
at new.hs:2:16
Expected type: [a]
Actual type: [(a, a)]
In the return type of a call of `zip'
In the expression: zip (x : xs) (y : xs)
In an equation for `ziptogether':
ziptogether (x : xs) (y : ys)
| length (x : xs) == length (y : ys) = zip (x : xs) (y : xs)
| otherwise = error "not same length"
【问题讨论】:
标签: haskell