an erlang monad?

Vlad Dumitrescu vlad_dumitrescu@REDACTED
Fri Mar 4 10:01:09 CET 2005


From: "Matthias Kretschmer" <mccratch@REDACTED>
> > It is important to be able to access the state even somewhere in-between the
> > sequence, and it should be easy to do so too.
> that's very easy with my monad implementation, but of course it has drawbacks:
one always has to give around the monad-type (as with Haskell, where everything
is packed into IO a, ST a or whatever). You can simply read the state anywhere
where you are generating monadic values. This makes it very simple: just read
the state, print it to screen and you're, done.

Yes, I agree. The other solution I was thinking about and that don't fullfill
this requirement (but is much easier to read) is simply using a call chain:
fun_1(...(fun_n(State))...)

> 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, 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?

regards,
Vlad

[*] I don't mean to say that I didn't ever mixed up my variables, or that it
won't happen again! :-)




More information about the erlang-questions mailing list