Last active
August 29, 2015 14:12
-
-
Save ToJans/00cd903feadfd19404c0 to your computer and use it in GitHub Desktop.
Natural numbers and vectors in C# , the Idris way
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
// Data Nat = Z | S Nat | |
// | |
// plus : Nat -> Nat -> Nat | |
// plus Z y = y | |
// plus (S k) y = S (plus k y) | |
abstract class Nat { | |
} | |
class Zero : Nat { | |
Zero() {} | |
} | |
class Successor : Nat { | |
Nat prev; | |
Successor(Nat prev) {this.prev = prev} | |
} | |
var _0 = new Zero() | |
var _1 = new Successor(_0) | |
var _2 = new Successor(_1) | |
var _2 = new Successor(new Successor(_0)) | |
Nat plus(Nat x, Nat y) { | |
if (typeof(Zero) == x.GetType()) { | |
return y; | |
} else { | |
var succ = (Successor)x; | |
return new Successor(plus(succ.prev,y)) | |
} | |
} | |
Vector0 = {} | |
Vector1 = {A = "Tom"} | |
Vector2 = {A = "Jef", B = "Greg"} | |
Vector1 ++ Vector2 = {A = "Tom", B= "Jef", C "Greg"} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment