File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ -- Main.hs
2+ module Main where
3+
4+ import qualified Markup
5+ import qualified Html
6+ import Convert (convert )
7+
8+ import System.Directory (doesFileExist )
9+ import System.Environment (getArgs )
10+
11+ main :: IO ()
12+ main = do
13+ args <- getArgs
14+ case args of
15+ -- No program arguments: reading from stdin and writing to stdout
16+ [] -> do
17+ content <- getContents
18+ putStrLn (process " Empty title" content)
19+
20+ -- With input and output file paths as program arguments
21+ [input, output] -> do
22+ content <- readFile input
23+ exists <- doesFileExist output
24+ let
25+ writeResult = writeFile output (process input content)
26+ if exists
27+ then whenIO confirm writeResult
28+ else writeResult
29+
30+ -- Any other kind of program arguments
31+ _ ->
32+ putStrLn " Usage: runghc Main.hs [-- <input-file> <output-file>]"
33+
34+ process :: Html. Title -> String -> String
35+ process title = Html. render . convert title . Markup. parse
36+
37+ confirm :: IO Bool
38+ confirm = do
39+ putStrLn " Are you sure? (y/n)"
40+ answer <- getLine
41+ case answer of
42+ " y" -> pure True
43+ " n" -> pure False
44+ _ -> do
45+ putStrLn " Invalid response. use y or n"
46+ confirm
47+
48+ whenIO :: IO Bool -> IO () -> IO ()
49+ whenIO cond action = do
50+ result <- cond
51+ if result
52+ then action
53+ else pure ()
Original file line number Diff line number Diff line change 1+ * Compiling programs with ghc
2+
3+ Running ghc invokes the Glasgow Haskell Compiler (GHC),
4+ and can be used to compile Haskell modules and programs into native
5+ executables and libraries.
6+
7+ Create a new Haskell source file named hello.hs, and write
8+ the following code in it:
9+
10+ > main = putStrLn "Hello, Haskell!"
11+
12+ Now, we can compile the program by invoking ghc with the file name:
13+
14+ > ➜ ghc hello.hs
15+ > [1 of 1] Compiling Main ( hello.hs, hello.o )
16+ > Linking hello ...
17+
18+ GHC created the following files:
19+
20+ - hello.hi - Haskell interface file
21+ - hello.o - Object file, the output of the compiler before linking
22+ - hello (or hello.exe on Microsoft Windows) - A native runnable executable.
23+
24+ GHC will produce an executable when the source file satisfies both conditions:
25+
26+ # Defines the main function in the source file
27+ # Defines the module name to be Main, or does not have a module declaration
28+
29+ Otherwise, it will only produce the .o and .hi files.
30+
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments