Skip to content

Instantly share code, notes, and snippets.

@steverichey
Created May 4, 2020 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steverichey/432e8b65e67910bf3096c0f0660a2533 to your computer and use it in GitHub Desktop.
Save steverichey/432e8b65e67910bf3096c0f0660a2533 to your computer and use it in GitHub Desktop.
A Swift playground for the beginning of the book, The Little Typer
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