
Is there an easy Haskell function that gets the last Char of a [Char] or String ? Many thanks in advance, Aaron

-- untested and won't work on an infinite list
last :: [a] -> a
last = head . reverse
-deech
On Fri, Dec 31, 2010 at 2:39 PM, Aaron Gray
Is there an easy Haskell function that gets the last Char of a [Char] or String ? Many thanks in advance, Aaron
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sat, Jan 1, 2011 at 8:54 AM, Felipe Almeida Lessa wrote: On Fri, Dec 31, 2010 at 6:43 PM, aditya siram -- 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@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sat, 1 Jan 2011, Jesse Schalken wrote:
On Sat, Jan 1, 2011 at 8:54 AM, Felipe Almeida Lessa
wrote: 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
How would this work for infinite lists?

On Sat, Jan 1, 2011 at 8:39 PM, Henning Thielemann < lemming@henning-thielemann.de> wrote:
On Sat, 1 Jan 2011, Jesse Schalken wrote:
On Sat, Jan 1, 2011 at 8:54 AM, Felipe Almeida Lessa <
felipe.lessa@gmail.com> wrote:
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
How would this work for infinite lists?
If your list is infinitely big, then reaching its end will take infinitely long. ;) It will loop forever, just like `last [1...]` does.

On Sat, Jan 1, 2011 at 10:06 PM, Jesse Schalken
On Sat, Jan 1, 2011 at 8:39 PM, Henning Thielemann < lemming@henning-thielemann.de> wrote:
On Sat, 1 Jan 2011, Jesse Schalken wrote:
On Sat, Jan 1, 2011 at 8:54 AM, Felipe Almeida Lessa <
felipe.lessa@gmail.com> wrote:
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
How would this work for infinite lists?
If your list is infinitely big, then reaching its end will take infinitely long. ;)
It will loop forever, just like `last [1...]` does.
Oh, sorry. My participation in this thread can be safely ignored. I read "No definition of last works for infinite lists" as "No definition of last works for empty lists", sorry.

On Sat, 2011-01-01 at 19:27 +1100, Jesse Schalken wrote:
On Sat, Jan 1, 2011 at 8:54 AM, Felipe Almeida Lessa
wrote: On Fri, Dec 31, 2010 at 6:43 PM, aditya siram 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
It may or may not be expected but it may also be written as: maybeLast' [] = Nothing maybeLast' (x:xs) = Just (fromMaybe x (maybeLast xs)) The main differences are: maybeLast (x:_|_) = _|_ maybeLast' (x:_|_) = Just _|_ maybeLast (fix (x:)) = _|_ maybeLast' (fix (x:)) = Just _|_ Regards

On Sat, 2011-01-01 at 19:27 +1100, Jesse Schalken wrote:
On Sat, Jan 1, 2011 at 8:54 AM, Felipe Almeida Lessa
wrote: On Fri, Dec 31, 2010 at 6:43 PM, aditya siram 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
It may or may not be expected but it may also be written as: maybeLast' [] = Nothing maybeLast' (x:xs) = Just (fromMaybe x (maybeLast xs)) The main differences are: maybeLast (x:_|_) = _|_ maybeLast' (x:_|_) = Just _|_ maybeLast (fix (x:)) = _|_ maybeLast' (fix (x:)) = Just _|_ Regards

On Sat, 2011-01-01 at 19:27 +1100, Jesse Schalken wrote:
On Sat, Jan 1, 2011 at 8:54 AM, Felipe Almeida Lessa
wrote: On Fri, Dec 31, 2010 at 6:43 PM, aditya siram 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
It may or may not be expected but it may also be written as: maybeLast' [] = Nothing maybeLast' (x:xs) = Just (fromMaybe x (maybeLast xs)) The main differences are: maybeLast (x:_|_) = _|_ maybeLast' (x:_|_) = Just _|_ maybeLast (fix (x:)) = _|_ maybeLast' (fix (x:)) = Just _|_ Regards

Sounds like you're looking for `last', which is in the Prelude. http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Prelude.h... Cheers, -Matt On Dec 31, 2010, at 3:39 PM, Aaron Gray wrote:
Is there an easy Haskell function that gets the last Char of a [Char] or String ?
Many thanks in advance,
Aaron
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 31 December 2010 20:44, Matthew Steele
Sounds like you're looking for `last', which is in the Prelude.
Yep, feeling dumb I did not try it ! Thanks, Aaron
http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Prelude.h...
Cheers, -Matt
On Dec 31, 2010, at 3:39 PM, Aaron Gray wrote:
Is there an easy Haskell function that gets the last Char of a [Char] or
String ?
Many thanks in advance,
Aaron
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

http://haskell.org/hoogle/?hoogle=[Char]+-%3E+Char
"last" looks conspicuous :-)
On Fri, Dec 31, 2010 at 1:39 PM, Aaron Gray
Is there an easy Haskell function that gets the last Char of a [Char] or String ? Many thanks in advance, Aaron
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (8)
-
Aaron Gray
-
aditya siram
-
Felipe Almeida Lessa
-
Henning Thielemann
-
Jesse Schalken
-
Luke Palmer
-
Maciej Piechotka
-
Matthew Steele