summaryrefslogtreecommitdiff
path: root/Haskell-book/09/src/Exercises.purs
blob: b18cf24a6053262c942c382f453c7eecb9e7a60a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module Exercises where

import Data.Array (filter)
import Data.Char (toLower, toUpper)
import Data.Maybe (Maybe(..))
import Data.String (fromCharArray, toCharArray, uncons)
import Prelude

-- 2
filterUppercase :: String -> String
filterUppercase s = fromCharArray $ filter (\x -> toLower x /= x) (toCharArray s)

-- 3
capitalize :: String -> String
capitalize s = maybeCapitalize $ uncons s
  where maybeCapitalize Nothing = ""
        maybeCapitalize (Just {head, tail}) = (fromCharArray $ [ toUpper head ]) <> tail

-- 4
capitalize' :: String -> String
capitalize' s = fromCharArray $ map toUpper $ toCharArray s

-- 5
firstLetter :: String -> Maybe Char
firstLetter = firstLetter' <<< uncons
  where firstLetter' Nothing = Nothing
        firstLetter' (Just {head, tail}) = Just $ toUpper head