-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch13ex.hs
57 lines (44 loc) · 1.37 KB
/
ch13ex.hs
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Main where
import Control.Monad (forever)
import Data.Char (toLower, isAlphaNum)
import System.Exit (exitSuccess)
palindrome :: IO ()
palindrome = forever $ do
line1 <- getLine
case isPalindrome line1 of
True -> putStrLn "palindrome"
False -> do
putStrLn "Nope"
exitSuccess
isPalindrome :: String -> Bool
isPalindrome str =
let filteredStr = (filter isAlphaNum) . (filter (/= ' ')) . (map toLower) $ str
in filteredStr == reverse filteredStr
main :: IO ()
main = palindrome
type Name = String
type Age = Integer
data Person = Person Name Age deriving Show
data PersonInvalid = NameEmpty
| AgeTooLow
| PersonInvalidUnknown String
deriving (Eq, Show)
mkPerson :: Name -> Age -> Either PersonInvalid Person
mkPerson name age
| name /= "" && age > 0 = Right $ Person name age
| name == "" = Left NameEmpty
| not (age > 0) = Left AgeTooLow
| otherwise = Left $ PersonInvalidUnknown $
"Name was: " ++ show name ++
" Age was: " ++ show age
gimmePerson :: IO ()
gimmePerson = do
putStr "Gimme a name: "
name <- getLine
putStr "Gimme an age: "
ageStr <- getLine
let age = read ageStr
in case mkPerson name age of
(Right person) -> putStrLn $ "YAY " ++ show person
(Left err) -> putStrLn $ "Error " ++ show err
return ()