简体   繁体   中英

Is there a way to predict infix function behavior for partially applied functions in Haskell?

I have two functions--

partialSubtractionWith5 :: (Num a) => a -> a
partialSubtractionWith5 = (subtract 5)

and

partialSubtractionWith5' :: (Num a) => a-> a
partialSubtractionwith5' = (`subtract` 5)

calling partialSubtractionWith5 x returns the equivalent of x - 5, while calling partialSubtractionWith5' x returns the equivalent of 5 - x .

In Learn You a Haskell , Lipovača defines the following function--

isUpperAlphanum :: Char -> Bool
isUpperAlphanum = (`elem` ['A'..'B'])

Which (based on my experiments with subtract ) I would have thought would have behaved like so when called as isUpperAlphanum 'some char' :

Prelude> ['A'..'B'] `elem` 'some char'
False

Clearly, this is not the case. But why? And is there a way to predict what functions will reverse their arguments when partially applied?

There is no contradiction, it's just that subtract = flip (-) . Ie

partialSubtractionWith5' x ≡ (`subtract` 5) x
                           ≡ x `subtract` 5
                           ≡ 5 - x

and, likewise,

isUpperAlphanum '□' ≡ '□' `elem` ['A'..'B']

OTOH,

partialSubtractionWith5 x ≡ (subtract 5) x
                          ≡ (5`subtract`) x
                          ≡ 5 `subtract` x
                          ≡ x - 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM