| 1 | // Copyright Louis Dionne 2013-2022 |
|---|---|
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) |
| 4 | |
| 5 | #include <boost/hana/functional/fix.hpp> |
| 6 | |
| 7 | #include <boost/hana/assert.hpp> |
| 8 | #include <boost/hana/config.hpp> |
| 9 | #include <boost/hana/equal.hpp> |
| 10 | #include <boost/hana/eval_if.hpp> |
| 11 | #include <boost/hana/functional/always.hpp> |
| 12 | #include <boost/hana/integral_constant.hpp> |
| 13 | #include <boost/hana/minus.hpp> |
| 14 | #include <boost/hana/mult.hpp> |
| 15 | namespace hana = boost::hana; |
| 16 | |
| 17 | |
| 18 | BOOST_HANA_CONSTEXPR_LAMBDA auto fact = hana::fix([](auto fact, auto n) { |
| 19 | return hana::eval_if(hana::equal(n, hana::ullong_c<0>), |
| 20 | hana::always(hana::ullong_c<1>), |
| 21 | [=](auto _) { return hana::mult(n, fact(_(n) - hana::ullong_c<1>)); } |
| 22 | ); |
| 23 | }); |
| 24 | |
| 25 | constexpr unsigned long long reference(unsigned long long n) |
| 26 | { return n == 0 ? 1 : n * reference(n: n - 1); } |
| 27 | |
| 28 | template <int n> |
| 29 | void test() { |
| 30 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 31 | fact(hana::ullong_c<n>), |
| 32 | hana::ullong_c<reference(n)> |
| 33 | )); |
| 34 | test<n - 1>(); |
| 35 | } |
| 36 | |
| 37 | template <> void test<-1>() { } |
| 38 | |
| 39 | int main() { |
| 40 | test<15>(); |
| 41 | } |
| 42 |
