this.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include <boost/phoenix/core.hpp>
  7. #include <boost/phoenix/statement.hpp>
  8. #include <boost/phoenix/operator.hpp>
  9. #include <boost/phoenix/scope.hpp>
  10. #include <iostream>
  11. #include <boost/phoenix/scope/this.hpp>
  12. template <typename T0>
  13. void f(T0 t)
  14. {
  15. std::cout << t(1) << "\n";
  16. std::cout << t(2) << "\n";
  17. std::cout << t(3) << "\n";
  18. std::cout << t(4) << "\n";
  19. std::cout << t(5) << "\n";
  20. std::cout << t(6) << "\n";
  21. std::cout << t(7) << "\n";
  22. }
  23. template <typename T0>
  24. void f_2(T0 t)
  25. {
  26. for(int i = 0; i < 10; ++i)
  27. {
  28. for(int j = 0; j < i; ++j)
  29. {
  30. std::cout << t(i, j) << " ";
  31. }
  32. std::cout << "\n";
  33. }
  34. }
  35. int main()
  36. {
  37. //using boost::phoenix::_this;
  38. using boost::phoenix::if_;
  39. using boost::phoenix::if_else;
  40. using boost::phoenix::val;
  41. using boost::phoenix::let;
  42. using boost::phoenix::nothing;
  43. using boost::phoenix::arg_names::_1;
  44. using boost::phoenix::arg_names::_2;
  45. using boost::phoenix::local_names::_a;
  46. f((
  47. if_(_1 == 0)
  48. [
  49. std::cout << val("\n")
  50. ]
  51. .else_
  52. [
  53. std::cout << _1 << " "
  54. , this_(_1 - 1)
  55. ]
  56. , val(0)
  57. ));
  58. /*
  59. f((
  60. if_else(
  61. _1 == 0
  62. , _1
  63. ,this_(_1 - 1)
  64. )
  65. ));
  66. */
  67. f((
  68. if_else(
  69. _1 != 0
  70. ,this_(_1 - 1)
  71. , _1
  72. )
  73. ));
  74. /*
  75. f(( // fac(n) = n * fac(n-1); fac(1) = 1
  76. if_else(
  77. _1 <= 1
  78. , 1
  79. , _1 * _this(_1 - 1)
  80. )
  81. ));
  82. f(( // fac(n) = n * fac(n-1); fac(1) = 1
  83. if_else(
  84. _1 > 1
  85. , let(_a = _this(_1-1))[_1 * _a]
  86. , 1
  87. )
  88. ));
  89. f(( // fib(n) = fib(n-1) + fib(n-2); fib(0) = 0; fib(1) = 1
  90. if_else(
  91. _1 == 0
  92. , 0
  93. , if_else(
  94. _1 == 1
  95. , 1
  96. , _this(_1 - 1) + _this(_1 - 2)
  97. )
  98. )
  99. ));
  100. f_2(( // bin(n, k) = bin(n-1, k-1) + bin(n-1, k); bin(n, 0) = 1; bin(0, k) = 0
  101. if_else(
  102. _1 == 0
  103. , 0
  104. , if_else(
  105. _2 == 0
  106. , 1
  107. , _this(_1 - 1, _2 - 1) + _this(_1 - 1, _2)
  108. )
  109. )
  110. ));
  111. */
  112. }