Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 764689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:46:07+00:00 2026-05-14T16:46:07+00:00

So I’ve been using Erlang for the last eight hours, and I’ve spent two

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T16:46:08+00:00Added an answer on May 14, 2026 at 4:46 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.