[erlang-questions] 1st day of Erlang: what is wrong with this code

Kenneth Lakin kennethlakin@REDACTED
Tue Jul 14 07:44:09 CEST 2015


On 07/12/2015 03:19 AM, avinash D'silva wrote:
> Error in process <0.58.0> with exit value:
> {function_clause,[{string,to_upper,
>                           [<<"llll\r\n">>],
>                           [{file,"string.erl"},{line,529}]},
>                   {echo,loop,1,[{file,"echo.erl"},{line,23}]}]}
> 
> That was the error. But how would I find the cause of the error?
> In java or other languages that I worked earlier, I would get a "Type
> Mismatch Error".

I'm fairly certain that this is how Erlang spells "Type Mismatch Error".
To see this in action, try compiling this program that makes use of
function guards:

-module(test).
-export([test/1]).

test(Arg) when is_list(Arg) ->
   io:format("Is list~n");
test(Arg) when is_atom(Arg) ->
   io:format("Is atom~n").

If you run the function with a list, atom, and then binary, you can see
that the first two are accepted, but there's no function to handle a
binary, so the program dies:

Erlang/OTP 18 [erts-7.0] [source] [smp:2:2] [async-threads:10] [hipe]
[kernel-poll:false]

Eshell V7.0 (abort with ^G)
1> c(test).
{ok,test}
2> spawn(test, test, ["hello"]).
Is list
<0.47.0>
3> spawn(test, test, [hello]).
Is atom
<0.49.0>
4> spawn(test, test, [<<"hello">>]).

=ERROR REPORT==== 13-Jul-2015::22:34:24 ===
Error in process <0.53.0> with exit value:
{function_clause,[{test,test,[<<"hello">>],[{file,"test.erl"},{line,4}]}]}
<0.53.0>
5>

(For information about spawn/3, check out
http://www.erlang.org/doc/man/erlang.html#spawn-3 . I'm spawning the
function, rather than calling it directly, as the shell does a bit of
interpretation of the error, so it's less obvious that what is happening
here is the same thing that was happening with the program that you were
having trouble with.)

Erlang error reports are hard to read at first. With more experience,
they'll become easy to read. I'm also pretty sure that this is the place
to ask questions when you have them. :)

Good luck, and have fun!

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150713/b5c117e8/attachment.bin>


More information about the erlang-questions mailing list