aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--ch04/4_a_4.hs58
2 files changed, 59 insertions, 1 deletions
diff --git a/README.md b/README.md
index 269e404..3f1ecff 100644
--- a/README.md
+++ b/README.md
@@ -71,7 +71,7 @@ more visible in the list the first exercise of a group is in bold italics.
| **_4_a_1_** | yes | 84 | 4. Functional programming |
| 4_a_2 | yes | | |
| 4_a_3 | yes | | |
-| 4_a_4 | | | |
+| 4_a_4 | yes | | |
| **_4_b_1_** | | 97 | |
| 4_b_2 | | 98 | |
| 4_b_3 | | | |
diff --git a/ch04/4_a_4.hs b/ch04/4_a_4.hs
new file mode 100644
index 0000000..1a66d45
--- /dev/null
+++ b/ch04/4_a_4.hs
@@ -0,0 +1,58 @@
+-- Write a program that transposes the text in a file. For instance, it should
+-- convert "hello\nworld\n" to "hw\neo\nlr\nll\nod\n".
+
+{-- From examples/examples/ch04/InteractWith.hs --}
+import System.Environment (getArgs)
+
+interactWith function inputFile outputFile = do
+ input <- readFile inputFile
+ writeFile outputFile (function input)
+
+main = mainWith myFunction
+ where mainWith function = do
+ args <- getArgs
+ case args of
+ [input,output] -> interactWith function input output
+ _ -> putStrLn "error: exactly two arguments needed"
+
+ -- replace "id" with the name of our function below
+ myFunction = transpose
+{-- End of code from examples --}
+
+-- The assignment doesn't mention that the implementation should handle rows of
+-- different lengths, and the example input contains only rows of the same
+-- length. Thus, for simplicity, we assume that all rows of the input have the
+-- same length.
+
+firstColumn :: [String] -> String
+firstColumn [] = ""
+firstColumn (x:xs)
+ | null x = ""
+ | otherwise = (head x):(firstColumn xs)
+
+removeFirstColumn :: [String] -> [String]
+removeFirstColumn [] = []
+removeFirstColumn (x:xs) = (tail x):(removeFirstColumn xs)
+
+transpose' :: [String] -> [String]
+transpose' rows = if null column
+ then []
+ else column:(transpose' rest)
+ where column = firstColumn rows
+ rest = removeFirstColumn rows
+
+transpose input = unlines (transpose' (lines input))
+
+-- $ stack ghc 4_a_4.hs
+-- [1 of 1] Compiling Main ( 4_a_4.hs, 4_a_4.o )
+-- Linking 4_a_4 ...
+-- $ cat input.txt
+-- hello
+-- world
+-- $ ./4_a_4 input.txt output.txt
+-- $ cat output.txt
+-- hw
+-- eo
+-- lr
+-- ll
+-- od