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
5
votes
2 answers

Monadic operations on Choice<'T1,'T2>

I could not find an object choice in the standard libraries, that allows me to write let safeDiv (numer : Choice) (denom : Choice) = choice { let! n = numer let! d = denom return! if d = 0 …
fghzxm
  • 1,185
  • 7
  • 19
5
votes
2 answers

How to implement the List Monad (computation expression) with a condition?

I'm trying to understand how to use F# computation expressions and it certainly puzzles me. The following example makes some amount of sense to me. type ListMonad() = member o.Bind( (m:'a list), (f: 'a -> 'b list) ) = List.collect f m …
jks612
  • 1,224
  • 1
  • 11
  • 20
5
votes
1 answer

Using custom computation expression operator inside match statement

Right now I'm experimenting with F# computation expressions. General idea is to return control mechanism to drive actions executed after each step of recursive function call build from computation expression. Whole example can be seen here. Using…
Bartosz Sypytkowski
  • 7,463
  • 19
  • 36
5
votes
2 answers

Recursive functions in computation expressions

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…
PetPaulsen
  • 3,442
  • 2
  • 22
  • 33
5
votes
0 answers

F# computation expressions and the Task Parallel Library

I've been working a lot with F# lately, and recently wrote a little MVC application with a C# web project and an F# class library, the latter of which contained the bulk of the code. I wanted to use the async controllers feature of the later…
Richiban
  • 5,569
  • 3
  • 30
  • 42
5
votes
2 answers

F#: generated IL code for seq{} vs other computational workflows

When I compare IL code that F# generates for seq{} expressions vs that for user-defined computational workflows, it's quite obvious that seq{} is implemented very differently: it generates a state machine similar to the once C# uses for its'…
user1411900
  • 384
  • 4
  • 14
5
votes
1 answer

F# Computation Expressions: How to use `for` to return a `seq`?

I'm writing a computation expression that is essentially implementing a State monad and I'm trying to use for expression. I can use the boilerplate function forLoop or even MBuilder.For(), and they all return a nice M, _> which can be…
Be Brave Be Like Ukraine
  • 7,596
  • 3
  • 42
  • 66
4
votes
1 answer

Implementing let! ... and! ... in a custom Computation Expression

I want to add support to my Computation Expression builder for this construct: let f = foo { let! x = foo { return 1 } and! y = foo { return 2 } return x + y } The compiler says I must…
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
4
votes
1 answer

how can I combine / compose computation expressions, in F#?

This is not for a practical need, but rather to try to learn something. I am using FSToolKit's asyncResult expression which is very handy and I would like to know if there is a way to 'combine' expressions, such as async and result here, or does a…
Thomas
  • 10,933
  • 14
  • 65
  • 136
4
votes
1 answer

what is 'and!' in the validation computation expression with FsToolkit.ErrorHandling, in F#

when using FsToolkit.ErrorHandling and looking at this example: testCase "Happy Path Result" <| fun () -> let actual : Validation = validation { let! a = Ok 3 and! b = Ok 2 and! c = Ok 1 …
Thomas
  • 10,933
  • 14
  • 65
  • 136
4
votes
2 answers

How to Access a Value from a Builder using Custom Operation in a Computation Expression

I have a Computational Expression Builder which receives a value during construction type SomeBuilder<'e> (e: 'e) = member this.Bind(x, fn) = ... member this.Return x = ... member this.ReturnFrom x = ... let buildSome v =…
robkuz
  • 9,488
  • 5
  • 29
  • 50
4
votes
1 answer

Return started Async in F# function from calling a non-async Func from C#?

Let's say I want to call, from F#, this C# function: public static class Foo { public Bar Baz() { ... } } The problem is this function is CPU intensive and I don't want to block on it. Unfortunately the C# library doesn't have an…
knocte
  • 16,941
  • 11
  • 79
  • 125
4
votes
2 answers

How to implement non-nested exception handling on each step in an F# task computation expression?

Given the F# task computation expression I can write:- task { try let! accessToken = getAccessTokenAsync a b try let! resource = getResourceAsync accessToken uri // do stuff with | ex…
Bellarmine Head
  • 3,397
  • 2
  • 22
  • 31
4
votes
1 answer

How can I implement a Stateful Builder with CustomOperations and some let

I am trying to get my head around defining a stateful builder and I can't get around some compiler errors type Movement = | Left of int | Right of int type MovementState = Movement list -> Movement list type MovementBuilder () = member…
robkuz
  • 9,488
  • 5
  • 29
  • 50
4
votes
2 answers

F# query expressions yield

I am learning F# and now am reading about computation expressions and query expressions to use with SQL type providers. I was doing some simple tasks and at some point needed to concatenate (Union) 2 queries, my first thought, after reading about…
Luiso
  • 4,173
  • 2
  • 37
  • 60
1 2
3
8 9