Created
April 25, 2017 17:30
-
-
Save b0urb4k1/df8444b35770e17c56f16cf07bc8ea14 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| open System | |
| type TurtleProgram<'a> = | |
| | Stop of 'a | |
| | PenUp of TurtleProgram<'a> | |
| | PenDown of TurtleProgram<'a> | |
| let returnT x = | |
| Stop x | |
| let rec bindT f inst = | |
| match inst with | |
| | Stop x -> | |
| f x | |
| | PenUp(next) -> | |
| PenUp(bindT f next) | |
| | PenDown(next) -> | |
| PenDown(bindT f next) | |
| type TurtleProgramBuilder() = | |
| member this.Return(x) = returnT x | |
| member this.Bind(x,f) = bindT f x | |
| member this.Zero(x) = returnT () | |
| let turtleProgram = TurtleProgramBuilder () | |
| let rec interpretAsDistance program = | |
| let recurse = interpretAsDistance | |
| let log = printfn "%s" | |
| match program with | |
| | Stop a -> | |
| log "stop" | |
| 0 | |
| | PenUp next -> | |
| log "pen up" | |
| let nextProgram = next | |
| recurse nextProgram | |
| | PenDown next -> | |
| log "pen down" | |
| let nextProgram = next | |
| recurse nextProgram | |
| let penup = (PenUp (Stop ())) | |
| let pendown = (PenDown (Stop ())) | |
| let pp = turtleProgram { | |
| do! penup | |
| do! pendown | |
| } | |
| [<EntryPoint>] | |
| let main argv = | |
| interpretAsDistance pp | |
| 0 // return an integer exit code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment