Monday 6 June 2016

Haskell: Square every element of list and then sort


ListUtil.hs
{-# LANGUAGE TransformListComp #-}

import GHC.Exts

squareSort :: [Integer] -> [Integer]
squareSort list = [x^2 | x <- list, then sortWith by x^2]

Prelude> :load ListUtil.hs
[1 of 1] Compiling Main             ( ListUtil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> squareSort [1, -2, 10, -8, 3, 5]
[1,4,9,25,64,100]


Previous                                                 Next                                                 Home

No comments:

Post a Comment