Monday, 6 June 2016

Haskell: Square every element of list and then reverse


ListUtil.hs
{-# LANGUAGE TransformListComp #-}
squareReverse :: [Integer] -> [Integer]
squareReverse list = [x^2 | x <- list, then reverse]

Prelude> :load ListUtil.hs
[1 of 1] Compiling Main             ( ListUtil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> squareReverse [1, -2, 8, -6]
[36,64,4,1]


Previous                                                 Next                                                 Home

No comments:

Post a Comment