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
3
votes
1 answer

Why does specialising the type of a monad lead to an error?

The following computation sequence works without error: type Monad_1 () = member M.Bind (so : 'T option, bf : 'T -> 'T option) : 'T option = match so with | Some s -> bf s | None -> None member M.Delay (df : unit ->…
Shredderroy
  • 2,860
  • 2
  • 29
  • 53
3
votes
1 answer

F# workflow builder for Rx

There's a nice F# workflow builder for Rx here: http://blogs.msdn.com/b/dsyme/archive/2011/05/30/nice-f-syntax-for-rx-reactive-extensions.aspx I've been trying to make a Using implementation for the workflow but keep banging my head against the…
Bent Rasmussen
  • 5,538
  • 9
  • 44
  • 63
2
votes
2 answers

F# Nested Computation Expression with Custom Operator

I am creating a DSL for modeling, and I would like to be able to create a Settings builder with two Custom Operations: Buffer and Constraint, which themselves are Computation Expressions. The reason for this is that the domain has terms that are…
Matthew Crews
  • 4,105
  • 7
  • 33
  • 57
2
votes
1 answer

How do you pass/set byref arguments within a computation expression?

This F# code is failing to compile: let DoubleString (str : string) = str + str let ExampleSetString (str : string byref) = async { str <- DoubleString str return 1 } I get the following error message on the line str <-…
Kurren
  • 827
  • 1
  • 9
  • 18
2
votes
1 answer

F# too many calls to nested Computation Expression

This questions is an evolution of this question. I am trying to find out why when I run State.exec on the CE that I appear to be getting undesired nesting behavior of the CEs. It seems to be calling them many times. Here is what I have: type…
Matthew Crews
  • 4,105
  • 7
  • 33
  • 57
2
votes
1 answer

FSharp Computation Expression: Cannot reference binding value in custom operation

I am trying to make a builder using FSharp Computation Expression, but get error FS0039: type UpdatebBuilder() = member this.Yield (x) = x member this.Return (x) = x member this.Bind (x, cont) = cont(x) member this.Quote (x) = x …
Chen
  • 1,654
  • 2
  • 13
  • 21
2
votes
1 answer

F# program flow question with Async and Option

I'm trying to wrap my head around how to accomplish the following task in F#. The following is a simplified C# pseudocode equivalent I'm looking to replicate. var x = await GetXAsync(); if (x == null) return "not found"; var y = await…
Brian Vallelunga
  • 9,869
  • 15
  • 60
  • 87
2
votes
0 answers

How to implement computaion expression with custom operations and statements execution

I'm trying to implement diagnostics computation expression to log activities and and events using System.Diagnostics.DiagnosticSource NuGet. But I can't figure out how my computation expression can execute regular statements between calls to custom…
Andrii
  • 1,081
  • 1
  • 11
  • 24
2
votes
1 answer

F# Computation Expression to build state and defer execution

I am looking to build a computation expression where I can express the following: let x = someComputationExpression { do! "Message 1" printfn "something 1" do! "Message 2" printfn "something 2" do! "Message 3" printfn…
Stuart
  • 5,358
  • 19
  • 28
2
votes
1 answer

Type inference error in computation expression

type Identity<'T> = Identity of 'T type IdentityBuilder() = member __.Bind (Identity x) (k : 'a -> Identity<'b>) = k x member __.Return x = Identity x let identity = new IdentityBuilder() let three = Identity 3 let four = Identity 4 let…
fghzxm
  • 1,185
  • 7
  • 19
2
votes
2 answers

F# computation expressions: Can one be used to simplify this code?

I have recently started using computation expressions to simplify my code. So far the only useful one for me is the MaybeBuilder, defined thusly: type internal MaybeBuilder() = member this.Bind(x, f) = match x with | None ->…
Chechy Levas
  • 2,206
  • 1
  • 13
  • 28
2
votes
2 answers

Computation Expression for constructing complex object graph

Given the following types: type Trip = { From: string To: string } type Passenger = { Name: string LastName: string Trips: Trip list } I'm using the following builders: type PassengerBuilder() = member this.Yield(_) =…
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
2
votes
1 answer

When to implement `Zero` member in Computation Expression?

Why can't I use pattern matching function in computation expressions without implement Zero member? For example, could somepone explain why it permits allowing pattern matching expression but not pattern matching function? type MaybeBuilder() = …
MiP
  • 5,846
  • 3
  • 26
  • 41
2
votes
4 answers

How to Build an Accumulating Either Builder

I want to build an computational expression for either expressions. That is simple enough type Result<'TSuccess> = | Success of 'TSuccess | Failure of List type Foo = { a: int b: string c: bool } type EitherBuilder () = …
robkuz
  • 9,488
  • 5
  • 29
  • 50
2
votes
1 answer

How best to catch missing let!, do!, return and return! in computation expressions in F#

I love computation expressions, but I make simple mistakes like forgetting the return keyword or the ! on expressions like let! and return!, or I simply forget to write the do!. This happens much with state monads where I tend to forget about the…
user519985
  • 71
  • 3
1 2 3
8 9