Saturday, 4 June 2016

Haskell: Remove first n elements of list


listUtil.hs
{- Remove first n elements from list-}
removeEle :: Int -> [a] -> [a]
removeEle n xs
    | ((n <= 0) || null xs) = xs
    | otherwise = removeEle (n-1) (tail xs)

*Main> :load listUtil.hs
[1 of 1] Compiling Main             ( listUtil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> removeEle 2 [2,3,5,7]
[5,7]
*Main> removeEle (-2) [2,3,5,7]
[2,3,5,7]
*Main> removeEle 0 [2,3,5,7]
[2,3,5,7]
*Main> removeEle 10 [2,3,5,7]
[]
*Main> removeEle 10 []
[]
*Main> 



Previous                                                 Next                                                 Home

No comments:

Post a Comment