Skip to content

Instantly share code, notes, and snippets.

@d-plaindoux
Last active August 29, 2015 13:57
Show Gist options
  • Save d-plaindoux/9837706 to your computer and use it in GitHub Desktop.
Save d-plaindoux/9837706 to your computer and use it in GitHub Desktop.
Denotate Peano's integer addition using Golang - Kind of pattern matching based on types only
package main
import "fmt"
type Peano interface { isPeano() }
// Zero
type Zero struct {}
func (z Zero) isPeano() {}
// Succ
type Succ struct { n Peano }
func (s Succ) isPeano() {}
func eval(p Peano) int {
switch e := p.(type) {
case Zero: return 0
case Succ: return 1 + eval(e.n)
}
return 0;
}
func main() {
fmt.Println(eval(Succ{Succ{Zero{}}}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment