Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. // Boolean logic
  2.  
  3. const tru = (t, f) => t()
  4. const fals = (t, f) => f()
  5.  
  6. const not = x => (t, f) => x(f, t)
  7. const and = (a, b) => (t, f) => a(
  8. () => b(t, f),
  9. () => f()
  10. )
  11. const or = (a, b) => (t, f) => a(
  12. () => t(),
  13. () => b(t, f)
  14. )
  15.  
  16.  
  17. // List basis
  18.  
  19. const emptyList = (selector) => selector(undefined, undefined, tru)
  20. const prepend = (el, list) => (selector) => selector(el, list, fals)
  21. const head = (list) => list((h, t, e) => h)
  22. const tail = (list) => list((h, t, e) => t)
  23. const isEmpty = (list) => list((h, t, e) => e)
  24.  
  25. const map = (list, fn) => isEmpty(list)(
  26. () => emptyList,
  27. () => prepend(fn(head(list)), map(tail(list), fn))
  28. )
  29.  
  30. const filter = (list, fn) => isEmpty(list)(
  31. () => emptyList,
  32. () => fn(head(list))(
  33. () => prepend(head(list), filter(tail(list), fn)),
  34. () => filter(tail(list), fn)
  35. )
  36. )
  37.  
  38.  
  39. // Numbers
  40.  
  41. const zero = emptyList
  42. const isZero = n => isEmpty(n)
  43. const inc = n => prepend(emptyList, n)
  44. const dec = n => tail(n)
  45. const one = inc(zero)
  46. const two = inc(one)
  47.  
  48. const add = (a, b) => isZero(b)(
  49. () => a,
  50. () => add(inc(a), dec(b))
  51. )
  52. const sub = (a, b) => isZero(b)(
  53. () => a,
  54. () => add(dec(a), dec(b))
  55. )
  56. const mul = (a, b) => isZero(b)(
  57. () => zero,
  58. () => add(a, mul(a, dec(b)))
  59. )
  60. const pow = (a, b) => isZero(b)(
  61. () => one,
  62. () => mul(a, pow(a, dec(b)))
  63. )
  64.  
  65. const isEqual = (a, b) => and(isZero(a), isZero(b))(
  66. () => tru,
  67. () => or(isZero(a), isZero(b))(
  68. () => fals,
  69. () => isEqual(dec(a), dec(b))
  70. )
  71. )
  72. const lessThan = (a, b) => and(isZero(a), isZero(b))(
  73. () => fals,
  74. () => isZero(a)(
  75. () => tru,
  76. () => isZero(b)(
  77. () => fals,
  78. () => lessThan(dec(a), dec(b))
  79. )
  80. )
  81. )
  82. const greaterThan = (a, b) => lessThan(b, a)
  83.  
  84. const div = (a, b) => lessThan(a, b)(
  85. () => zero,
  86. () => inc(div(sub(a, b), b))
  87. )
  88. const rem = (a, b) => lessThan(a, b)(
  89. () => a,
  90. () => rem(sub(a, b), b)
  91. )
  92.  
  93.  
  94. // Array
  95.  
  96. const nth = (list, n) => isZero(n)(
  97. () => head(list),
  98. () => nth(tail(list), dec(n))
  99. )
  100. const length = (list) => isEmpty(list)(
  101. () => zero,
  102. () => inc(length(tail(list)))
  103. )
  104.  
  105. const drop = (list, n) => isZero(n)(
  106. () => list,
  107. () => drop(tail(list), dec(n))
  108. )
  109. const take = (list, n) => isZero(n)(
  110. () => emptyList,
  111. () => prepend(head(list), take(tail(list), dec(n)))
  112. )
  113. const slice = (l, start, end) => take(drop(l, start), sub(end, start))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement