[Haskell-cafe] Hi can u explain me how drop works in Haskell

Antonio Cangiano acangiano at gmail.com
Sun Feb 25 21:31:21 EST 2007


On 2/25/07, iliali16 <iliali16 at gmail.com> wrote:
>
>
> Hi I am trying to implement the function drop in haskell the thing is that
> I
> I have been trying for some time and I came up with this code where I am
> trying to do recursion:
>
> drop :: Integer -> [Integer] -> [Integer]
> drop 0 (x:xs) = (x:xs)
> drop n (x:xs)
>         |n < lList (x:xs) = dropN (n-1) xs :
>         |otherwise = []


drop :: Integer -> [a] -> [a]
drop n xs | n < 1 =  xs
drop _ [] =  []
drop n (_:xs) =  drop (n-1) xs

Line 1: It specifies that drop will accept an Integer and a list, and return
a list;
Line 2: If n < 1, the function will return the list as it is (this pattern
is matched if you're dropping 0 or -2 elements, for example);
Line 3: No matter what Integer has been passed to the function, if the list
passed is empty, an empty list will be returned as well;
Line 4: Dropping n elements from a list is equivalent to dropping n-1
elements from the tail (xs) of that same list.

HTH
Antonio
-- 
http://antoniocangiano.com
Zen and the Art of Ruby Programming
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070225/86455fdc/attachment.htm


More information about the Haskell-Cafe mailing list