Last active
March 4, 2021 06:23
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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