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

# baixeing's solution

## to Pascal's Triangle in the Erlang Track

Published at Jun 22 2020 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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
``````

## Running tests

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
``````

## Questions?

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.

## Source

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

## Submitting Incomplete Solutions

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

### pascals_triangle_tests.erl

``````%% 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]).``````