Question:

Help with Haskell Program

by  |  earlier

0 LIKES UnLike

Im learning Haskell and was wondering if anyone could tell me how to write a program called say triangle which takes a number and adds all previous numbers down to 0.

EG: triangle 4 = 4 3 2 1 0 so triangle 4 = 10?

Any idea's thanks.

 Tags:

   Report

1 ANSWERS


  1. My Haskell syntax is really rusty so I will leave the syntax out but basically you need to store the total and add to it the decrementing number e.g.

    triangle 4

    value = 4;

    sum = 0;

    while(value >= 0)

    {

        sum = sum + value;

        value = value - 1;

    }

    Sorry for lack on Haskell syntax but there should be examples on the net

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions