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

(How) can I make this monadic bind tail-recursive?

I have this monad called Desync - [] module DesyncModule = /// The Desync monad. Allows the user to define in a sequential style an operation that spans /// across a bounded number of events. Span is bounded because I've yet to…
Bryan Edds
  • 1,696
  • 12
  • 28
10
votes
1 answer

Intuition behind calling Zero for else branch of if..then construct in computation expressions

The msdn documentation for the Zero method in computation expressions states that Called for empty else branches of if...then expressions in computation expressions. Assume we're using an identity computation builder which does not have Zero…
Tejas Sharma
  • 3,420
  • 22
  • 35
10
votes
2 answers

StackOverflow in continuation monad

Using the following continuation monad: type ContinuationMonad() = member this.Bind (m, f) = fun c -> m (fun a -> f a c) member this.Return x = fun k -> k x let cont = ContinuationMonad() I fail to see why the following gives me a stack…
David Grenier
  • 1,098
  • 6
  • 17
9
votes
2 answers

Implement Bind in a Custom Computation Expression

I'm trying to learn a bit more about F#'s computation expressions by implementing one of my own. However, I've hit a stumbling block with relation to the Bind method. Here's what I've got so far: type public op<'a> = Op of ('a list -> 'a list) let…
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
8
votes
1 answer

F# saying value not defined in Computation Expression

I've been working on a State Monad with F# Computation Expression and I'm trying to also utilize Custom Operations. I'm getting some weird behavior that does not make sense. The compiler is reporting that a value does not exist when it was declared…
Matthew Crews
  • 4,105
  • 7
  • 33
  • 57
8
votes
1 answer

Does ReasonML have an equivalent of F#'s Computation Expressions?

In F# we have Computation Expressions, which can reduce boiler-plate and nesting when working in various computational contexts (async, optionals and so on). Does ReasonML have an equivalent of this? If so, what is the syntax?
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
8
votes
1 answer

What is the role of `while`-loops in computation expressions in F#?

If you define a While method of the builder-object, you can use while-loops in your computation expressions. The signature of the While method is: member b.While (predicate:unit->bool, body:M<'a>) : M<'a> For comparison, the signature of the For…
Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
8
votes
1 answer

async computation doesn't catch OperationCancelledException

I'm trying to make an asynchronous web request to a URL that will return if the request takes too long. I'm using the F# asynchronous workflow and the System.Net.Http library to do this. However, I am unable to catch the…
WiseGuyEh
  • 18,584
  • 1
  • 20
  • 20
8
votes
3 answers

Computation expressions for a Haskell programmer

I'm looking to learn F#, but one thing that's confusing to me is the computation expression (do-notation??) syntax and desugaring. In haskell, you have a very simple Monad typeclass and rules for desugaring do-notation into bind and return. There's…
nimish
  • 4,755
  • 3
  • 24
  • 34
7
votes
0 answers

Why are f# custom operation not allowed into control flow statement?

Using a custom operation of a f# computation expression inside any control flow statement fails typechecking with error FS3086: A custom operation may not be used in conjunction with 'use', 'try/with', 'try/finally', 'if/then/else' or 'match'…
pqnet
  • 6,070
  • 1
  • 30
  • 51
7
votes
1 answer

Recursive computation expressions

In a previous question I was told how to rewrite my computation expressions so it uses tail recursion. I rewrote my code but still got a StackOverflowException. To locate the problem I wrote some small code using a state monad (taken from this blog…
PetPaulsen
  • 3,442
  • 2
  • 22
  • 33
7
votes
1 answer

F# computation expression transparent state passing with Bind

I have the following code that try to read a possibly incomplete data (image data, for example) from a network stream using usual MaybeBuilder: let image = maybe { let pos = 2 //Initial position skips 2 bytes of packet ID let! width, pos =…
Dzugaru
  • 322
  • 3
  • 8
6
votes
1 answer

Composing quoted functions using computation expressions

I've looked around and struggled to get an answer to this; I'm sure there's an obvious answer but I just can't seem to find it; or I've hit a limitation of quotations that I can't pass when used with computation expressions. Basically I want to work…
akara
  • 396
  • 2
  • 7
6
votes
2 answers

Custom computation expressions in F#

I've been toying with monads in F# (aka computation expressions) and I wrote this simple Identity monad: type Identity<'a> = | Identity of 'a type IdentityBuilder() = member x.Bind (Identity v) f = f(v) member x.Return v = Identity…
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
5
votes
1 answer

Continuation Passing Style Computation Expression

Can you implement CPS using computation expressions in F#? Brian McNamara's blog gives this solution: type ContinuationBuilder() = member this.Return(x) = (fun k -> k x) member this.ReturnFrom(x) = x member this.Bind(m,f) = (fun k -> m (fun…
J D
  • 48,105
  • 13
  • 171
  • 274
1
2
3
8 9