Skip to content

Instantly share code, notes, and snippets.

@Slackwise
Last active March 4, 2021 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Slackwise/9450a5a7c0e8a28a4d04276142d15c80 to your computer and use it in GitHub Desktop.
Save Slackwise/9450a5a7c0e8a28a4d04276142d15c80 to your computer and use it in GitHub Desktop.
Implemented the cons cell linked list data structure as well as reduce() using only recursive curried functions and closures.
cons = a => b => f => f(a)(b)
car = a => b => a
cdr = a => b => b
reduce = f => i => l =>
l
? reduce (f) (f(i)(l(car))) (l(cdr))
: i
l = cons(1)(cons(2)(cons(3)(null)))
sum = x => y => x + y
reduce(sum)(0)(l)
(r = f => i => l => l ? r (f) (f(i)(l(a => b => a))) (l(a => b => b)) : i)(x => y => x + y)(0)((a => b => f => f(a)(b))(1)((a => b => f => f(a)(b))(2)((a => b => f => f(a)(b))(3)(null))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment