Monday 6 June 2016

Haskell: Sort elements of list of string by their length.


ListUtil.hs
{-# LANGUAGE TransformListComp #-}

import GHC.Exts

sortList :: [String] -> [String]
sortList list = [x | x <- list, then sortWith by (length x)]

Prelude> :load ListUtil.hs
[1 of 1] Compiling Main             ( ListUtil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> sortList ["Hari", "Krishna", "PTR", "Nayan"]
["PTR","Hari","Nayan","Krishna"]



Previous                                                 Next                                                 Home

No comments:

Post a Comment