4
$\begingroup$

I just read this blog post which argues that monads might be too obscure or difficult to understand as the default "interface to the impure world" in purely functional programming languages; instead, it suggests that the actor model would be more comprehensible when it comes to pedagogy. That may be true -- I do think that actors are easier to understand than monads -- but the post leaves a lot to be desired. In particular, the author offers no examples of what this sort of actor-based purely-functional language would look like, and he doesn't seem to have followed up on the blog post since it was written a few years ago.

So my question is: in what way can actors be leveraged to capture side-effects in a purely functional way? How would source code in that hypothetical language look? What advantages/disadvantages would that approach offer in comparison to monads (outside of the cosmetic differences)?

$\endgroup$
1
$\begingroup$

Erlang is an example. I actually don't know Erlang, so I'm going to use some pseudocode:

Suppose you have two threads, Alice and Bob. They talk by calling each other's Send member route, which blocks until the other end replies. Alice can keep some "mutable state" for a single integer as follows:

Alice:
  function main_loop(state):
     message = Receive()
     if message == "get":
        Reply(state)
     else
        Reply("Okay")
        main_loop(message)
  main_loop(0)

Bob:
  Alice.Send(100) // Store 100 into the mutable state
  Alice.Send("get") // obtain the 100
  Alice.Send(200) // Store 200
  Alice.Send("get") // get the 200
$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.