-
Notifications
You must be signed in to change notification settings - Fork 1
/
TakingAWalk.fr
50 lines (43 loc) · 1.33 KB
/
TakingAWalk.fr
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
module learnyou.chapter14.TakingAWalk where
data Tree a = Empty | Node a (Tree a) (Tree a)
derive Show (Tree a)
freeTree :: Tree Char
freeTree =
Node 'P'
(Node 'O'
(Node 'L'
(Node 'N' Empty Empty)
(Node 'T' Empty Empty)
)
(Node 'Y'
(Node 'S' Empty Empty)
(Node 'A' Empty Empty)
)
)
(Node 'L'
(Node 'W'
(Node 'C' Empty Empty)
(Node 'R' Empty Empty)
)
(Node 'A'
(Node 'A' Empty Empty)
(Node 'C' Empty Empty)
)
)
data Direction = L | R
derive Show Direction
type Directions = [Direction]
changeToP :: Directions -> Tree Char -> Tree Char
changeToP _ Empty = Empty
changeToP [] (Node _ l r) = Node 'P' l r
changeToP (L : ds) (Node x l r) = Node x (changeToP ds l) r
changeToP (R : ds) (Node x l r) = Node x l (changeToP ds r)
-- 'elemAt' is already defined at ArrayElem.elemAt
nodeAt :: Directions -> Tree a -> a
nodeAt _ Empty = error "no element."
nodeAt [] (Node x _ _) = x
nodeAt (L : ds) (Node _ l _) = nodeAt ds l
nodeAt (R : ds) (Node _ _ r) = nodeAt ds r
main _ = do
let newTree = changeToP [R, L] freeTree
println $ nodeAt [R, L] newTree