blob: c088ddc06cbcb3ba92c3850d74eb98f507c1b7ce (
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
-- Using the command framework from the earlier section "A simple command line
-- framework" on page 71, write a program that prints the first word of each
-- line of its input.
{-- 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 = firstWords
{-- End of code from examples --}
firstWords' :: [String] -> [String]
firstWords' [] = []
firstWords' (x:xs) = first:(firstWords' xs)
where lineWords = words x
first = if not (null lineWords)
then head lineWords
else ""
firstWords input = unlines (firstWords' (lines input))
-- $ stack ghc 4_a_3.hs
-- [1 of 1] Compiling Main ( 4_a_3.hs, 4_a_3.o )
-- Linking 4_a_3 ...
-- $ cat input.txt
-- ab cde fghi
--
-- j k
--
-- lm
-- $ ./4_a_3 input.txt output.txt
-- $ cat output.txt
-- ab
--
-- j
--
-- lm
|