Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile error on Real World Haskell example

Tags:

haskell

I am writing up a piece of code from "Real World Haskell" :

 ghc --make ch04/InteractWith.hs 
[1 of 1] Compiling Main             ( ch04/InteractWith.hs, ch04/InteractWith.o )

ch04/InteractWith.hs:9:5: parse error on input `args'

dan@dbmint ~/testHaskell $ cat 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"

myFunction = id
like image 856
Dan Billings Avatar asked Aug 26 '26 21:08

Dan Billings


1 Answers

Wrong indentation. The do-block (the args <- getArgs ... part) is at the same level as the start of the mainWith definition.

This compiles without errors:

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"

myFunction = id
like image 177
Mikhail Glushenkov Avatar answered Aug 30 '26 09:08

Mikhail Glushenkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!