Reputation: 21
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?
Upvotes: 2
Views: 274
Reputation: 1978
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]).
Upvotes: 7