Sunday 5 June 2016

Haskell: Delete the first occurrence of element from list


ListUtil.hs
deleteEle:: Eq a => a -> [a] -> [a]
deleteEle _ [] = []
deleteEle ele (x:xs)
    | (ele==x) = xs
    | otherwise = x : deleteEle ele xs

Prelude> :load ListUtil.hs
[1 of 1] Compiling Main             ( ListUtil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> :t deleteEle
deleteEle :: Eq a => a -> [a] -> [a]
*Main> deleteEle 10 []
[]
*Main> deleteEle 10 [2, 3, 10, 11, 10]
[2,3,11,10]
*Main> deleteEle 'a' "abrakadabra"
"brakadabra"
*Main> deleteEle [2,3] [[2,3,5],[2,3],[3,5,7],[2,3]]
[[2,3,5],[3,5,7],[2,3]]



Previous                                                 Next                                                 Home

No comments:

Post a Comment