1 | /*============================================================================= |
2 | Copyright (c) 2011 Thomas Heller |
3 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
4 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
5 | ==============================================================================*/ |
6 | |
7 | #include <boost/phoenix/core.hpp> |
8 | #include <boost/phoenix/statement.hpp> |
9 | #include <boost/phoenix/operator.hpp> |
10 | #include <boost/phoenix/scope.hpp> |
11 | #include <iostream> |
12 | #include <boost/phoenix/scope/this.hpp> |
13 | |
14 | |
15 | template <typename T0> |
16 | void f(T0 t) |
17 | { |
18 | std::cout << t(1) << "\n" ; |
19 | std::cout << t(2) << "\n" ; |
20 | std::cout << t(3) << "\n" ; |
21 | std::cout << t(4) << "\n" ; |
22 | std::cout << t(5) << "\n" ; |
23 | std::cout << t(6) << "\n" ; |
24 | std::cout << t(7) << "\n" ; |
25 | } |
26 | |
27 | template <typename T0> |
28 | void f_2(T0 t) |
29 | { |
30 | for(int i = 0; i < 10; ++i) |
31 | { |
32 | for(int j = 0; j < i; ++j) |
33 | { |
34 | std::cout << t(i, j) << " " ; |
35 | } |
36 | std::cout << "\n" ; |
37 | } |
38 | } |
39 | |
40 | int main() |
41 | { |
42 | //using boost::phoenix::_this; |
43 | using boost::phoenix::if_; |
44 | using boost::phoenix::if_else; |
45 | using boost::phoenix::val; |
46 | using boost::phoenix::let; |
47 | using boost::phoenix::nothing; |
48 | using boost::phoenix::arg_names::_1; |
49 | using boost::phoenix::arg_names::_2; |
50 | using boost::phoenix::local_names::_a; |
51 | |
52 | f(t: ( |
53 | if_(cond: _1 == 0) |
54 | [ |
55 | std::cout << val(t: "\n" ) |
56 | ] |
57 | .else_ |
58 | [ |
59 | std::cout << _1 << " " |
60 | , this_(a0: _1 - 1) |
61 | ] |
62 | , val(t: 0) |
63 | )); |
64 | |
65 | /* |
66 | f(( |
67 | if_else( |
68 | _1 == 0 |
69 | , _1 |
70 | ,this_(_1 - 1) |
71 | ) |
72 | )); |
73 | */ |
74 | |
75 | f(t: ( |
76 | if_else( |
77 | a0: _1 != 0 |
78 | ,a1: this_(a0: _1 - 1) |
79 | , a2: _1 |
80 | ) |
81 | )); |
82 | /* |
83 | |
84 | f(( // fac(n) = n * fac(n-1); fac(1) = 1 |
85 | if_else( |
86 | _1 <= 1 |
87 | , 1 |
88 | , _1 * _this(_1 - 1) |
89 | ) |
90 | )); |
91 | |
92 | f(( // fac(n) = n * fac(n-1); fac(1) = 1 |
93 | if_else( |
94 | _1 > 1 |
95 | , let(_a = _this(_1-1))[_1 * _a] |
96 | , 1 |
97 | ) |
98 | )); |
99 | |
100 | f(( // fib(n) = fib(n-1) + fib(n-2); fib(0) = 0; fib(1) = 1 |
101 | if_else( |
102 | _1 == 0 |
103 | , 0 |
104 | , if_else( |
105 | _1 == 1 |
106 | , 1 |
107 | , _this(_1 - 1) + _this(_1 - 2) |
108 | ) |
109 | ) |
110 | )); |
111 | |
112 | f_2(( // bin(n, k) = bin(n-1, k-1) + bin(n-1, k); bin(n, 0) = 1; bin(0, k) = 0 |
113 | if_else( |
114 | _1 == 0 |
115 | , 0 |
116 | , if_else( |
117 | _2 == 0 |
118 | , 1 |
119 | , _this(_1 - 1, _2 - 1) + _this(_1 - 1, _2) |
120 | ) |
121 | ) |
122 | )); |
123 | */ |
124 | } |
125 | |