ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Jun 22 2020
·
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
```

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3`

is used as it is the official build and
dependency management tool for erlang now. Please refer to the tracks installation
instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

```
$ rebar3 eunit
```

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

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

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

```
%% Based on canonical data version 1.5.0
%% https://github.com/exercism/problem-specifications/raw/master/exercises/pascals-triangle/canonical-data.json
%% This file is automatically generated from the exercises canonical data.
-module(pascals_triangle_tests).
-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").
'1_zero_rows_test'() ->
?assertMatch([], pascals_triangle:rows(0)).
'2_single_row_test'() ->
?assertMatch([[1]], pascals_triangle:rows(1)).
'3_two_rows_test'() ->
?assertMatch([[1], [1, 1]], pascals_triangle:rows(2)).
'4_three_rows_test'() ->
?assertMatch([[1], [1, 1], [1, 2, 1]],
pascals_triangle:rows(3)).
'5_four_rows_test'() ->
?assertMatch([[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]],
pascals_triangle:rows(4)).
'6_five_rows_test'() ->
?assertMatch([[1], [1, 1], [1, 2, 1], [1, 3, 3, 1],
[1, 4, 6, 4, 1]],
pascals_triangle:rows(5)).
'7_six_rows_test'() ->
?assertMatch([[1], [1, 1], [1, 2, 1], [1, 3, 3, 1],
[1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]],
pascals_triangle:rows(6)).
'8_ten_rows_test'() ->
?assertMatch([[1], [1, 1], [1, 2, 1], [1, 3, 3, 1],
[1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1],
[1, 6, 15, 20, 15, 6, 1], [1, 7, 21, 35, 35, 21, 7, 1],
[1, 8, 28, 56, 70, 56, 28, 8, 1],
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]],
pascals_triangle:rows(10)).
```

```
-module(pascals_triangle).
-export([rows/1]).
-define(INIT, 1).
rows(0) ->
[];
rows(Count) ->
rows(Count - 1, [[?INIT]]).
rows(0, P) ->
P;
rows(N, P) ->
rows(N - 1, P ++ [next_row(lists:last(P))]).
next_row(Xs) ->
next_row(Xs, []).
next_row(Xs = [X | _], []) ->
next_row(Xs, [X]);
next_row([X], P) ->
P ++ [X];
next_row([X, Y | Xs], P) ->
next_row([Y | Xs], P ++ [X + Y]).
```

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,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments