Search code examples
haskellhaskell-platformpointfreepurely-functional

Transforming a Haskell function to a point free representaion


I want to create a pointfree function that takes a list of functions, applies a single argument to each listed function, and then compresses the list via another function. A pointfree version of this function would have the following type signature:

multiplex :: ([a] -> b) -> [(c -> a)] -> (c -> b)

And an example usage:

invariantsHold :: (Integral a) => a -> Bool
invariantsHold = multiplex (all id) [(>=0),(<=50),even]

I was able to write the following:

multiplex :: ([a] -> b) -> [(c -> a)] -> c -> b
multiplex f xs e = f $ map ((flip ($)) e) xs

This implementation is not pointfree, how can I transform this function to a pointfree representation?


Solution

  • Not in pointfree style, but surely could be simplified significantly by using Applicative (need to import Control.Applicative):

    multiplex f xs e = f $ xs <*> pure e
    

    invariantsHold also could be simplified a little:

    invariantsHold = multiplex and [(>=0),(<=50),even]
    

    Using sequenceA (from Data.Traversable) is another way to define this multiplex:

    multiplex f xs e = f $ sequenceA xs e
    

    And this definition can be rewritten in pointfree style (give by pointfree):

    multiplex = (. sequenceA) . (.)
    

    or

    multiplex = flip ((.) . (.)) sequenceA
    

    Beautiful, but seems pointless to me :-)