From 4995d07fdc713077b595a5a09f6cbe427b919bf1 Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Wed, 20 Sep 2023 15:57:37 +0200 Subject: 9_b_2: Add solution --- ch09/9_b_2.hs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 ch09/9_b_2.hs (limited to 'ch09/9_b_2.hs') diff --git a/ch09/9_b_2.hs b/ch09/9_b_2.hs new file mode 100644 index 0000000..990dc83 --- /dev/null +++ b/ch09/9_b_2.hs @@ -0,0 +1,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", ...}] -- cgit v1.2.3