Search code examples
haskellpointfree

Haskell difference lists and point free function


I was researching difference lists and found the DList type

newtype DList a = DL { unDL :: [a] -> [a] }

and the function

dlToList :: DList a -> [a]
dlToList = ($[]) . unDL

I am wondering what is the non point free version of the function and what does ($[]) do?


Solution

  • The first step in seeing into a point-free definition of a function is to revert the η-reduction:

    dlToList = ($[]) . unDL
    dlToList dl = (($[]) . unDL) dl
    

    Then you start applying to the composition-chain, right-to-left:

    dlToList dl = ($[]) (unDL dl)
    

    You could then unpack the operator section

    dlToList dl = unDL dl $ []
    

    However, keeping the ($[]) as it is actually makes sense, because this is the essential converter between difference lists and ordinary lists: it takes a [a]->[a]-prepender-function and applies it to the terminator [], resulting in a concrete list.


    We could simplify that further:

    dlToList dl = unDL dl []
    

    which, incidentally, could be made point-free again in a shorter manner:

    dlToList = (`unDL`[])