Question

This code worked fine when it only took first. I get this error after having added second.

Couldn't match expected type 'Char' with actual type 'String'
Expected type: [Char]
Actual type: [String]
In the first argument of `(++)', namely `first'
In the second argument of `(++)', namely `first ++ " " ++ last !! 0'

Which makes no sense to me because getArg is suppose to produce a list of string.

module Main where
import System.Environment

main :: IO ()
main = do
first <- getArgs
last <- getArgs
putStrLn ("Hello" ++ first ++ last !! 0)
Was it helpful?

Solution

getArgs returns a list of string, not a string. You could use it like this:

module Main where
import System.Environment

main :: IO ()
main = do
    args <- getArgs
    putStrLn ("Hello" ++ (args !! 0) ++ (args !! 1))

Of course, this is only a demonstration of how to use getArgs. In real program, you need to make sure you are given enough arguments before you actually use them.

OTHER TIPS

You're trying to concatenate "Hello" which is a String to first which is a list of Strings.

I also don't understand why you're using getArgs twice. first == last.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top