| Author |
Message |
< Erlang ~ [newbie] I can't understand compiler warning 'badarith' |
| breetai |
Posted: Wed Oct 17, 2007 11:33 pm |
|
|
|
Joined: 17 Oct 2007
Posts: 2
Location: Southern Arizona, USA
|
I get the warning :this expression would cause a 'badarith' exception at run-time
on line 22, the spawn() in start/1
Code: %%-----8<-----------------
-module(guessgame).
-compile(export_all).
loop(Target) ->
receive
{Pid, Guess} when Guess == Target ->
Pid ! {self(), correct},
loop(Target);
{Pid, Guess} when Guess < Target ->
Pid ! {self(), low},
loop(Target);
{Pid, _ } ->
Pid ! {self(), high},
loop(Target)
end.
start(Target) ->
io:format("Target = ~p~n",[Target]),
spawn(guessgame, loop/1, [Target]).
mcguess(Pid, What) ->
rpc(Pid, What).
rpc(Pid, Request) ->
Pid ! {self(), Request},
receive
{Pid, Response} ->
Response
end.
%%-----8<-----------------
I don't understand the error above. Please help. |
|
|
| Back to top |
|
| hao |
Posted: Thu Oct 18, 2007 8:09 am |
|
|
|
User
Joined: 20 Aug 2007
Posts: 18
Location: Uppsala, Sweden
|
I think the problem is that the second argument of function 'spawn(guessgame, loop/1, [Target])' on line 22 is wrong. That causes the compile warning of 'badarith' exception at run-time. The correct one should be 'spawn(guessgame, loop, [Target])'.
Please refer to 'spawn' function in erlang module. You should not write in the format of 'func/arity' when that function is used as the parameter of another caller function. 'func/arity' format is to be used in '-export([func/arity, ...])' in the beginning of a module. |
|
|
| Back to top |
|
| hao |
Posted: Thu Oct 18, 2007 8:18 am |
|
|
|
User
Joined: 20 Aug 2007
Posts: 18
Location: Uppsala, Sweden
|
|
| Back to top |
|
| breetai |
Posted: Thu Oct 18, 2007 12:14 pm |
|
|
|
Joined: 17 Oct 2007
Posts: 2
Location: Southern Arizona, USA
|
| Thanks, I'll change it and have a look. Can you explain why using 'fun' the way I did would cause a 'badarith' error? I was looking at how I used '[Target]', because that's the only place I put a number, I thought. |
|
|
| Back to top |
|
| hao |
Posted: Thu Oct 18, 2007 2:44 pm |
|
|
|
User
Joined: 20 Aug 2007
Posts: 18
Location: Uppsala, Sweden
|
No problem.
Well, the reason why your way of using "function/arity" in the original code would cause a "badarith" error is:
1) spawn(Module, Function, Args) -> pid()
Types:
Module = Function = atom()
Args = [term()]
Returns the pid of a new process started by the application of Module:Function to Args. The new process created will be placed in the system scheduler queue and be run some time later.
So the second argument "Function" should be an exact function name without appending "/arity". Otherwise, it will cause an run-time error because the spawn function cannot find the specified function to spawn.
2) What's more, the second argument "Function" in spawn function should be the type of atom(). if you look at what is atom data type in Erlang, you will find this:
An atom is a literal, a constant with name. An atom should be enclosed in single quotes (') if it does not begin with a lower-case letter or if it contains other characters than alphanumeric characters, underscore (_), or @.
Examples:
hello
phone_number
'Monday'
'phone number'
So in the spawn function of your original code, loop/1 is not a valid atom. Because spawn function must have checked the types of its input arguments, when the type is not correct, it will return a "badarith" error. You can try is_atom(loop/1) in the Erlang shell, it will exactly return a "badarith" error.
Therefore, it is not how you used "[Target]" that causes the "badarith" error. "Target" here could be of any type since it is a term(). So the way you used it should be correct.  |
|
|
| Back to top |
|
| Mazen |
Posted: Fri Oct 19, 2007 8:12 am |
|
|
|
User
Joined: 20 Jul 2006
Posts: 164
Location: London
|
breetai wrote: I was looking at how I used '[Target]', because that's the only place I put a number, I thought.
Actually, you should look for operators, not numbers when you get badarith. badarith, basically just says that the expression can not be computed (you can not divide an atom by 1 in this case) E.g. 'hello'/'world' doesn't work either, but will still cause a badarith crash So look for the operator, not the numbers.  |
|
|
| Back to top |
|
| hao |
Posted: Fri Oct 19, 2007 8:26 am |
|
|
|
User
Joined: 20 Aug 2007
Posts: 18
Location: Uppsala, Sweden
|
Quote: Actually, you should look for operators, not numbers when you get badarith. badarith, basically just says that the expression can not be computed (you can not divide an atom by 1 in this case) E.g. 'hello'/'world' doesn't work either, but will still cause a badarith crash Smile So look for the operator, not the numbers. Smile
Thanks, Mazen. I did not realize '/' could be recognized as a division operator in "function/arity" format. I agree with your point that we should focus on the operator not number when the 'badarith' occurs.  |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|