[Haskell-cafe] getting last char of String

Jesse Schalken jesseschalken at gmail.com
Sat Jan 1 09:27:28 CET 2011


On Sat, Jan 1, 2011 at 8:54 AM, Felipe Almeida Lessa <felipe.lessa at gmail.com
> wrote:

> On Fri, Dec 31, 2010 at 6:43 PM, aditya siram <aditya.siram at gmail.com>
> wrote:
> > -- untested and won't work on an infinite list
> > last :: [a] -> a
> > last = head . reverse
>
> No definition for last works with infinite lists =).


Unless you make the result nullable, of course.

maybeLast :: [a] -> Maybe a


maybeLast [] = Nothing

maybeLast [x] = Just x
maybeLast (_:xs) = maybeLast xs


> Cheers,
>
> --
> Felipe.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110101/48164248/attachment.htm>


More information about the Haskell-Cafe mailing list