开发者

Exception error in Erlang

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜