gpt4 book ai didi

Haskell - 如何在 where 子句中使用 <-

转载 作者:行者123 更新时间:2023-12-03 15:04:43 25 4
gpt4 key购买 nike

我有以下代码:

foo :: Int -> IO ()
foo n = do
x <- bar 6
print "foo running..."
print x


bar :: Int -> IO Int
bar n = do
print "bar running..."
return (n*2)

现在我想将 "x <- bar 6"部分放在 where 子句中,如下所示:
foo :: Int -> IO ()
foo n = do
print "foo running..."
print x
where
x <- bar 6

bar :: Int -> IO Int
bar n = do
print "bar running..."
return (n*2)

我该怎么做呢?

最佳答案

这是不允许的。一个 where子句不强加评估顺序,这对于大多数 Monad 来说都是必需的,例如 IO .如果这是可能的,bar 6 什么时候可以?执行相对于两个print年代?是在一开始还是介于两者之间?

关于Haskell - 如何在 where 子句中使用 <-,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26962127/

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