16

In pointful notation:

absoluteError x y = abs (x-y)

An unclear example in pointfree notation:

absoluteError' = curry (abs . uncurry (-))

Ben Hamner
  • 4,575
  • 4
  • 30
  • 50
  • 16
    If it's clear in pointy notation, then what's wrong with it? This looks like the sort of example where any point-free version is going to have to be read by mentally converting back anyway... – Ben Jun 13 '12 at 00:49

2 Answers2

33

Here's how you could derive it yourself, in small steps:

absoluteError x y = abs (x-y) = abs ((-) x y) = abs ( ((-) x) y) 
                  = (abs . (-) x) y = ( (abs .) ((-) x) ) y = 
                  = ( (abs .) . (-) ) x y

so, by eta-reduction, if f x y = g x y we conclude f = g.

Further, using _B = (.) for a moment,

(abs .) . (-) = _B (abs .) (-) = _B (_B abs) (-) = (_B . _B) abs (-)
              = ((.) . (.)) abs (-)
Will Ness
  • 70,110
  • 9
  • 98
  • 181
26

Here's a handful of ways.

  1. the old-fashioned: absoluteError = (abs .) . (-)
  2. use the so-called "boobs operator", or "owl operator" absoluteError = ((.) . (.)) abs (-)
  3. name the boobs operator something more politically correct (and what the heck, generalize it at the same time)

    (.:) = fmap fmap fmap
    absoluteError = abs .: (-)
    
  4. using semantic editor combinators:

    result :: (o1 -> o2) -> (i -> o1) -> (i -> o2)
    result = (.)
    
    absoluteError = (result . result) abs (-)
    

Of course, these are all the same trick, just with different names. Enjoy!

Davorak
  • 7,362
  • 1
  • 38
  • 48
Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380