I'm using F# v 1.9.6.2, and I've defined a very simple computation expression:
type MaybeBuilder() =
member this.Let(x, f) =
printfn "this.Let: %A" x
this.Bind(Some x, f)
member this.Bind(x, f) =
printfn "this.Bind: %A" x
match x with
| Some(x) when x >= 0 && x <= 100 -> f(x)
| _ -...
This code:
type Result = Success of string
type Tracer() =
member x.Bind(p: Result, rest: (string -> Result)) =
match p with
| Success s -> rest s
let tracer = new Tracer()
let t = tracer {
let! x = Success "yes!"
let! y = Success "waste of time"
return! Success x
}
printfn "%A" t
prints Success "yes!"
But gives ...
I've been toying with monads in F# (aka computation expressions) and I wrote this simple Identity monad:
type Identity<'a> =
| Identity of 'a
type IdentityBuilder() =
member x.Bind (Identity v) f = f(v)
member x.Return v = Identity v
let identity = new IdentityBuilder()
let getInt() = identity { return Int32.Parse(Conso...
I'd like to create a builder that builds expressions that returns something like a continuation after each step.
Something like this:
module TwoSteps =
let x = stepwise {
let! y = "foo"
printfn "got: %A" y
let! z = y + "bar"
printfn "got: %A" z
return z
}
printfn "two steps"
let a = x()
printfn "somethin...
I'm reading the book Real-world functional programming by Tomas Petricek and Jon Skeet and I'm having a hard time digesting the section on computation expressions1) (aka monads).
Through this book, I learnt that contrary to my previous experiences LINQ query expressions aren't restricted to IEnumerable<T>, but can work on other custom...
Some background first. I am currently learning some stuff about monadic parser combinators. While I tried to transfer the 'chainl1' function from this paper (p. 16-17), I came up with this solution:
let chainl1 p op = parser {
let! x = p
let rec chainl1' (acc : 'a) : Parser<'a> =
let p' = parser {
let! f = op
...
In a previous question I was told how to rewrite my computation expressions so it uses tail recursion. I rewrote my code but still got a StackOverflowException. To locate the problem I wrote some small code using a state monad (taken from this blog entry):
type State<'a, 's> = State of ('s -> 'a * 's)
let runState (State s) initialSta...