-
Notifications
You must be signed in to change notification settings - Fork 0
/
Play.hs
45 lines (35 loc) · 1.01 KB
/
Play.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
module Play ( isPalindrome ) where
import Data.Char ( isPunctuation, toLower, isAlpha )
isPalindrome :: String -> Maybe Bool
isPalindrome =
reverseMaybe . rejectEmpty . normalize
tReverse :: [Char] -> Bool
tReverse t =
t == reverse t
rejectEmpty :: [Char] -> Maybe [Char]
rejectEmpty str =
case str of
[] -> Nothing
_ -> Just str
normalize :: [Char] -> [Char]
normalize = filter tPunctuation . filter noSpace . tLowerCase
-- Maybe a = Nothing | Just a --
reverseMaybe :: Maybe String -> Maybe Bool
reverseMaybe word =
case word of
Nothing -> Nothing
Just w -> Just (tReverse w)
tLowerCase :: [Char] -> [Char]
tLowerCase = map toLower
noSpace :: Char -> Bool
noSpace t = not (t == ' ')
tPunctuation :: Char -> Bool
tPunctuation x = not (isPunctuation x)
-- Ignore nonAlphabetics
-- Use foldr and all and lambda function
-- Learn Haskell thunks
ignoreNonAlpha :: String -> Maybe String
ignoreNonAlpha x =
if all isAlpha x
then Just x
else Nothing