summaryrefslogtreecommitdiff
path: root/Haskell-book/11/As.hs
blob: 744a5fe7e40b3b74fbb97712c09f74d434955dbe (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
28
29
30
module As where

import Data.Char

isSubseqOf :: (Eq a) => [a] -> [a] -> Bool
isSubseqOf [] _ = True
isSubseqOf _ [] = False
isSubseqOf a@(x:xs) (y:ys)
  | x == y    = isSubseqOf xs ys
  | otherwise = isSubseqOf a ys

capitalize :: Char -> Char
capitalize x
  | (ord x) >= (ord 'a') = (chr ((ord x) - 32))
  | otherwise            = x

capitalizeWords :: String -> [(String, String)]
capitalizeWords text = map f (words text)
    where f w@(x:xs) = (w, (capitalize x) : xs)

capitalizeWord :: String -> String
capitalizeWord (x:xs) = (capitalize x) : xs

capitalizeParagraph :: String -> String
capitalizeParagraph = it True
    where it _ [] = []
          it _ ('.':xs) = '.' : (it True xs)
          it y (x:xs)
            | (ord x) >= (ord 'a') && (ord x) < (ord 'z') && y = (capitalize x) : (it False xs)
            | otherwise                                        = x              : (it y xs)