Created
January 13, 2017 23:29
-
-
Save tenderlove/04e1a4d5f9f0bf44e222e4104bc14cdf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
I am trying to implement cons / car / cdr in terms of lambdas, then | |
implement `each` to walk the cons cells, but I can't seem to get the `each` | |
function to work :( | |
*/ | |
@discardableResult func cons(_ x: Any, _ y: Any) -> (((Any, Any) -> Any) -> Any) { | |
return { m in m(x, y) } | |
} | |
@discardableResult func car(_ z: (((Any, Any) -> Any) -> Any)) -> Any { | |
return z { p, q in p } | |
} | |
@discardableResult func cdr(_ z: (((Any, Any) -> Any) -> Any)) -> Any { | |
return z { p, q in q } | |
} | |
/* | |
func each(_ x: Any, _ thing: (Any) -> Any) -> Any { | |
thing(car(x)) | |
each(cdr(x), thing) | |
} | |
*/ | |
car(cons(1, 2)) | |
print(cdr(cons(1, cons(2, 3)))) | |
print(car(cons(1, cons(2, 3)))) // => 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here’s what I came up with using casting. Note that the types differ—I wasn’t clear on what should be returned, so I opted to return
()
(nothing).