views:

92

answers:

2

Okay so I started learning erlang recently but am baffled by the errors it keeps returning. I made a bunch of changes but I keep getting errors. The syntax is correct as far as I can tell but clearly I'm doing something wrong. Have a look...

-module(pidprint).
-export([start/0]).

dostuff([]) ->
    receive
        begin ->
     io:format("~p~n", [This is a Success])
 end.

sender([N]) ->
    N ! begin,
    io:format("~p~n", [N]).


start() ->
    StuffPid = spawn(pidprint, dostuff, []),
    spawn(pidprint, sender, [StuffPid]).

Basically I want to compile the script, call start, spawn the "dostuff" process, pass its process identifier to the "sender" process, which then prints it out. Finally I want to send a the atom "begin" to the "dostuff" process using the process identifier initially passed into sender when I spawned it.

The errors I keep getting occur when I try to use c() to compile the script. Here they are..

./pidprint.erl:6: syntax error before: '->'
./pidprint.erl:11: syntax error before: ','

What am I doing wrong?

+6  A: 

It appears that begin is a reserved word in Erlang. Use some other atom or put single quotes around it: 'begin'.

Also,you forgot your double quotes around "This is a success".

There are a couple of other bugs I fixed...

-module(pidprint).
-export([start/0, dostuff/0, sender/1]).

dostuff() -> 
   receive 
      'begin' ->
         io:format("~p~n", ["This is a Success"])
   end.

sender(N) -> 
   N ! 'begin',
   io:format("~p~n", [N]).

start() -> 
   StuffPid = spawn(pidprint, dostuff, []), 
   spawn(pidprint, sender, [StuffPid]).
dsmith
A: 

Thanks for clearing up the problem. I wasn't aware that "begin" was reserved. I also wasn't aware that I needed double-quotes around a list when printing using io:format. Thanks for the help, otherwise I would have been forced to by a new keyboard at the rate I was banging my head against my current one.

Jim