Exception error in Erlang

Posted by Jim on Stack Overflow See other posts from Stack Overflow or by Jim
Published on 2010-05-04T04:25:33Z Indexed on 2010/05/04 4:38 UTC
Read the original article Hit count: 275

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about erlang

Related posts about error