Created
March 21, 2016 11:51
-
-
Save Baudin999/62f865dd7bca09213bb6 to your computer and use it in GitHub Desktop.
A simple Process MOnad in F#
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
type Process<'a> = | |
| Value of 'a | |
| Message of string | |
let quad a = a * a | |
let add a b = a + b | |
let print m = | |
match m with | |
| Value n -> printf "Number: %d\n" n | |
| Message m -> printf "Message: %s\n" m | |
m | |
let validateNumber a = | |
match a with | |
| even when a % 2 = 0 -> Value a | |
| _ -> Message "Should be an even number!" | |
// map :: (a -> b) -> (a -> Mb) | |
let map f d = | |
Value (f d) | |
// bind :: Ma -> (a -> Mb) -> Mb | |
let bind f d = | |
match d with | |
| Value i -> f i | |
| m -> m | |
let p = (map quad) >> (bind validateNumber) >> print | |
p 2 | |
p 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment