views:

114

answers:

1

So I've been using Erlang for the last eight hours, and I've spent two of those banging my head against the keyboard trying to figure out the exception error my console keeps returning.

I'm writing a dice program to learn erlang. I want it to be able to call from the console through the erlang interpreter. The program accepts a number of dice, and is supposed to generate a list of values. Each value is supposed to be between one and six.

I won't bore you with the dozens of individual micro-changes I made to try and fix the problem (random engineering) but I'll post my code and the error.

The Source: -module(dice2). -export([d6/1]).

d6(1) -> random:uniform(6); d6(Numdice) -> Result = [], d6(Numdice, [Result]).

d6(0, [Finalresult]) -> {ok, [Finalresult]};

d6(Numdice, [Result]) -> d6(Numdice - 1, [random:uniform(6) | Result]).


When I run the program from my console like so... dice2:d6(1).

...I get a random number between one and six like expected. However when I run the same function with any number higher than one as an argument I get the following exception...

**exception error: no function clause matching dice2:d6(1, [4|3])

... I know I I don't have a function with matching arguments but I don't know how to write a function with variable arguments, and a variable number of arguments.

I tried modifying the function in question like so....

d6(Numdice, [Result]) -> Newresult = [random:uniform(6) | Result], d6(Numdice - 1, Newresult).

... but I got essentially the same error. Anyone know what is going on here?

+5  A: 

This is basically a type error. When Result is a list, [Result] is a list with one element. E.g., if your function worked, it would always return a list with one element: Finalresult.

This is what happens (using ==> for "reduces to"):

d6(2) ==> %% Result == []
d6(2, [[]]) ==> %% Result == [], let's say random:uniform(6) gives us 3
d6(1, [3]) ==> %% Result == 3, let's say random:uniform(6) gives us 4
d6(0, [4|3]) ==> %% fails, since [Result] can only match one-element lists

Presumably, you don't want [[]] in the first call, and you don't want Result to be 3 in the third call. So this should fix it:

d6(Numdice) ->  Result = [], d6(Numdice, Result). %% or just d6(Numdice, []).

d6(0, Finalresult) -> {ok, Finalresult};
d6(Numdice, Result) -> d6(Numdice - 1, [random:uniform(6) | Result]).

Lesson: if a language is dynamically typed, this doesn't mean you can avoid getting the types correct. On the contrary, it means that the compiler won't help you in doing this as much as it could.

Alexey Romanov
Your solution worked beautifully and helped me to understand my mistake. I'm new to the functional paradigm so there is a lot to absorb.Where I was confused was in thinking by calling d6(Numdice, Result) with an empty list, any new calls would end up only being able to match against an empty list as one of the parameters to d6 because you can't rebind variables in scope.I guess I forgot binding rules only apply within a given function *before* the next call [rolls eyes]. I got caught up in the matching issue, and forgot about element length.Anyway, thanks for answering my question.
Jim
Don't forget you can vote the answer up as well as accepting it :)
Alexey Romanov
just for completness: `d6(1)` gives you a number, but e.g. `d6(2)` gives you `{ok, [Value1, Value2]}` which might lead to other bugs.
ZeissS
Good point! I think it's best to just remove the `d6(1)` clause.
Alexey Romanov