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)
有帮助吗?

解决方案

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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top