Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. fun flip f lhs rhs -> f rhs lhs;
  2.  
  3. fun expt b n
  4. -> fun square x -> x * x;
  5. fun isEven -> toBool . (flip (%) 2);
  6. isEven n ? square (expt b (n / 2)),
  7. else ? b * expt b (n - 1);
  8. fun expt _ 0 -> 1;
  9.  
  10. C++ OUTPUT:
  11.  
  12. template<template <typename> class a1, typename a2, typename a3>
  13. struct flip__a3_;
  14. template<template <typename> class a1, typename a2, typename a3>
  15. using flip__a3 = typename flip__a3_<a1, a2 ,a3>::ret;
  16. template<template <typename> class a1, typename a2>
  17. struct flip__a2 {
  18. template<typename a3>
  19. using apply = flip__a3_<a1,a2,a3>;
  20. };
  21. template<template <typename> class a1>
  22. struct flip__a1 {
  23. template<typename a2>
  24. using apply = flip__a2<a1,a2>;
  25. };
  26.  
  27. template<typename a1, typename a2>
  28. struct expt__a2;
  29. template<typename a1>
  30. struct expt__a1 {
  31. template<typename a2>
  32. using apply = expt__a2<a1,a2>;
  33. };
  34.  
  35. template<template <typename> class a1, typename a2, typename a3>
  36. struct flip__a3_ {
  37. using ret = typename a1<a3>::template apply<a2>;
  38. };
  39.  
  40. template<typename a1, typename a2>
  41. struct expt__a2 {
  42. template <typename s1a1>
  43. struct square__a1;
  44. template <typename s1a1>
  45. struct isEven__a1;
  46.  
  47. template <typename s1a1>
  48. struct square__a1 {
  49. template <typename s1a2>
  50. using ret = typename mul__a1<s1a1>::template apply<s1a2>::ret;
  51. };
  52.  
  53. template <typename s1a1>
  54. struct isEven__a1_ {
  55. using ret = typename comp__a1<toBool__a1>::apply<typename flip__a1<mod__a1>::apply<Int<2>>>::apply<s1a1>::ret;
  56. };
  57.  
  58. using ret = typename cond<
  59. if<typename isEven__a1<a2>::ret,
  60. typename square<typename expt__a1<a1>::template apply<typename div<a2, Int<2>>::ret>::ret>::ret >
  61. if<true,
  62. typename mul<a1, typename expt__a1<a1>::template apply<typename sub<a2>::template apply<Int<1>>::ret>::ret>::ret>
  63. >::type;
  64. };
  65.  
  66. template<typename a1>
  67. struct expt__a2<a1, Int<0>> {
  68. using ret = Int<1>;
  69. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement