I’m writing a function in Haskell that recursively returns the list with the specified number of elements removed from the front of the list. I’ve gotten it to work doing this:
removefront :: Int -> [Int] -> [Int]
removefront n xs =
if n <= 0 then xs
else removefront (n-1) (tail xs)
This works and does exactly what I want however is there a way to do the same thing without the tail function. Thanks!
this function is “built in” in the sense that it is in the prelude and called drop
now, I will assume that is not the answer you were looking for. You can easily modify your function to not use tail. The trick is to use pattern matching.
three notes
Most Haskeller would not use
if then elsefor such a function, preferring guardsThe type of
removefrontcan be much more generalactually it could be all the way to
but that is getting excessive
you should consider what happens when you hand your function the empty list–what do you want it to do?