How does the List monad work in this example?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: The List monad has return x = [x] . So why in the following example is the result not [(["a", "b"], [2, 3])] ? > pairs a b = do { x <- a; y <- b; return (x, y)} > pairs ["a", "b"] [2,3] [("a",2),("a",3),("b",2),("b",3)] 回答1: Let us first analyze and rewrite the function pairs : pairs a b = do { x <- a; y <- b; return (x, y)} Here we thus have a monad. We use do as syntactical sugar. But the compiler rewrites this to: pairs a b = a >>= (\x -> b >>= (\y -> return (x, y))) Or in a more canonical form: pairs a b = (>>=) a (\x -> (>>=) b (\y ->