Created
June 11, 2014 06:41
-
-
Save matthieubulte/27f0cb9f3d803e2632e0 to your computer and use it in GitHub Desktop.
cons/car/cdr in swift
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
func cdr<L, R>(p: () -> (L,R)) -> R { | |
let (l,r) = p() | |
return r | |
} | |
func car<L, R>(p: () -> (L,R)) -> L { | |
let (l,r) = p() | |
return l | |
} | |
func cons<L, R>(l:L, r:R) -> (()->(L,R)) { | |
return {()->(L,R) in return (l,r)} | |
} | |
var list = cons("left", cons("center", cons("right", nil))) | |
car(list) | |
car(cdr(list)) | |
car(cdr(cdr(list))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment