an erlang monad?
Matthias Kretschmer
mccratch@REDACTED
Fri Mar 4 12:12:22 CET 2005
On Fri, 4 Mar 2005 10:01:09 +0100
"Vlad Dumitrescu" <vlad_dumitrescu@REDACTED> wrote:
> > Another way to makes this nicer would be to use other combinators that make
> life easier. E.g. something like (forgive the pseudo-code):
> >
> > lift(Fun, [Action0, Action1, ...]) -->
> > apply(Fun, [X0, X1, ...] <- (X0 <- Action0 >>= X1 <- Action1 >>- ...))
>
> Please forgive my ignorance, but do you really think the above is easier to read
> and less error-prone than the "hated"
> A1=fun_1(Start),
> A2=fun_2(A1),
> ...
> A_n=fun_n(A_k),
> ?
Yes I think so, because I don't have to care about the state and state naming. Remember, my X_n's are not the state, they are just values returned by the monad-values while using/changing the state. E.g. if in one step you want to read a value from you dictionary. So if I have a chain of dictionary changing functions which don't return anything I want to bother about, it would be looking like:
lift(fun (_, _, _, _, ...) -> ok end,
[Action0, Action1, Action2, ...]).
you could even reduce it to some lift/1, if you don't care about the possible values carried by the actions and you are just interested in the state itself.
In my humble opinion it is much nicer, because I don't have to care about the state directly as in the sequence given above by you.
>
> Yes, it's easy to mix up the variable names, but the code flow is cleaner,
> uncluttered by plenty of 'fun()->...end's. If I get to mre than three chained
> calls, I usually switch to using more meaningful names than A1, A2 - which takes
> slightly more time (for thinking of a good name and typing it as it's longer
> than 2 chars), but I feel it saves debugging time. [*]
>
> Also, in my code it happens often that I need to do pattern matching - how would
> it be accounted for in such a monadic style?
well in fact using my transformer it is quite easy, simply putting a case statement in the chain. The transformer is very simple and you have to introduce a new monad-statement in each case clause or call a function returning a monad value, so it is not as nice as Haskell's do in that concern, but it is usable.
--
cheer
Matthias Kretschmer
More information about the erlang-questions
mailing list