gpt4 book ai didi

haskell - 为什么 GHC 对于 `IO b` 的应用程序推断类型为 `hSetBuffering` ?

转载 作者:行者123 更新时间:2023-12-02 12:03:58 27 4
gpt4 key购买 nike

我正在尝试在使用 GHC 6.10 的 Haskell 程序中使用 hSetBuffering。当我尝试这个非常简单的程序时:

module Ctlc
where

import IO

main :: ()
main = do hSetBuffering stdout NoBuffering
return ()

我收到一条令人费解的错误消息:

ctlc.hs:8:10:
Couldn't match expected type `()' against inferred type `IO b'
In a stmt of a 'do' expression: hSetBuffering stdout NoBuffering
In the expression:
do hSetBuffering stdout NoBuffering
return ()
In the definition of `main':
main = do hSetBuffering stdout NoBuffering
return ()

我不明白为什么 GHC 会推断出一种 IO b,因为 ghci 声称

Prelude Data.List IO> :t hSetBuffering
hSetBuffering :: Handle -> BufferMode -> IO ()

答案:我愚蠢地在main上放置了错误的类型。谢谢ja敏锐的眼睛。

最佳答案

您已将 main 声明为 () 类型,而不是 IO () 类型。

关于haskell - 为什么 GHC 对于 `IO b` 的应用程序推断类型为 `hSetBuffering` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2349922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com