[Haskell-cafe] Ultra-newbie Question

Jake McArthur jake.mcarthur at gmail.com
Sat Sep 18 13:44:38 EDT 2010


On 09/18/2010 02:51 AM, Christopher Tauss wrote:
> I am trying to write a function that takes a list and returns the last n elements.

This may just be for the sake of learning, in which case this is fine, 
but usually, needing to do this would be a sign that you are using lists 
improperly (since this is a O(n) time operation).

> 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]

     n_lastn n = reverse . take n . reverse

- Jake


More information about the Haskell-Cafe mailing list