Created
May 4, 2020 15:22
-
-
Save steverichey/432e8b65e67910bf3096c0f0660a2533 to your computer and use it in GitHub Desktop.
A Swift playground for the beginning of the book, The Little Typer
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
import Cocoa | |
import Foundation | |
protocol AtomType {} | |
struct Atom: AtomType {} | |
func == <Atom1, Atom2> (lhs: Atom1, rhs: Atom2) -> Bool where Atom1: AtomType, Atom2: AtomType { | |
return type(of: lhs) == type(of: rhs) // maybe | |
} | |
func == (lhs: (Atom, Atom), rhs: (Atom, Atom)) -> Bool { | |
return true // ?? | |
} | |
func cons<CarType, CdrType>(_ car: CarType, _ cdr: CdrType) -> (CarType, CdrType) { | |
return (car, cdr) | |
} | |
func car<CarType, CdrType>(_ pair: (CarType, CdrType)) -> CarType { | |
return pair.0 | |
} | |
func cdr<CarType, CdrType>(_ pair: (CarType, CdrType)) -> CdrType { | |
return pair.1 | |
} | |
let ratatouille = Atom() | |
let garlic = Atom() | |
let meal = cons(ratatouille, garlic) | |
let tomato = Atom() | |
let egg = Atom() | |
let omelette = cons(tomato, egg) | |
print(ratatouille == garlic) | |
print(meal == meal) | |
print(ratatouille == car(meal)) | |
print(garlic == cdr(meal)) | |
print(tomato == garlic) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment