icyrock.com

Home

PureScript experiments - daily programmer challenge #7 easy

2017-May-30 20:31
purescript-experimentspurescript

Solution to Daily programmer challenge #7 easy.

Morse code table courtesy of thofmann's JavafMorse Decoder.

The code is pretty self-explanatory:

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
module C7E where
 
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Data.Either (Either(..))
import Data.Generic (class Generic, gShow)
import Data.String (Pattern(..), fromCharArray, joinWith, split, toCharArray)
import Data.Traversable (traverse)
 
parseWord :: String -> Array String
parseWord = split (Pattern " ")
 
parse :: String -> Array (Array String)
parse s =
  let words = split (Pattern " / ") s
  in map parseWord words
 
decodeLetter :: String -> Either String Char
decodeLetter ".-"     = Right 'A'
decodeLetter "-..."   = Right 'B'
decodeLetter "-.-."   = Right 'C'
decodeLetter "-.."    = Right 'D'
decodeLetter "."      = Right 'E'
decodeLetter "..-."   = Right 'F'
decodeLetter "--."    = Right 'G'
decodeLetter "...."   = Right 'H'
decodeLetter ".."     = Right 'I'
decodeLetter ".---"   = Right 'J'
decodeLetter "-.-"    = Right 'K'
decodeLetter ".-.."   = Right 'L'
decodeLetter "--"     = Right 'M'
decodeLetter "-."     = Right 'N'
decodeLetter "---"    = Right 'O'
decodeLetter ".--."   = Right 'P'
decodeLetter "--.-"   = Right 'Q'
decodeLetter ".-."    = Right 'R'
decodeLetter "..."    = Right 'S'
decodeLetter "-"      = Right 'T'
decodeLetter "..-"    = Right 'U'
decodeLetter "...-"   = Right 'V'
decodeLetter ".--"    = Right 'W'
decodeLetter "-..-"   = Right 'X'
decodeLetter "-.--"   = Right 'Y'
decodeLetter "--.."   = Right 'Z'
decodeLetter "-----"  = Right '0'
decodeLetter ".----"  = Right '1'
decodeLetter "..---"  = Right '2'
decodeLetter "...--"  = Right '3'
decodeLetter "....-"  = Right '4'
decodeLetter "....."  = Right '5'
decodeLetter "-...."  = Right '6'
decodeLetter "--..."  = Right '7'
decodeLetter "---.."  = Right '8'
decodeLetter "----."  = Right '9'
decodeLetter ".-.-.-" = Right '.'
decodeLetter "--..--" = Right ','
decodeLetter "-..-."  = Right '/'
decodeLetter "..--.." = Right '?'
decodeLetter s        = Left $ "Unrecognized Morse sequence " <> s
 
decodeWord :: Array String -> Either String String
decodeWord = traverse decodeLetter
         >>> map fromCharArray
 
decodeWords :: Array (Array String) -> Either String String
decodeWords = traverse decodeWord
          >>> map (joinWith " ")
 
encodeLetter :: Char -> Either String String
encodeLetter 'A' = Right ".-"
encodeLetter 'B' = Right "-..."
encodeLetter 'C' = Right "-.-."
encodeLetter 'D' = Right "-.."
encodeLetter 'E' = Right "."
encodeLetter 'F' = Right "..-."
encodeLetter 'G' = Right "--."
encodeLetter 'H' = Right "...."
encodeLetter 'I' = Right ".."
encodeLetter 'J' = Right ".---"
encodeLetter 'K' = Right "-.-"
encodeLetter 'L' = Right ".-.."
encodeLetter 'M' = Right "--"
encodeLetter 'N' = Right "-."
encodeLetter 'O' = Right "---"
encodeLetter 'P' = Right ".--."
encodeLetter 'Q' = Right "--.-"
encodeLetter 'R' = Right ".-."
encodeLetter 'S' = Right "..."
encodeLetter 'T' = Right "-"
encodeLetter 'U' = Right "..-"
encodeLetter 'V' = Right "...-"
encodeLetter 'W' = Right ".--"
encodeLetter 'X' = Right "-..-"
encodeLetter 'Y' = Right "-.--"
encodeLetter 'Z' = Right "--.."
encodeLetter '0' = Right "-----"
encodeLetter '1' = Right ".----"
encodeLetter '2' = Right "..---"
encodeLetter '3' = Right "...--"
encodeLetter '4' = Right "....-"
encodeLetter '5' = Right "....."
encodeLetter '6' = Right "-...."
encodeLetter '7' = Right "--..."
encodeLetter '8' = Right "---.."
encodeLetter '9' = Right "----."
encodeLetter '.' = Right ".-.-.-"
encodeLetter ',' = Right "--..--"
encodeLetter '/' = Right "-..-."
encodeLetter '?' = Right "..--.."
encodeLetter c   = Left $ "Do not know how to encode char " <> show c
 
encodeWord :: String -> Either String String
encodeWord = toCharArray
         >>> traverse encodeLetter
         >>> map (joinWith " ")
 
encodeWords :: String -> Either String String
encodeWords = split (Pattern " ")
          >>> traverse encodeWord
          >>> map (joinWith " / ")
 
newtype Result = Result { decoded :: String
                        , encoded :: String
                        , equal :: Boolean
                        , original :: String
                        }
 
derive instance genericResult :: Generic Result
 
instance showResult :: Show Result where
  show = gShow
   
main :: forall eff. Eff (console :: CONSOLE | eff) Unit
main = do
  let original = ".... . .-.. .-.. --- / -.. .- .. .-.. -.-- / .--. .-. --- --. .-. .- -- -- . .-. / --. --- --- -.. / .-.. ..- -.-. -.- / --- -. / - .... . / -.-. .... .- .-.. .-.. . -. --. . ... / - --- -.. .- -.--"
  let pieces = parse original
  logShow do
    decoded <- decodeWords pieces
    encoded <- encodeWords decoded
    let equal = original == encoded
    pure (Result { decoded, encoded, equal, original })

Result:

1
2
3
4
5
6
7
8
9
10
11
12
$ pulp psci
import Compiling C7E
CPSCi, version 0.11.1
Type :? for help
 
import Prelude
> import C7E
> main
(Right C7E.Result {decoded: "HELLO DAILY PROGRAMMER GOOD LUCK ON THE CHALLENGES TODAY", encoded: ".... . .-.. .-.. --- / -.. .- .. .-.. -.-- / .--. .-. --- --. .-. .- -- -- . .-. / --. --- --- -.. / .-.. ..- -.-. -.- / --- -. / - .... . / -.-. .... .- .-.. .-.. . -. --. . ... / - --- -.. .- -.--", equal: true, original: ".... . .-.. .-.. --- / -.. .- .. .-.. -.-- / .--. .-. --- --. .-. .- -- -- . .-. / --. --- --- -.. / .-.. ..- -.-. -.- / --- -. / - .... . / -.-. .... .- .-.. .-.. . -. --. . ... / - --- -.. .- -.--"})
unit
 
>