Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make pointfree functions more readable using different combinators than (.)?

What are potential alternative representations (e.g. using arrows, lenses, Haskell idioms, do syntax) of pointfree expressions that could read more like plain English?

Here is trivial example:

qNameIs :: String -> QName -> Bool
qNameIs = (. qName) . (==)

QName is a record from Text.Xml

What are possible equivalent to qNameIs but not pointful expressions? Ideally, ones that would show that first argument will be passed to (==) and result will be evaluated with result of qName applied to second argument of this expression?

like image 921
Rumca Avatar asked Apr 04 '14 20:04

Rumca


1 Answers

You can take the .^ operator of the module Data.Function.Pointless:

import Data.Function.Pointless (.^)

qNameIs :: String -> QName -> Bool
qNameIs = (==) .^ qName

An example with arrows (its not elegant...):

qNameIs :: String -> QName -> Bool
qNameIs = curry $ uncurry (==) . second qName

You can also write a new operator:

with :: (a -> c -> d) -> (b -> c) -> a -> b -> d
with f g = (. g) . f

Then you can write:

qNameIs = (==) `with` qName

which can be read as "equal with qName" (you can also take another operator name).

In General you shall also have a look on the module Data.Composition (Unfortunately it does not help in your case...).

like image 76
Stephan Kulla Avatar answered Sep 27 '22 16:09

Stephan Kulla