【发布时间】:2014-03-26 00:48:01
【问题描述】:
我有一个带有两个参数的函数,我必须对其进行模式匹配。如果我在第一个模式上使用柯里化,它将无法编译:
drop' :: Int -> [a] -> [a]
drop' 0 = id -- ghci: "Equations for drop' have different numbers of arguments"
drop' n (x:xs) = drop' (n-1) xs
编译器给出这个输出:
99.hs:106:3:
Equations for drop' have different numbers of arguments
99.hs:106:3-15
99.hs:107:3-33
In an equation for `split':
split xs n
= (take' n xs, drop' n xs)
where
take' 0 _ = []
take' n (x : xs) = x : take (n - 1) xs
drop' 0 = id
drop' n (x : xs) = drop' (n - 1) xs
Failed, modules loaded: none.
如果我只给出柯里化模式,那么它会编译:
drop' :: Int -> [a] -> [a]
drop' 0 = id -- compiles
什么给了?
【问题讨论】:
-
假设你知道你可以简单地写
drop' 0 x = x? -
定义函数的所有方程必须具有相同数量的参数。所以
drop 0 = id; drop n = \ (_:xs) -> drop (n-1) xs会编译。 -
@Koterpillar 是的,我知道。
标签: haskell pattern-matching currying