11

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?

Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
Rumca
  • 1,809
  • 12
  • 17
  • 2
    I frequently replace `.` with `>>>`, because left-to-right order seems to have a more natural feel to it - I guess because it follows the order we're used to with english, and it's the typical way we make pipelines in other languages. – Mark H Apr 04 '14 at 20:51
  • 4
    See also [semantic editor combinators](http://conal.net/blog/posts/semantic-editor-combinators). – Daniel Wagner Apr 05 '14 at 00:36
  • `(==) <*> qName` is what you have. – Will Ness Oct 07 '14 at 17:57

1 Answers1

8

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...).

Stephan Kulla
  • 4,739
  • 3
  • 26
  • 35