[erlang-questions] newbie: why this code caused function_clause exception?

Anthony Kong anthony.hw.kong@REDACTED
Sat Jan 26 03:57:27 CET 2008


Hi, all,

I have written a very simple script:

==========================

-module(p2).
-export([getfib/1, main/0]).

start() -> spawn(fun() -> fib(1, 2) end).

fib(N, M) ->
  receive
    {Pid, next} ->
      Pid ! {self(), N + M},
      fib(M, N+ M)
  end.

getfib(Pid) ->
  Pid ! {self(), next},
  receive
    {_, N} ->
      %% io:format("~p~n", [N])
      N
  end.


%%%
%%% Program main()
%%%
main() ->
  Pid = start(),
  [X || X <- getfib(Pid)].
  %% getfib(Pid).

==========================

When I tried to run the p2:main(), I got the following error:

2> p2:main().

=ERROR REPORT==== 26-Jan-2008::13:27:42 ===
Error in process <0.87.0> with exit value:
{function_clause,[{p2,'-main/0-lc$^0/1-0-',[3]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}

** exited: {function_clause,[{p2,'-main/0-lc$^0/1-0-',[3]},
                             {erl_eval,do_apply,5},
                             {shell,exprs,6},
                             {shell,eval_loop,3}]} **


My questions:

1) Why the list comprehension in the p2:main() caused the
function_clause exception?
2) In the stack trace, what does '-main/0-lc$^0/1-0-'  mean?

I am using

$ erl -version
Erlang (ASYNC_THREADS) (BEAM) emulator version 5.5.2

on Ubuntu.

Cheers,

Anthony

-- 
/*--*/
Don't EVER make the mistake that you can design something better than
what you get from ruthless massively parallel trial-and-error with a
feedback cycle. That's giving your intelligence _much_ too much
credit.

- Linus Torvalds



More information about the erlang-questions mailing list