Skip to content

Instantly share code, notes, and snippets.

@robertoaloi
Created June 29, 2013 22:35
Show Gist options
  • Save robertoaloi/5892963 to your computer and use it in GitHub Desktop.
Save robertoaloi/5892963 to your computer and use it in GitHub Desktop.
-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
Copy link
Author

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