haskell programming refactoring

More Haskell Refactoring

A while ago I posted some Haskell code for sorting a table of hierarchical ordered data into a tree. Since then, I’ve learned a little more Haskell, and about half of the code I wrote is covered by standard libraries, or not necessary. Here’s an slimmed down version that makes greater use of GHC’s wealth of libraries, and for bonus marks, it’s also point free:

import Data.Tree
import Data.List (groupBy)
import Data.Function (on)
import Control.Applicative
 
listToForest :: Eq a => [[a]] -> Forest a
listToForest = map toBranch . groupBy ((==) `on` head) . filter (/= [])
               where toBranch = Node . (head . head) <*> (listToForest . map tail)
Syndicate content