gpt4 book ai didi

Haskell Monadic 形式

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

一个简单的问题:给定定义,(来自 Haskell SOE)

   do x — el; el\ ...; en 
=> el »= \x — do e2\ ...; en

和:

do let decllist; el\...; en 
=> let decllist in do e2\ ...; en

这两个结构似乎是相同的:

do let x = e1
e2

do x <- e1
e2

两者都评估 e1,将其绑定(bind)到 e2,然后评估 e2。

是吗?

最佳答案

让我们在 Maybe monad 中做一个简单的例子:

foo = do
let x = Just 1
return x

bar = do
x <- Just 1
return x

将两者脱糖,我们得到

foo = let x = Just 1 in return x    -- do notation desugaring
= return (Just 1) -- let
= Just (Just 1) -- definition of return for the Maybe monad

bar = let ok x = return x in Just 1 >>= ok -- do notation desugaring
= let ok x = return x in ok 1 -- definition of >>= for the Maybe monad
= return 1 -- definiton of ok
= Just 1 -- definition of return for the Maybe monad

作为引用,我正在使用 translation from section 3.14 of the Haskell 2010 Report .

关于Haskell Monadic 形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6599415/

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