Haskell将多行代码视为单个函数调用

3 haskell

pure :: String -> ()
pure x = unsafePerformIO $ do
  print x
  return ()

pureCall :: String -> IO ()
pureCall x = do
  pure x
  putStrLn "inside child function"
Run Code Online (Sandbox Code Playgroud)

这会抛出编译错误,

  The function ‘pure’ is applied to three arguments,
    but its type ‘String -> ()’ has only one
Run Code Online (Sandbox Code Playgroud)

我在其他语言中使用分号作为代码行分隔符.但不确定,我怎么能在haskell中做到这一点,并将pureCall功能块作为两个单独的语句运行!!

xny*_*hps 7

确保你以相同的方式缩进pureputStrLn行(所以不要使用制表符,而另一个使用空格).如果ghc认为putStrLn进一步缩进,那么它将被视为参数pure.

如果你愿意,你也可以在Haskell中使用分号:

pureCall :: String -> IO ()
pureCall x = do {
  pure x ;
      putStrLn "inside child function"
  }
Run Code Online (Sandbox Code Playgroud)

按照你的期望工作.

(另请注意,您的代码输入pure不正确:不是类型IO a.)