Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell中带参数的I/O函数的正确类型_Haskell - Fatal编程技术网

Haskell中带参数的I/O函数的正确类型

Haskell中带参数的I/O函数的正确类型,haskell,Haskell,我想实现一个函数,根据性别提出不同的问题。但是我没有给它正确的类型 askDifferentQuestion :: String -> IO String askDifferentQuestion sex = do putStrLn "ciao" main = do sex <- getLine askDifferentQuestion sex 为什么我做错了?putStrLn的类型是String->IO not String->IO String。putStrLn的

我想实现一个函数,根据性别提出不同的问题。但是我没有给它正确的类型

askDifferentQuestion :: String -> IO String
askDifferentQuestion sex = do
  putStrLn "ciao"

main = do
  sex <- getLine
  askDifferentQuestion sex
为什么我做错了?

putStrLn的类型是String->IO not String->IO String。

putStrLn的类型是String->IO not String->IO String。

类型IO String表示运行时生成字符串的输入/输出操作。按原样,askDifferentQuestion会导致,这通常表示一个不重要的值。这是因为要运行的唯一操作是类型为IO的putStrLn,也就是说,您运行它只是为了它的副作用

假设您的类型正确,请更改askDifferentQuestion的定义,以提示用户并返回响应。比如说

askDifferentQuestion :: String -> IO String
askDifferentQuestion sex = putStrLn (q sex) >> getLine
  where q "M" = "What is the airspeed velocity of an unladen swallow?"
        q "F" = "How do you like me now?"
        q "N/A" = "Fuzzy Wuzzy wasn’t fuzzy, was he?"
        q "N" = "Why do fools fall in love?"
        q "Y" = "Dude, where’s my car?"
        q _ = "Why do you park on a driveway and drive on a parkway?"
IO字符串类型表示在运行时生成字符串的输入/输出操作。按原样,askDifferentQuestion会导致,这通常表示一个不重要的值。这是因为要运行的唯一操作是类型为IO的putStrLn,也就是说,您运行它只是为了它的副作用

假设您的类型正确,请更改askDifferentQuestion的定义,以提示用户并返回响应。比如说

askDifferentQuestion :: String -> IO String
askDifferentQuestion sex = putStrLn (q sex) >> getLine
  where q "M" = "What is the airspeed velocity of an unladen swallow?"
        q "F" = "How do you like me now?"
        q "N/A" = "Fuzzy Wuzzy wasn’t fuzzy, was he?"
        q "N" = "Why do fools fall in love?"
        q "Y" = "Dude, where’s my car?"
        q _ = "Why do you park on a driveway and drive on a parkway?"

... 因此,askDifferentQuestion的类型是askDifferentQuestion::String->IO。。。。因此,askDifferentQuestion的类型是askDifferentQuestion::String->IO。在这种情况下,可以做的一件事是省略askDifferentQuestion的类型声明,然后使用:t命令询问ghci它自动推断的类型。在这种情况下,可以做的一件事是省略askDifferentQuestion的类型声明askDifferentQuestion,然后使用:t命令询问ghci自动推断的类型。