【发布时间】:2016-12-08 22:29:46
【问题描述】:
我是 Haskell 和函数式编程的新手,有点困惑。为什么我不能 curry 一个匿名函数,甚至有可能吗?
我有以下代码:
largestDivisible :: (Integral a) => a -> a
largestDivisible x
| x <= 0 = error "NOT A VALID VALUE"
| otherwise = head (myFilter (f x) [x-1, x-2..1])
where f x y= x `mod` y == 0
当我尝试这样写时:
largestDivisible :: (Integral a) => a -> a
largestDivisible x
| x <= 0 = error "NOT A VALID VALUE"
| otherwise = head (myFilter (\ x y = x `mod` y == 0) [x-1, x-2..1])
然后,如果我尝试将其加载到 GHCi 中,则会收到以下错误:
ListStuff.hs:85:35: error:
• Couldn't match expected type ‘Bool’ with actual type ‘a -> Bool’
• The lambda expression ‘\ x y -> (mod x y == 0)’
has two arguments,
but its type ‘a -> Bool’ has only one
In the first argument of ‘myFilter’, namely
‘(\ x y -> (mod x y == 0))’
In the first argument of ‘head’, namely
‘(myFilter (\ x y -> (mod x y == 0)) [x - 1, x - 2 .. 1])’
• Relevant bindings include
x :: a (bound at ListStuff.hs:83:19)
largestDivisible' :: a -> a (bound at ListStuff.hs:83:1)
Failed, modules loaded: none.
【问题讨论】:
-
你为什么用
myFilter而不是filter?我认为更合适的是使用find而不是filter。
标签: haskell functional-programming currying