Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
-module(this_is_fun).
-export([have_fun/0]).
-export([loop/0]).
have_fun() ->
table = ets:new(table, [named_table]),
F = fun(X) -> X + 1 end,
ets:insert(table, {function, F}),
register(funny, spawn(?MODULE, loop, [])).
loop() ->
receive
X ->
[{function, F}] = ets:lookup(table, function),
io:format("~p", [F(X)])
end,
loop().
@robertoaloi

This comment has been minimized.

Copy link
Owner Author

@robertoaloi robertoaloi commented Jun 29, 2013

1> c(this_is_fun).
{ok,this_is_fun}
2> this_is_fun:have_fun().
true
3> funny ! 10.
1110
4> l(this_is_fun).        
{module,this_is_fun}
5> funny ! 10.    
1110
6> l(this_is_fun).
{module,this_is_fun}
7> funny ! 10.    
** exception error: bad argument
     in operator  !/2
        called as funny ! 10

Have the students explain why:

  • The crash happens
  • The reason behind 1110 being printed out.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment