Questions tagged [computation-expression]

Computation expressions in F# is a technique for writing computations that can be combined using control flow constructs and bindings.

Computation expressions are derived from , which in turn represent programming implementation of monads, a mathematical concept in .

Computation expressions are usually implemented by a developer as a set of functions, while the F# development environment provides the means of indirect calling of these functions with a variety of keywords and other syntactic constructs (see ).

Further reading:
F# Programming/Computation Expressions on WikiBooks
Computation Expressions on MSDN
F# 2.0 Language Specification/Computation Expressions

134 questions
1
vote
1 answer

meaning of "This control construct may only be used if the computation expression builder defines a 'Zero' method" with F#

I have this code: Ok stringBuffer { let r = get some list.... match r with | [] -> "no active tasks" | r -> String.Join("\n", r) } with stringBuffer defined as: [] module StringBuffer = type StringBuffer =…
Thomas
  • 10,933
  • 14
  • 65
  • 136
1
vote
2 answers

Is there a way to make different implementation of do! and let! in a computation expression?

I need a different behavior for do! and let! in my custom computation expression. I try to achieve this in the following way: type FooBuilder() = class member b.Bind<'T, 'U>(x:'T, f:unit->'U):'U = failwith "not implemented" //do! implementation …
andrey.ko
  • 852
  • 1
  • 7
  • 17
1
vote
2 answers

Are list comprehensions with for and yield! tail-recursive in F#?

I wrote this little function, I'll repeat it here for ease-of-reference: /// Take a list of lists, go left-first, and return each combination, /// then apply a function to the resulting sublists, each length of main list let rec nestedApply f acc…
Abel
  • 56,041
  • 24
  • 146
  • 247
1
vote
0 answers

Computation expression custom operation with multiple parameters

I am creating a DSL for C4 model diagrams. 1st stab at it is here I decided it would make more sense to separate software concepts and diagram. What this means is the position on the canvas only need be assigned when creating the diagram. So when I…
Devon Burriss
  • 2,497
  • 1
  • 19
  • 21
1
vote
2 answers

Why can't I use match! in a computation expression yet?

Starting with F# 4.5, the match! keyword allows you to inline a call to another computation expression and pattern match on its result As far as I can tell, I am using F# 4.5. However, VS2017 does not like the following code let someAsyncIntOption…
Chechy Levas
  • 2,206
  • 1
  • 13
  • 28
1
vote
1 answer

In the following computation expression, for/while do not loop

I have the following computation expression. The problem is that For/While do not loop. type Internal<'T> = Internal of option<'T> type Maybe<'T> = Maybe of Lazy< Internal<'T> > type Delayed<'T> = Delayed of (unit -> Internal<'T>) let run…
Sasha Babaei
  • 455
  • 3
  • 8
1
vote
1 answer

Cannot define a computation expression custom "condition" operator

I am having serious difficulty coming up with a definition for condition in the following code. Hoping for an example and insight: // a computation expression builder class type Builder() = . . . [
Sasha Babaei
  • 455
  • 3
  • 8
1
vote
1 answer

Re-assign a variable in a function with computation expression

Based on the question How can I re-assign a variable in a function in Haskell?, there is a haskell solution to change the total congo elephant count Congo 0 in a function: main' :: StateT Congo IO () main' = do printElephant function 2 …
MiP
  • 5,846
  • 3
  • 26
  • 41
1
vote
1 answer

How do sequence expressions and polymorphic recursion play together?

This project really is a source of questions for me. I already learned about polymorphic recursion and I understand why it is a special case and therefore F# needs full type annotations. For regular functions I might need some fiddeling but usually…
1
vote
2 answers

Embed workflow/monad in another workflow/monad?

For example, I have a workflow for logging, and I want to use async in the logging workflow. How to call logging's bind in async workflow? log { async { let! a = .... // Need to do logging here. let! b = .... // Need to do async…
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
1
vote
0 answers

Error propagating monad computational expression

I'm trying to write a computational expression that effectively threads warnings and errors from all dependent let! arguments and combines them with a result of a given calculation. The problem is getting the error list from all used let! to…
akara
  • 396
  • 2
  • 7
1
vote
1 answer

query expression self-join

How can I change this query expression's join clause so that I don't have to wrap parent.ID in an option just to join on a candidate child.ParentID which might be None? query { for parent in d.People do join child in d.People on (Some…
Jason Kleban
  • 20,024
  • 18
  • 75
  • 125
1
vote
2 answers

F# Computation Expressions and Return statement

On the Try F# website they give an example of a computation expression: type Age = | PossiblyAlive of int | NotAlive type AgeBuilder() = member this.Bind(x, f) = match x with | PossiblyAlive(x) when x >= 0 && x <= 120 -> f(x) …
Tahir Hassan
  • 5,715
  • 6
  • 45
  • 65
1
vote
2 answers

Is it necessary to use else branch in async expressions?

I want to write the following code: let someAsync () = async { if 1 > 2 then return true // Error "this expression is expected to have type unit ..." // I want to place much code here return false } F# for some reason thinks that I…
Rustam
  • 1,766
  • 18
  • 34
1
vote
1 answer

How to make f# query expressions reusable?

i am trying to get accustomed with f# query expressions. First of all, they are definitely eager, because: let arr = [1; 2; 3] let q = query { for item in arr do select item; count };; val arr : int list = [1; 2; 3] val q : int = 3 I want to reuse…
Rustam
  • 1,766
  • 18
  • 34
1 2 3
8
9