Skip to content

Instantly share code, notes, and snippets.

@bcobb
Last active June 18, 2021 12:18
Show Gist options
  • Save bcobb/70dac95ae10a39632e0b to your computer and use it in GitHub Desktop.
Save bcobb/70dac95ae10a39632e0b to your computer and use it in GitHub Desktop.
SICP cons/car/cdr in Swift
enum ConsPosition {
case Left, Right
}
func cons<T>(a: T, b: T) -> (ConsPosition -> T) {
func innerCons(i: ConsPosition) -> T {
if i == .Left {
return a;
} else {
return b;
}
}
return innerCons;
}
func car<T>(innerCons: ConsPosition -> T) -> T {
return innerCons(.Left);
}
func cdr<T>(innerCons: ConsPosition -> T) -> T {
return innerCons(.Right);
}
// let list = cons("a", 2)
// println(car(list))
// println(cdr(list))
@eczn
Copy link

eczn commented Jun 18, 2021

cons(1, cons(2, nil));

it doesnt work ?

@bcobb
Copy link
Author

bcobb commented Jun 18, 2021

@eczn you are very likely correct -- I cannot remember if I thought I had this working or if I did not have this working and created a gist to get some help with it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment