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

F#: Is there a way to extend the monad keyword list?

Inside an F# monad, if you say let!, the compiler translates that to a Bind member that you've defined on the monad builder. Now I see there are Query monads, as shown here on MSDN, where you can say: query { for student in db.Student do …
FSharpN00b
  • 933
  • 1
  • 8
  • 17
24
votes
2 answers

Extended computation expressions without for..in..do

What I mean by extended computation expressions is computation expressions with custom keywords defined via CustomOperation attribute. When reading about extended computation expressions, I come across very cool IL DSL by @kvb: let il =…
pad
  • 41,040
  • 7
  • 92
  • 166
23
votes
2 answers

How do you compose query expressions in F#?

I've been looking at query expressions here http://msdn.microsoft.com/en-us/library/vstudio/hh225374.aspx And I've been wondering why the following is legitimate let testQuery = query { for number in netflix.Titles do where…
Brian Rosamilia
  • 1,448
  • 14
  • 24
21
votes
1 answer

Defining new keywords in F#'s computation expression

The F# 3.0 beta contains a query {} computation expression with tons of new keywords. How can I define my own keywords in a computation builder?
forki23
  • 2,784
  • 1
  • 28
  • 42
21
votes
3 answers

Combine F# async and maybe computation expression

Say i want to return an Option while in an async workflow: let run = async { let! x = doAsyncThing let! y = doNextAsyncThing x match y with | None -> return None | Some z -> return Some <| f z …
Geoff
  • 1,634
  • 16
  • 25
17
votes
3 answers

Why do F# computation expressions require a builder object (rather than a class)?

F# computation expressions have the syntax: ident { cexpr } Where ident is the builder object (this syntax is taken from Don Syme's 2007 blog entry). In all the examples I've seen, builder objects are singleton instances, and stateless to boot. Don…
Todd Owen
  • 15,650
  • 7
  • 54
  • 52
16
votes
2 answers

How do I translate a `where T : U` generic type parameter constraint from C# to F#?

F# is giving me some trouble with its type inference rules. I'm writing a simple computation builder but can't get my generic type variable constraints right. The code that I would want looks as follows in C#: class FinallyBuilder { …
15
votes
2 answers

F# Custom Operator reporting incorrect usage in Computation Expression

I am creating a Computation Expression (CE) for simplifying the definition of Plans for modelers. I want to define functions that are only available in the CE. In this example the compiler says that the custom operations step and branch are being…
Matthew Crews
  • 4,105
  • 7
  • 33
  • 57
15
votes
4 answers

Turn list of Result into Result of list inside a computation expression?

I have a Result<'T, 'E> list that I would like to turn into a single Result<'T list, 'E> following these rules: If any Result is an Error then the result should be an Error If the result is an Error it should be the first Error in the list If every…
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
13
votes
3 answers

Is there a real-world applicability for the continuation monad outside of academic use?

(later visitors: two answers to this question both give excellent insight, if you are interested you should probably read them both, I could only except one as a limitation of SO) From all discussions I find online on the continuation monad they…
Abel
  • 56,041
  • 24
  • 146
  • 247
13
votes
4 answers

LINQ query expressions that operate on types (monads?) other than IEnumerable -- Possible uses?

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…
13
votes
4 answers

How do I write a computation expression builder that accumulates a value and also allows standard language constructs?

I have a computation expression builder that builds up a value as you go, and has many custom operations. However, it does not allow for standard F# language constructs, and I'm having a lot of trouble figuring out how to add this support. To give a…
Joel Mueller
  • 28,324
  • 9
  • 63
  • 88
11
votes
5 answers

How do I change the Rx Builder implementation to fix the stack overflow exception?

I'm trying to come up with an Rx Builder to use Reactive Extension within the F# Computation Expression syntax. How do I fix it so that it doesnt blow the stack? Like the Seq example below. And is there any plans to provide an implementation of the…
Holoed
  • 643
  • 4
  • 15
11
votes
2 answers

How to keep the stacktrace when rethrowing an exception out of catch-context?

TL;DR: how to raise a previously caught exception later on, while preserving the original exception's stacktrace. Since I think this is useful with the Result monad or computation expression, esp. since that pattern is often used for wrapping an…
Abel
  • 56,041
  • 24
  • 146
  • 247
10
votes
2 answers

How to implement delay in the maybe computation builder?

Here is what I have so far: type Maybe<'a> = option<'a> let succeed x = Some(x) let fail = None let bind rest p = match p with | None -> fail | Some r -> rest r let rec whileLoop cond body = if cond() then match…
Joh
  • 2,380
  • 20
  • 31
1
2 3
8 9