Skip to content

Instantly share code, notes, and snippets.

@taketo1024 taketo1024/PeanoNatural
Last active Aug 29, 2015

Embed
What would you like to do?
//: Playground - noun: a place where people can play
import UIKit
struct N: Equatable, CustomStringConvertible, IntegerLiteralConvertible {
private let val: [Any]
private init(_ val: [Any]) {
self.val = val
}
static var zero: N {
return N([])
}
var description: String {
return "\(val.count) = \(val.description)"
}
init(integerLiteral value: IntegerLiteralType) {
var n = N.zero
for _ in 0..<value {
n = n+
}
self.val = n.val
}
}
postfix operator + {}
postfix func +(n: N) -> N {
return N(n.val + [n.val])
}
postfix operator - {}
postfix func -(n: N) -> N {
if n.val.isEmpty {
assertionFailure("0- doesn't exist.")
}
return N(n.val.last as! [Any])
}
func ==(n: N, m: N) -> Bool {
if (n.val.isEmpty && m.val.isEmpty) {
return true
} else if (n.val.isEmpty || m.val.isEmpty) {
return false
} else {
return (n- == m-)
}
}
func +(n: N, m: N) -> N {
if(m.val.isEmpty) {
return n
} else {
return (n + m-)+
}
}
func *(n: N, m: N) -> N {
if(m.val.isEmpty) {
return N.zero
} else {
return (n * m-) + n
}
}
// sample
let n0 = N.zero
let n1 = n0+
let n2 = n1+
let n3 = n2+
let n5 = n2 + n3
print(n5)
let n10 = n2 * n5
print(n10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.