forked from ehamberg/flatmap-haskell-workshop
/
E10_QuickCheckExamples.hs
47 lines (38 loc) · 1.33 KB
/
E10_QuickCheckExamples.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
module E10_QuickCheckExamples where
import Common
{-
QuickCheck can be used for property-based testing.
A common example is that a list of something twice reversed should equal the original list.
This could be written as:
-}
reverseProperty :: String -> Bool
reverseProperty xs = xs == reverse (reverse xs)
{-
We could also use function composition:
-}
reverseProperty' xs = xs == (reverse . reverse) xs
{-
Lossless encoding/compression is also a group of suitable problems for QuickCheck,
as the decoded output of an encoded <something> should always equal the original input.
-}
{-
Excercise 1:
Run length encoding
Transform a list of comparable elements into a list of tuples (n, e) where n is the number
of consecutive elements, and e is the element.
Example:
encode "Heeeello Haskell!!!"
[(1,'H'),(4,'e'),(2,'l'),(1,'o'),(1,' '),(1,'H'),(1,'a'),(1,'s'),(1,'k'),(1,'e'),(2,'l'),(3,'!')]
-}
encode :: String -> [(Int, Char)]
encode xs = _YOUR_CODE_HERE
{-
Excercise 2:
Run length decoding
Transform the run length encoded representation back to the original list of elements.
Example:
decode [(1,'H'),(4,'e'),(2,'l'),(1,'o'),(1,' '),(1,'H'),(1,'a'),(1,'s'),(1,'k'),(1,'e'),(2,'l'),(3,'!')]
"Heeeello Haskell!!!"
-}
decode :: [(Int, Char)] -> String
decode xs = _YOUR_CODE_HERE