Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun flip f lhs rhs -> f rhs lhs;
- fun expt b n
- -> fun square x -> x * x;
- fun isEven -> toBool . (flip (%) 2);
- isEven n ? square (expt b (n / 2)),
- else ? b * expt b (n - 1);
- fun expt _ 0 -> 1;
- C++ OUTPUT:
- template<template <typename> class a1, typename a2, typename a3>
- struct flip__a3_;
- template<template <typename> class a1, typename a2, typename a3>
- using flip__a3 = typename flip__a3_<a1, a2 ,a3>::ret;
- template<template <typename> class a1, typename a2>
- struct flip__a2 {
- template<typename a3>
- using apply = flip__a3_<a1,a2,a3>;
- };
- template<template <typename> class a1>
- struct flip__a1 {
- template<typename a2>
- using apply = flip__a2<a1,a2>;
- };
- template<typename a1, typename a2>
- struct expt__a2;
- template<typename a1>
- struct expt__a1 {
- template<typename a2>
- using apply = expt__a2<a1,a2>;
- };
- template<template <typename> class a1, typename a2, typename a3>
- struct flip__a3_ {
- using ret = typename a1<a3>::template apply<a2>;
- };
- template<typename a1, typename a2>
- struct expt__a2 {
- template <typename s1a1>
- struct square__a1;
- template <typename s1a1>
- struct isEven__a1;
- template <typename s1a1>
- struct square__a1 {
- template <typename s1a2>
- using ret = typename mul__a1<s1a1>::template apply<s1a2>::ret;
- };
- template <typename s1a1>
- struct isEven__a1_ {
- using ret = typename comp__a1<toBool__a1>::apply<typename flip__a1<mod__a1>::apply<Int<2>>>::apply<s1a1>::ret;
- };
- using ret = typename cond<
- if<typename isEven__a1<a2>::ret,
- typename square<typename expt__a1<a1>::template apply<typename div<a2, Int<2>>::ret>::ret>::ret >
- if<true,
- typename mul<a1, typename expt__a1<a1>::template apply<typename sub<a2>::template apply<Int<1>>::ret>::ret>::ret>
- >::type;
- };
- template<typename a1>
- struct expt__a2<a1, Int<0>> {
- using ret = Int<1>;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement