reader-monad

Haskell - depth for each node in binary tree using Reader monad

ぃ、小莉子 提交于 2019-11-27 09:44:55
I wrote the following code. It is working and using the Reader monad. Could you give me some hints about code style in Haskell ? Mainly, I mean monads -- I am newbie. import Control.Monad.Reader data Tree a = Node a (Tree a) (Tree a) | Empty renumberM :: Tree a -> Reader Int (Tree Int) renumberM (Node _ l r) = ask >>= (\x -> return (Node x (runReader (local (+1) (renumberM l)) x) (runReader (local (+1) (renumberM r)) x))) renumberM Empty = return Empty renumber'' :: Tree a -> Tree Int renumber'' t = runReader (renumberM t) 0 I want to show you that your idea is an instance of a more general

What is the purpose of the reader monad?

流过昼夜 提交于 2019-11-27 02:36:19
The reader monad is so complex and seems to be useless. In an imperative language like Java or C++, there is no equivalent concept for the reader monad, if I am not mistaken. Can you give me a simple example and clear this up a little bit? Philip JF Don't be scared! The reader monad is actually not so complicated, and has real easy-to-use utility. There are two ways of approaching a monad: we can ask What does the monad do ? What operations is it equipped with? What is it good for? How is the monad implemented? From where does it arise? From the first approach, the reader monad is some

Haskell - depth for each node in binary tree using Reader monad

杀马特。学长 韩版系。学妹 提交于 2019-11-26 17:52:18
问题 I wrote the following code. It is working and using the Reader monad. Could you give me some hints about code style in Haskell ? Mainly, I mean monads -- I am newbie. import Control.Monad.Reader data Tree a = Node a (Tree a) (Tree a) | Empty renumberM :: Tree a -> Reader Int (Tree Int) renumberM (Node _ l r) = ask >>= (\x -> return (Node x (runReader (local (+1) (renumberM l)) x) (runReader (local (+1) (renumberM r)) x))) renumberM Empty = return Empty renumber'' :: Tree a -> Tree Int

What is the purpose of the reader monad?

我怕爱的太早我们不能终老 提交于 2019-11-26 12:34:37
问题 The reader monad is so complex and seems to be useless. In an imperative language like Java or C++, there is no equivalent concept for the reader monad, if I am not mistaken. Can you give me a simple example and clear this up a little bit? 回答1: Don't be scared! The reader monad is actually not so complicated, and has real easy-to-use utility. There are two ways of approaching a monad: we can ask What does the monad do ? What operations is it equipped with? What is it good for? How is the