List monad 和 bind with list.concat 和 list map?

问题描述 投票:0回答:0

鉴于此列表 monad:

module ListMonad = struct
  type 'a t = 'a list
  let return x = [x]
  let bind m f = List.concat (List.map f m) 
  let (>>=) = bind
  let zero = [] (* neutral element *)
  let (++) = List.append 
end

我知道

f
会返回
'a -> 'b list
f
总是需要返回 monad 中定义的
'b t
?这里例如
'a t = 'a list
,所以对于
f
它必须返回
'b t
,对吗?有时,它在模块内部执行,有时在我们评估时执行。

functional-programming ocaml
© www.soinside.com 2019 - 2024. All rights reserved.