FabolousPotato
FabolousPotato

Reputation: 65

How to fix ''couldn't match expected type IO a0 with int'' in Haskell?

The line checkArgCount args gives me the trouble. args is a type [String] isn't it? I'm confused over why it gives me error.

import System.IO
import Data.List
import System.Environment --for command line args

--Checks if number of arguments provided is 2
checkArgCount ::[String] -> Int
checkArgCount a = if (length a) == 2 then 2 else error "No arguments" 

main :: IO()
main = do
     args <- getArgs
     checkArgCount args

Upvotes: 1

Views: 351

Answers (3)

FabolousPotato
FabolousPotato

Reputation: 65

args <- getArgs
     if (length args) /= 2
        then return()
        else do -- ..continue coding

Proved to be far simpler solution

Upvotes: 1

Will Ness
Will Ness

Reputation: 71075

Yes, args :: [String], but checkArgCount args must have type IO (), being the last line in the do block with the overall type IO ().

So it's about the output type, not the input type of that function.

Changing 2 to print 2 in the if's consequent should fix it (and of course changing the type signature of checkArgCount to fit the change).

Upvotes: 3

Mark Seemann
Mark Seemann

Reputation: 233170

main is declared to have the type IO () (which is correct). The last expression in any function is the return value. This particular code returns the result of checkArgCount args, which is Int.

An Int value isn't an IO () value, so that doesn't type check.

You should either change the type of checkArgCount to return () or IO (), or add more code to main that uses the Int returned by checkArgCount.

Upvotes: 3

Related Questions