문제

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