Created
August 9, 2017 13:57
-
-
Save nojaf/aa8b191e2f21c291fa58e80076f3c92f to your computer and use it in GitHub Desktop.
Computation Expressions question
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
| // Learn more about F# at http://fsharp.org | |
| // See the 'F# Tutorial' project for more help. | |
| open System | |
| let toInt a = | |
| match (Int32.TryParse(a)) with | |
| | (true, x) -> Some x | |
| | (false, _) -> None | |
| type MaybeBuilder() = | |
| member this.Bind(x, f) = | |
| printfn "x: %A, f: %A" x f | |
| match x with | |
| | None -> None | |
| | Some a -> f a | |
| member this.Return(x) = | |
| Some x | |
| let maybe = new MaybeBuilder() | |
| [<EntryPoint>] | |
| let main argv = | |
| printfn "%A" argv | |
| let parsed = | |
| maybe { | |
| let! head = Array.tryHead argv | |
| let! v = head |> toInt | |
| return v | |
| } | |
| printfn "parsed is %A" parsed | |
| 0 // return an integer exit code |
Thanks for the quick responses!
So if I change the maybe block to use the maybe instance I get:
let parsedTwo =
maybe.Bind
(tryHead argv,
fun head ->
maybe.Bind(toInt head, fun result ->
maybe.Return(result)
)
)if tryHead argv is None then the fun head -> ... never get's executed right?
@nojaf correct!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PS: it's not you. This is as tricky as functional programming in F# gets and the official documentation is terrible :)