caesar.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
module Caesar(caesar, decode) where

import Data.List.Split
import Data.Set (Set, member, fromList)
import Data.List
import Data.Char

caesar :: Int -> String -> String
caesar n = map (rotateChar n)


rotateChar :: Int -> Char -> Char
rotateChar n c
    | isLower c = rotateFrom 'a'
    | isUpper c = rotateFrom 'A' 
    | otherwise = c
    where rotateFrom start = toEnum $ (cVal - startVal + n) `mod` 26 + startVal
                           where cVal = fromEnum c
                                 startVal = fromEnum start

capitalize = map toUpper

getWords = words . filter (\c -> isAlpha c || isSpace c)

countRealWords str dict =
    length . filter isRealWord . getWords $ str  
        where isRealWord x = member (capitalize x) dict 

everyCaesar str = [caesar n str | n <- [1..26]]

decode :: Set String -> String -> String
decode dict  = minimumBy comp . everyCaesar
    where comp a b = (countRealWords b dict) `compare` (countRealWords a dict)
33:22: Warning: Redundant bracket
Found:
(countRealWords b dict) `compare` (countRealWords a dict)
Why not:
countRealWords b dict `compare` (countRealWords a dict)
33:22: Warning: Redundant bracket
Found:
(countRealWords b dict) `compare` (countRealWords a dict)
Why not:
(countRealWords b dict) `compare` countRealWords a dict