Skip to content

Instantly share code, notes, and snippets.

@matthieubulte
Created June 11, 2014 06:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthieubulte/27f0cb9f3d803e2632e0 to your computer and use it in GitHub Desktop.
Save matthieubulte/27f0cb9f3d803e2632e0 to your computer and use it in GitHub Desktop.
cons/car/cdr in swift
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