I'd like the example computation expression and values below to return 6. For some the numbers aren't yielding like I'd expect. What's the step I'm missing to get my result? Thanks!
type AddBuilder() =
let mutable x = 0
member _.Yield i = x <- x + i
member _.Zero() = 0
member _.Return() = x
let add = AddBuilder()
(* Compiler tells me that each of the numbers in add don't do anything
and suggests putting '|> ignore' in front of each *)
let result = add { 1; 2; 3 }
(* Currently the result is 0 *)
printfn "%i should be 6" result
Note: This is just for creating my own computation expression to expand my learning. Seq.sum would be a better approach. I'm open to the idea that this example completely misses the value of computation expressions and is no good for learning.
There is a lot wrong here.
First, let's start with mere mechanics.
In order for the
Yieldmethod to be called, the code inside the curly braces must use theyieldkeyword:But now the compiler will complain that you also need a
Combinemethod. See, the semantics ofyieldis that each of them produces a finished computation, a resulting value. And therefore, if you want to have more than one, you need some way to "glue" them together. This is what theCombinemethod does.Since your computation builder doesn't actually produce any results, but instead mutates its internal variable, the ultimate result of the computation should be the value of that internal variable. So that's what
Combineneeds to return:But now the compiler complains again: you need a
Delaymethod.Delayis not strictly necessary, but it's required in order to mitigate performance pitfalls. When the computation consists of many "parts" (like in the case of multipleyields), it's often the case that some of them should be discarded. In these situation, it would be inefficient to evaluate all of them and then discard some. So the compiler inserts a call toDelay: it receives a function, which, when called, would evaluate a "part" of the computation, andDelayhas the opportunity to put this function in some sort of deferred container, so that laterCombinecan decide which of those containers to discard and which to evaluate.In your case, however, since the result of the computation doesn't matter (remember: you're not returning any results, you're just mutating the internal variable),
Delaycan just execute the function it receives to have it produce the side effects (which are - mutating the variable):And now the computation finally compiles, and behold: its result is
6. This result comes from whateverCombineis returning. Try modifying it like this:Now suddenly the result of your computation becomes
"foo".And now, let's move on to semantics.
The above modifications will let your program compile and even produce expected result. However, I think you misunderstood the whole idea of the computation expressions in the first place.
The builder isn't supposed to have any internal state. Instead, its methods are supposed to manipulate complex values of some sort, some methods creating new values, some modifying existing ones. For example, the
seqbuilder1 manipulates sequences. That's the type of values it handles. Different methods create new sequences (Yield) or transform them in some way (e.g.Combine), and the ultimate result is also a sequence.In your case, it looks like the values that your builder needs to manipulate are numbers. And the ultimate result would also be a number.
So let's look at the methods' semantics.
The
Yieldmethod is supposed to create one of those values that you're manipulating. Since your values are numbers, that's whatYieldshould return:The
Combinemethod, as explained above, is supposed to combine two of such values that got created by different parts of the expression. In your case, since you want the ultimate result to be a sum, that's whatCombineshould do:Finally, the
Delaymethod should just execute the provided function. In your case, since your values are numbers, it doesn't make sense to discard any of them:And that's it! With these three methods, you can add numbers:
I think numbers are not a very good example for learning about computation expressions, because numbers lack the inner structure that computation expressions are supposed to handle. Try instead creating a
maybebuilder to manipulateOption<'a>values.Added bonus - there are already implementations you can find online and use for reference.
1
seqis not actually a computation expression. It predates computation expressions and is treated in a special way by the compiler. But good enough for examples and comparisons.