Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Haskell

Idiom #340 Last character of string

Assign to c the value of the last character of the string s.

Explain the type of c, and what happens if s is empty.

Make sure to properly handle multi-bytes characters.

lastChar :: String -> Char
lastChar s
    | s=="" = ""
    | otherwise = s !! (length s - 1)
char c = s[^1];

New implementation...
< >
programming-idioms.org