以下程序有什么问题?

Luk*_*uke 0 haskell

这段代码有什么问题?

module Main where
import System.Environment
main :: IO ()
main = do
    args <- getArgs
    putStrLn ("Hello, " ++ args !! 0 ++ ", " ++ args !! 1)
    putStrLn(add (read args !! 0) (read args !! 1))
add x y = x + y
Run Code Online (Sandbox Code Playgroud)

以下是错误消息:

main.hs:8:15:
    No instance for (Num String)
      arising from a use of `add'
    Possible fix: add an instance declaration for (Num String)
    In the first argument of `putStrLn', namely
      `(add (read args !! 0) (read args !! 1))'
    In the expression: putStrLn (add (read args !! 0) (read args !! 1))
    In the expression:
      do { args <- getArgs;
           putStrLn ("Hello, " ++ args !! 0 ++ ", " ++ args !! 1);
           putStrLn (add (read args !! 0) (read args !! 1)) }

main.hs:8:25:
    Couldn't match expected type `Char' with actual type `[Char]'
    Expected type: String
      Actual type: [String]
    In the first argument of `read', namely `args'
    In the first argument of `(!!)', namely `read args'
Run Code Online (Sandbox Code Playgroud)

Tar*_*sch 8

read args !! 0应该read (args !! 0)而且add x y = x +应该是add x y = x + y.也putStrLn只使用一个字符串,所以请使用print它来打印数字.


但是,看到你是哈斯凯尔的新手.我重写了你的程序的一部分,以展示一种更加漂亮的方式.

main = do
    (arg0:arg1:restArgs) <- getArgs
    putStrLn $ "Hello, " ++ arg0 ++ ", " ++ arg1
    print $ add (read arg0) (read arg1)
add = (+)
Run Code Online (Sandbox Code Playgroud)

我觉得现在看起来有点干净了.请注意,它通常被认为是不好的做法!!.