Here is a more manual way to do it, hopefully this shows the approach required. You don't need to store anything, just keep removing the head of the list until its of the size you want. n_lastn :: Int -> [a] -> [a] n_lastn n xs = let len = length xs - n drp = if len < 0 then 0 else len rmv 0 ys = ys rmv m (y:ys) = rmv (m - 1) ys in rmv drp xs Cheers, David On 18 September 2010 17:51, Christopher Tauss <ctauss1@gmail.com> wrote:
Hello Haskell Community -
I am a professional programmer with 11 years experience, yet I just do not seem to be able to get the hang of even simple things in Haskell. I am trying to write a function that takes a list and returns the last n elements.
There may be a function which I can just call that does that, but I am trying to roll my own just to understand the concept.
Let's call the function n_lastn and, given a list [1,2,3,4,5], I would like n_lastn 3 = [3,4,5]
Seems like it would be something like:
n_lastn:: [a]->Int->[a] n_lastn 1 (xs) = last(xs) n_lastn n (x:xs) = ????
The issue is I do not see how you can store the last elements of the list.
Thanks in advance.
ctauss _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe