aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Sucan <jan@jansucan.com>2023-09-20 15:57:37 +0200
committerJan Sucan <jan@jansucan.com>2023-09-20 15:57:37 +0200
commit4995d07fdc713077b595a5a09f6cbe427b919bf1 (patch)
tree3a12f5e548cc2382fe41078762d7d55dd3d5a4dc
parent6bfd58d65e1f407fb04d4e899b69642e140be590 (diff)
9_b_2: Add solution
-rw-r--r--README.md2
-rw-r--r--ch09/9_b_2.hs31
2 files changed, 32 insertions, 1 deletions
diff --git a/README.md b/README.md
index 6c0f579..c5d1a8e 100644
--- a/README.md
+++ b/README.md
@@ -126,7 +126,7 @@ are prefixed with 'Module_'.
| **_8_d_1_** | yes | 212 | |
| **_9_a_1_** | yes | 221 | 9. I/O case study: a library for searching the filesystem |
| **_9_b_1_** | yes | 228 | |
-| 9_b_2 | | | |
+| 9_b_2 | yes | | |
| 9_b_3 | | | |
| 9_b_4 | | | |
| **_9_c_1_** | | 232 | |
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", ...}]