Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call main function with arguments

Tags:

haskell

I have problem with reading from file. Whenever I need to read from a file, I do something like this:

main = do x <- readFile "/tmp/foo.txt"
      putStr x

But now I would like the path to be an argument, so I tried the following

main s = do x <- readFile s
        putStr x

It doesn't work. I see the following error:

Couldn't match expected type `IO t0'
            with actual type `FilePath -> IO ()'
In the expression: main
When checking the type of the function `main'

My homework is to write a program and the program has to contain a main function (because it will be compiled) and argument of call must contain the name of the file. I'm not sure I understand this and I don't know how to proceed. I will grateful for some help.

like image 728
user2533428 Avatar asked Jun 28 '13 22:06

user2533428


People also ask

Can you pass arguments to main function?

Yes, we can give arguments in the main() function. Command line arguments in C are specified after the name of the program in the system's command line, and these argument values are passed on to your program during program execution. The argc and argv are the two arguments that can pass to main function.

When the main function is called it is called with arguments?

A main() function can be called using command line arguments. It is a function that contains two parameters, integer (int argc) and character (char *argv) data type. The argc parameter stands for argument count, and argv stands for argument values.


2 Answers

The Haskell report specifies that the main function always has type IO t, (for some type t which will be ignored) and hence never takes normal function arguments, so this is not the right approach.

You are looking for the function getArgs (for which you have to import the module System.Environment. It returns the arguments passed to your program as a list of Strings.

So your code would look like:

import System.Environment
main = do
    args <- getArgs
    case args of 
      [file] -> do
        x <- readFile file
        putStr x
      _ -> putStrLn "Wrong number of arguments"
like image 90
Joachim Breitner Avatar answered Oct 18 '22 12:10

Joachim Breitner


In Haskell, the arguments are NOT given to the main function because of the way Haskell binds its start up and to remain consistent. You need to use System.Environment.getArgs.

In particular, because Haskell is a pure functional language, main is a monadic action that organizes the side-effect-ful computations performed by the software – the result computed by main is discarded, because in functional languages you are detached from the environment w.r.t. computation and only interact with it as a side-effect.

Example

import System.Environment

main = do x <- getArgs; print x

This will print out whatever you type on the command line.

The Haskell wiki has an excellent tutorial on the topic.

like image 33
Ahmed Masud Avatar answered Oct 18 '22 13:10

Ahmed Masud