Last active
August 29, 2015 13:57
-
-
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
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
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