[erlang-questions] Beginner: What's wrong with this code?

Jeroen Koops koops.j@REDACTED
Wed Jun 22 11:10:15 CEST 2011


Hi Sid,

The final argument to spawn is a list of arguments. In your case, the
function format_temps takes one argument, which happens to be a list.

Therefore, you should use a list containing a list as the final argument to
spawn, as in:

spawn(?MODULE, format_temps, [ [{moscow,{f,-40}}, {bangalore,{f,40}},
{wellington, {c,24}}] ])

On Wed, Jun 22, 2011 at 11:03 AM, Sid Carter <sidcarter@REDACTED> wrote:

>  Hi Folks,
>
> New to Erlang and playing with code and trying to learn erlang.
>
> I have been reading the getting started guide and trying this code (on Mac
> OS X with erlang from MacPorts):
>
> -module(first).
> % call would look like format_temps([{moscow,{f,-40}}, {bangalore,{f,40}},
> {wellington, {c,24}}]).
> -export([format_temps/1, start/0]).
> format_temps([]) ->
> ok;
> format_temps([City|Rest]) ->
> print_temp(convert_to_celsius(City)),
> format_temps(Rest).
> convert_to_celsius({City, {c, C}}) ->
> {City, {c, C}};
> convert_to_celsius({City, {f, F}}) ->
> {City, {c, (F-32)*5/9}}.
> print_temp({City, {c, C}}) ->
> io:format("~p has a temperature of ~w C~n",[City,C]).
> start() ->
> Pid = spawn(?MODULE, format_temps, [{moscow,{f,-40}}, {bangalore,{f,40}},
> {wellington, {c,24}}]),
> Pid.
>
> When I try to run:
>
> first:format_temps(args).
>
> I don't have any problems. I get the correct response.
>
> When I try to run as:
>
> first:start().
>
> I get the following error:
>
> Error in process <0.49.0> with exit value:
> {undef,[{first,format_temps,[{moscow,{f,-40}},{bangalore,{f,40}},{wellington,{c,24}}]}]}
>
> What's wrong?
>
> Thanks
> Sid
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110622/04e4df73/attachment.htm>


More information about the erlang-questions mailing list