Last active
June 18, 2021 12:18
-
-
Save bcobb/70dac95ae10a39632e0b to your computer and use it in GitHub Desktop.
SICP cons/car/cdr in Swift
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
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 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
it doesnt work ?