Published at Aug 10 2019
·
0 comments

Instructions

Test suite

Solution

Compute Pascal's triangle up to a given number of rows.

In Pascal's Triangle each number is computed by adding the numbers to the right and left of the current position in the previous row.

```
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
# ... etc
```

Refer to the Installing Elm page for information about installing elm.

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

```
$ elm-test
```

Automatically run tests again when you save changes:

```
$ elm-test --watch
```

As you work your way through the test suite, be sure to remove the `skip <|`

calls from each test until you get them all passing!

Pascal's Triangle at Wolfram Math World http://mathworld.wolfram.com/PascalsTriangle.html

It is possible to submit an incomplete solution so you can see how others have completed the exercise.

```
module Tests exposing (tests)
import Expect
import Test exposing (..)
import Triangle exposing (rows)
tests : Test
tests =
describe "Triangle"
[ test "no rows" <|
\() ->
Expect.equal [] (rows 0)
, skip <|
test "single row" <|
\() ->
Expect.equal [ [ 1 ] ] (rows 1)
, skip <|
test "two rows" <|
\() ->
Expect.equal [ [ 1 ], [ 1, 1 ] ] (rows 2)
, skip <|
test "three rows" <|
\() ->
Expect.equal [ [ 1 ], [ 1, 1 ], [ 1, 2, 1 ] ] (rows 3)
, skip <|
test "four rows" <|
\() ->
Expect.equal [ [ 1 ], [ 1, 1 ], [ 1, 2, 1 ], [ 1, 3, 3, 1 ] ] (rows 4)
, skip <|
test "negative rows" <|
\() ->
Expect.equal [] (rows -1)
]
```

```
module Triangle exposing (rows)
rows : Int -> List (List Int)
rows n =
{- https://en.wikipedia.org/wiki/Pascal%27s_triangle
"The rows of Pascal's triangle are conventionally enumerated
starting with row n = 0 at the top (the 0th row)", so instantly decrement
the number of rows by 1.
-}
List.range 0 (n - 1)
|> List.map generateRow
-- PRIVATE
generateRow : Int -> List Int
generateRow rowNum =
List.range 0 rowNum
|> List.map (binomial rowNum)
binomial : Int -> Int -> Int
binomial rowNum exponent =
{- https://en.wikipedia.org/wiki/Binomial_theorem
"n (rowNum) choose k (exponent)" => n!/(n - k)!k!
-}
factorial rowNum // (factorial (rowNum - exponent) * factorial exponent)
factorial : Int -> Int
factorial n =
List.range 1 n
|> List.product
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,076 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments