Skip to content

Instantly share code, notes, and snippets.

@steverichey
Created May 4, 2020 15:22

Revisions

  1. steverichey created this gist May 4, 2020.
    39 changes: 39 additions & 0 deletions LittleTyper.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    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)