Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to express the absolute error function in point-free notation?

In pointful notation:

absoluteError x y = abs (x-y)

An unclear example in pointfree notation:

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

like image 449
Ben Hamner Avatar asked Jun 13 '12 00:06

Ben Hamner


2 Answers

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 (-)
like image 116
Will Ness Avatar answered Nov 10 '22 15:11

Will Ness


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!

like image 42
Daniel Wagner Avatar answered Nov 10 '22 16:11

Daniel Wagner