0

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?

duplode
  • 33,731
  • 7
  • 79
  • 150
HandsomeGorilla
  • 585
  • 1
  • 3
  • 20

1 Answers1

8

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
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319