[Haskell-cafe] Ultra-newbie Question

Christopher Tauss ctauss1 at gmail.com
Sat Sep 18 14:01:11 EDT 2010


Thanks you Ivan and David for clarifying this.

Best Regards,

Chris

On Sat, Sep 18, 2010 at 3:55 AM, Ivan Lazar Miljenovic <
ivan.miljenovic at gmail.com> wrote:

>  On 18 September 2010 17:51, Christopher Tauss <ctauss1 at 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.
>
> Easiest way I can think of:
>
> n_lastn n = reverse . take n . reverse
>
> Alternatively:
>
> n_lastn n xs = drop (len - n) xs
>    where
>        len = length xs
>
> --
> Ivan Lazar Miljenovic
> Ivan.Miljenovic at gmail.com
> IvanMiljenovic.wordpress.com <http://ivanmiljenovic.wordpress.com/>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100918/0b2aa8b5/attachment.html


More information about the Haskell-Cafe mailing list