Last active
February 19, 2021 20:17
-
-
Save awostenberg/1fcce76e1d852103e50acd017f621d09 to your computer and use it in GitHub Desktop.
life
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
// q: how would you encode Conways rule of life in f#? | |
// a: how about a pattern match emitting an action? | |
type Action = NoChange | BecomeInactive | BecomeActive | |
/// return what should happen to the central cell given active neighbor count | |
let kernel' centralCell activeNeighbors = | |
match centralCell, (activeNeighbors<2), (activeNeighbors>3), activeNeighbors=3 with | |
| true,true,_,_ -> BecomeInactive // living cell with fewer than two neighbors dies | |
| true,false,false,_ -> NoChange // living cell with two or three neighbors lives to next generation | |
| true,false,true,_ -> BecomeInactive // living cell with more than there neighbors dies | |
| false,_,_,true -> BecomeActive // empty cell with exactly three neighbors births new life | |
| false,_,_,false -> NoChange |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool.
Could you also implement it like so?
It's not as explicit with the rules, but it seems easier to understand.