[erlang-questions] chained functions

Robert Raschke <>
Wed Jan 25 10:35:02 CET 2012


On Tue, Jan 24, 2012 at 7:31 PM, Reynaldo Baquerizo
<>wrote:

>
> A friend of mine asked:
>
> ##
> If you have functions that return {ok, Result} | {error, Reason}
> how do you chained them? So that you have:
>
> w(x(y(z(...))))
>
> without building a staircasing. Something that would be done in Haskell
> with monads.
> ##
>
> I would probably go for:
>
> x({ok, Value}) ->
>  NewValue = <do something with Value>,
>  {ok, NewValue};
> x({error, Reason}) ->
>  {error, Reason}.
>
> in each function
>
> which brings me other question, when do you tag return values?
>
> I tend to only use tagged return values with impure functions, were an
> error is more likely due to side effects.
>
> --
> Reynaldo
>
>
You'll find that pure functions (those without side effects) will rarely
return {ok, Value} tuples. You get those usually because the function you
are calling is interacting with something stateful. In languages like Java
you end up getting gross misuse of exceptions instead of tagged tuple
results. I know what I prefer. ;-)

Robby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120125/69e28357/attachment.html>


More information about the erlang-questions mailing list