aboutsummaryrefslogtreecommitdiff
path: root/ch09/9_b_2.hs
blob: 990dc838368b987dfdf51ef44387e342d0783a8d (plain)
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
-- Using 'id' as a control function, 'traverse id' performs a preorder traversal of
-- a tree: it returns a parent directory before its children. Write a control
-- function that makes 'traverse' perform a postorder traversal, in which it
-- returns children before their parent.

-- ghci> :l ControlledVisit.hs
-- [1 of 1] Compiling ControlledVisit  ( ControlledVisit.hs, interpreted )
-- Ok, one module loaded.

-- Output of the following commands is manually shortened and formatted for clarity
-- ghci> traverse' id "test-9_b_1"
-- [Info {infoPath = "test-9_b_1", ...},
--  Info {infoPath = "test-9_b_1/dirC", ...},
--  Info {infoPath = "test-9_b_1/dirC/F", ...},
--  Info {infoPath = "test-9_b_1/dirC/E", ...},
--  Info {infoPath = "test-9_b_1/A", ...},
--  Info {infoPath = "test-9_b_1/B", ...},
--  Info {infoPath = "test-9_b_1/dirD", ...},
--  Info {infoPath = "test-9_b_1/dirD/G", ...},
--  Info {infoPath = "test-9_b_1/dirD/H", ...}]

-- ghci> traverse' (\list -> (tail list) ++ [(head list)]) "test-9_b_1"
-- [Info {infoPath = "test-9_b_1/dirC/F", ...},
--  Info {infoPath = "test-9_b_1/dirC/E", ...},
--  Info {infoPath = "test-9_b_1/dirC", ...},
--  Info {infoPath = "test-9_b_1/A", ...},
--  Info {infoPath = "test-9_b_1/B", ...},
--  Info {infoPath = "test-9_b_1/dirD/G", ...},
--  Info {infoPath = "test-9_b_1/dirD/H", ...},
--  Info {infoPath = "test-9_b_1/dirD", ...},
--  Info {infoPath = "test-9_b_1", ...}]