26 August 2009

pointless

This post is a literate haskell file.

I like the pointless style of programming - that is, functions do not mention the variables they act upon explicitly. While this is very common and cool, it's also a source of amusement - occasionally I find myself distract from a project by a need/desire/delusional whim to re-write various functions as pointless as possible. So here is a little one, it's nothing special.

> import Control.Arrow

> por1 :: (b -> Bool) -> (b -> Bool) -> b -> Bool
> por1 = curry $ curry $ uncurry (||) . (uncurry $ uncurry (&&&))

That is, por is a function which takes two predicates and input -
each predicate is from a polymorphic type b to a boolean
Apply both the predicates to the input, producing two boolean valies
Apply "Or" to these values to return one result.

There are automated tools to do this sort of obsfucation with haskell but I find my brain boils attempting to hack out this sort of "write-only" code. (this is a good thing)

Here is what the pointfree took makes of a pointful version* of the above:

> por2 = ((uncurry (||) .) .) . flip (&&&)

I like that, though the flip, in this case, is semantically irrelevant.

More about the pointless style
also comparison to unix pipelines
The question is: how to implement uncurry in bash? Next post, possibly. Hobgoblin has had the better of me tonight! :)

* the pointful version:

> por3 p q = uncurry (||) . (q &&& p)

No comments:

Post a Comment