0

I'm trying to get files as arguments like this:

main = do  
 (file1:file2:file3:_) <- getArgs
 checkdata
 command <- getLine
 runQuery(words command)

The problem is this runQuery(words command) is not recognizing those arguments.

runQuery ("queryname":parameter1:parameter2) = do

 myfile1 <- readFile file1
 myfile2 <- readFile file2
 myfile3 <- readFile file3

The error I'm getting is:

Not in scope: file1
....

How can I pass them to functions like I intended to? Please help.

3
  • I have no idea what that code is supposed to do whatsoever. Commented Aug 27, 2014 at 22:36
  • 2
    The question seems quite clear to me. It's true that the standard "best practice" for asking questions wasn't followed, but it seems enough information is available to give a good answer. To the question-asker: in the future, one should give a complete, minimal code sample (so that readers can play along at home) together with an exact copy of the compiler error (so that readers can focus on problems they know how to solve). Commented Aug 27, 2014 at 22:43
  • Thanks for feedback Daniel, I'm completely new to this site (and haskell) but I'll keep that in mind. Cheers. Commented Aug 27, 2014 at 23:45

2 Answers 2

2

You have to pass file1 etc. to runQuery like every other function argument:

main = do  
 (file1:file2:file3:_) <- getArgs
 checkdata
 command <- getLine
 runQuery file1 file2 file3 (words command)

runQuery file1 file2 file3 ("queryname":parameter1:parameter2) = do
 ...
Sign up to request clarification or add additional context in comments.

1 Comment

Well that was simple. Thanks.
1

In Haskell, function arguments are separated simply by spaces, so if you had a function defined as

runQuery queryName param1 param2 = <implementation>

You would have a function of three arguments called runQuery with the arguments queryName, param1, and param2. You would then pass in arguments in the same syntax:

main = do
    (name:param1:_) <- getArgs
    param2 <- getLine
    runQuery name param1 param2

Here we are calling the function runQuery with the arguments name, param1, and param2, which were obtained from getArgs and getLine.

Note that the : character is an operator, it has nothing to do with function call syntax, and its purpose is the construct a new list by prepending an element to the front of an existing list. Since it is a constructor as well, it can be used for pattern matching, hence its use in (name:param1:_) <- getArgs. The _ is a wildcard pattern that matches anything, so it is taking the place of "the rest of the args passed in at the command line".

You seem to also be confused about scoping in Haskell. I would highly recommend you read some tutorials on beginning Haskell, my favorite is Learn You a Haskell For Great Good, to become more familiar with the basic syntax and language rules of the language before attempting more complicated programs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.