Search code examples
haskellsyntaxcurryingpartial-application

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?


Solution

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

    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