C++ Library Extensions
2022.12.09
To help learn modern C++ programming
044-functional.cpp
Go to the documentation of this file.
1
#include <iostream>
2
3
auto
&
cout
=
std::cout
;
4
auto
endl
=
"\n"
;
5
auto
endL
=
"\n\n"
;
6
7
/*
8
9
For your convenience, this is Pascal's Triangle formula
10
11
n C r = n-1 C r-1 + n-1 C r
12
13
*/
14
15
// N and R are constant expression or compile-time expressions
16
// we cannot modify the values of N and R, as in functional programming paradigm
17
// once the values of N and R are initialized, we cannot change their states.
18
//
19
// Functional Programming is based on Recursion.
20
21
// this is primary template.
22
// primary template determines the number of template parameters.
23
template
<
size_t
N,
size_t
R>
24
struct
pascal_triangle
25
{
26
// in functional programming paradigm, once we initialize an object
27
// we cannot modify its state. So, keyword constexpr makes value immutable.
28
constexpr
static
size_t
value
=
pascal_triangle
< N-1, R-1 >
::value
// n-1 C r-1
29
+
// +
30
pascal_triangle
< N-1, R >
::value
;
// n-1 C r
31
};
32
33
template
<
size_t
N>
34
struct
pascal_triangle
<N, N>
// nCn = 1
35
{
36
constexpr
static
size_t
value
= 1;
37
};
38
39
template
<
size_t
N>
40
struct
pascal_triangle
<N, 0>
// nC0 = 1
41
{
42
constexpr
static
size_t
value
= 1;
43
};
44
45
template
<
size_t
N>
46
struct
pascal_triangle
<N, 1>
// nC1 = N
47
{
48
constexpr
static
size_t
value
= N;
49
};
50
51
template
<
size_t
N,
size_t
R>
52
constexpr
size_t
pascal
=
pascal_triangle<N, R>::value
;
53
54
void
test_pascal_triangle
()
55
{
56
cout
<<
"5 C 0 = "
<< pascal<5, 0> <<
endl
;
57
cout
<<
"5 C 1 = "
<< pascal<5, 1> <<
endl
;
58
cout
<<
"5 C 2 = "
<< pascal<5, 2> <<
endl
;
59
cout
<<
"5 C 3 = "
<< pascal<5, 3> <<
endl
;
60
cout
<<
"5 C 4 = "
<< pascal<5, 4> <<
endl
;
61
cout
<<
"5 C 5 = "
<< pascal<5, 5> <<
endl
;
62
}
63
64
template
<
size_t
Start_R,
size_t
End_R>
65
struct
static_loop
66
{
67
template
<
size_t
N>
68
static
void
print_pascal_triangle
()
69
{
70
if
constexpr
(Start_R <= End_R)
71
{
72
cout
<< N <<
" C "
<< Start_R <<
" = "
73
<< pascal<N, Start_R> <<
endl
;
74
}
75
76
if
constexpr
(Start_R + 1 <= End_R)
77
{
78
// we are calling print_pascal_triangle<N>()
79
// recursively
80
static_loop<Start_R+1, End_R>
::
81
template print_pascal_triangle<N>();
82
}
83
}
84
85
};
86
87
template
<
size_t
N>
88
void
print_pascals
()
89
{
90
static_loop<0, N>
::
91
template print_pascal_triangle<N>();
92
}
93
94
/*
95
Fibonacci numbers are defined as below
96
97
f0 = 0
98
f1 = 1;
99
100
if n > 1,
101
102
f(n) = f(n-2) + f(n-1)
103
104
pascal's tree
105
C(n, r) = C(n-1, r-1) + C(n-1, r)
106
*/
107
int
main
()
108
{
109
// test_pascal_triangle();
110
111
cout
<<
"Combinations with 4 elements "
<<
endL
;
112
113
print_pascals<4>();
114
115
cout
<<
"Combinations with 5 elements "
<<
endL
;
116
117
print_pascals<5>();
118
119
cout
<<
"Combinations with 6 elements "
<<
endL
;
120
121
print_pascals<6>();
122
123
cout
<<
"Combinations with 7 elements "
<<
endL
;
124
125
print_pascals<7>();
126
127
cout
<<
"Combinations with 8 elements "
<<
endL
;
128
129
print_pascals<8>();
130
131
}
132
pascal
constexpr size_t pascal
Definition:
044-functional.cpp:52
endL
auto endL
Definition:
044-functional.cpp:5
cout
auto & cout
Definition:
044-functional.cpp:3
test_pascal_triangle
void test_pascal_triangle()
Definition:
044-functional.cpp:54
endl
auto endl
Definition:
044-functional.cpp:4
main
int main()
Definition:
044-functional.cpp:107
print_pascals
void print_pascals()
Definition:
044-functional.cpp:88
pascal_triangle
Definition:
044-functional.cpp:25
pascal_triangle::value
static constexpr size_t value
Definition:
044-functional.cpp:28
static_loop
Definition:
055-functional_programming.cpp:40
static_loop::print_pascal_triangle
static void print_pascal_triangle()
Definition:
044-functional.cpp:68
CppExtension
tutorial
044-functional.cpp
Generated by
1.9.4