Advertisement
nullzero

Untitled

Jan 16th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. # fun fact(n :: Number) -> Number:
  2. # fun help(shadow n :: Number, acc :: Number) -> Number:
  3. # if n == 0:
  4. # acc
  5. # else:
  6. # help(n - 1, acc * n)
  7. # end
  8. # end
  9. # help(n, 1)
  10. # where:
  11. # fact(0) is 1
  12. # fact(5) is 120
  13. # end
  14.  
  15. # fun len(l, acc):
  16. # cases (List) l:
  17. # | empty => acc
  18. # | link(_, r) => len(r, 1 + acc)
  19. # end
  20. # where:
  21. # len(empty, 0) is 0
  22. # len(range(0, 10), 0) is 10
  23. # end
  24.  
  25. data Test:
  26. | zero
  27. | one(e :: Test, n :: Number)
  28. | two(e :: Test, n :: Number)
  29. | three(e :: Test, n :: Number)
  30. end
  31.  
  32. fun f<a>(t :: Test) -> Number:
  33. rec help = lam(shadow t :: Test, acc :: Number) -> Number:
  34. cases (Test) t:
  35. | zero => acc
  36. | one(e, n) =>
  37. var to-add = n
  38. when num-modulo(n, 2) == 0:
  39. to-add := 0 - to-add
  40. end
  41. help(e, acc + to-add)
  42. | two(e, n) =>
  43. to-add =
  44. if num-modulo(n, 2) == 0:
  45. 0 - n
  46. else:
  47. n
  48. end
  49. help(e, acc + to-add)
  50. | three(e, n) =>
  51. help(e, acc + if num-modulo(n, 2) == 0: 0 - n else: n end)
  52. end
  53. end
  54. help(t, 0)
  55. where:
  56. f(zero) is 0
  57. f(one(two(one(two(three(three(one(two(three(one(three(one(two(three(zero, 1), 2), 3), 4), 5), 6), 7), 8), 9), 10), 11), 12), 13), 14))
  58. is (1 + 3 + 5 + 7 + 9 + 11 + 13) - (2 + 4 + 6 + 8 + 10 + 12 + 14)
  59.  
  60. end
  61. #
  62. # fun triangle(n :: Number) -> Number:
  63. # fun help(shadow n :: Number, acc :: Number) -> Number:
  64. # if n == 0:
  65. # acc
  66. # else:
  67. # help(n - 1, acc + n)
  68. # end
  69. # end
  70. # help(n, 0)
  71. # where:
  72. # triangle(0) is 0
  73. # triangle(10) is 55
  74. # end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement