Link to home
Start Free TrialLog in
Avatar of Fero45
Fero45

asked on

Haskell drop function

Dear Experts

I understand that when I create my own myLength function, e.g.
myLength :: [a] -> Int
myLength [] = 0
myLength (_:xs) = 1 + myLength xs

and as example have a string "apple", the internal logic of Haskell is as follows:
1 + (myLength "pple")
1 + (1 + (myLength "ple"))
1 + (1 + (1 + (myLength "le")))
1 + (1 + (1 + (1 + (myLength "e"))))
1 + (1 + (1 + (1 + (1 + (myLength []])))))
1 + (1 + (1 + (1 + (1 + ( 0 )))))

We have the Haskell "drop" function (example taken from cs.arizona.edu)
drop :: Int -> [a] -> [a]
drop 0 xs = xs
drop _ [] = []
drop (n+1) (x:xs) = drop n xs

I do not understand the last line. Could somebody explain to me this function the same way as myLength using the same string "apple"?

Thanks
Fero
ASKER CERTIFIED SOLUTION
Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fero45
Fero45

ASKER

Thank you.
I got misled by addition  (n+1). I assumed it should be (n-1)