我最近在Haskell遇到了一个奇怪的问题。下面的代码应该返回一个被裁剪成范围的值(如果它高于high,它应该返回high,如果它在low下面,它应该返回low。
inRange :: Int -> Int -> Int -> Int
inRange low high = max low $ min high错误信息是:
scratch.hs:2:20:
Couldn't match expected type ‘Int -> Int’ with actual type ‘Int’
In the expression: max low $ min high
In an equation for ‘inRange’: inRange low high = max low $ min high
scratch.hs:2:30:
Couldn't match expected type ‘Int’ with actual type ‘Int -> Int’
Probable cause: ‘min’ is applied to too few arguments
In the second argument of ‘($)’, namely ‘min high’
In the expression: max low $ min high难道不应该再争论一次并把它放高吗?我已经尝试过其他的可能性,比如:
\x -> max low $ min high x和
\x -> max low $ (min high x)在GHCI中尝试时,我得到以下错误:
<interactive>:7:5:
Non type-variable argument in the constraint: Num (a -> a)
(Use FlexibleContexts to permit this)
When checking that ‘inRange’ has the inferred type
inRange :: forall a.
(Num a, Num (a -> a), Ord a, Ord (a -> a)) =>
a -> a发布于 2017-03-19 15:59:30
($)被定义为:
f $ x = f x所以你的例子实际上是:
max low (min high)这是错误的,因为你真的想
max low (min high x)使用功能组合,其定义为:
f . g = \x -> f (g x)我们得到的工作示例\x -> max low (min high x):
\x -> max low (min high x)
== max low . min high -- by definition of (.)https://stackoverflow.com/questions/42888723
复制相似问题