[Haskell-cafe] Ultra-newbie Question

Aai bradypus at xs4all.nl
Sun Sep 19 10:23:17 EDT 2010


I like this one!

Here's a variant using fold:

lastk :: Int -> [a] -> [a]
lastk k xs = foldl' (const.tail) xs (drop k xs)

or point free:
lastk = ap (foldl' (const. tail)). drop


Hallo Luke Palmer, je schreef op 18-09-10 22:42:
> I think this is O(n) time, O(1) space (!).
>
> lastk :: Int -> [a] -> [a]
> lastk k xs = last $ zipWith const (properTails xs) (drop k xs)
>     where properTails = tail . tails
>
>   

-- 
Met vriendelijke groet,
=@@i



More information about the Haskell-Cafe mailing list