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

How can I call bind on a computation expression without the let keyword?

Say I have this: MyComputationExpression { let! addr = Salesrecord.Address let! name = Salesrecord.Name return (name + " " + addr) } Is there any way to "unwrap" (call the bind method on) the address and name on the fly? Sort of…
Kurren
  • 827
  • 1
  • 9
  • 18
3
votes
1 answer

Issue with yield in nested workflow

I'm trying to write my own Either builder as part of my quest to learn computation expressions in f#, but I have hit a wall with what I think is issue with Combine method. My code so far: type Result<'a> = | Failure | Success of 'a type…
DevNewb
  • 841
  • 7
  • 19
3
votes
1 answer

Refactor algorithm as computation expression?

This question contains spoilers for those who haven't finished problem 61 of Project Euler. I wrote an answer to the problem that was imperative so I set out to make a more generic, functional answer. I succeeded but am now trying to figure out how…
jks612
  • 1,224
  • 1
  • 11
  • 20
3
votes
2 answers

Partially deferred computation builder

I'm trying to work out how to use a computation builder to represent a deferred, nested set of steps. I've got the following so far: type Entry = | Leaf of string * (unit -> unit) | Node of string * Entry list * (unit -> unit) type…
Clint
  • 6,133
  • 2
  • 27
  • 48
3
votes
1 answer

Problem with computational workflow

trying to follow example in the expert f# book, and having an issue with the workflows...the code is as follows: type Attempt<'a> = option<'a> let succeed x = Some (x) let fail = None let bind p rest = match p with | None ->…
Alex
  • 2,342
  • 1
  • 18
  • 30
3
votes
1 answer

Computation expressions vs applicative functors and what not

Not entirely sure the title describes it ok, but I do have about the following code: paket.dependencies: source https://www.nuget.org/api/v2 nuget fsharpx.extras nuget mongodb.driver some.fsx: #r…
Helge Rene Urholm
  • 1,190
  • 6
  • 16
3
votes
1 answer

Zero and Combine in Computation Expression

I have the following computation expression builder: type ExprBuilder() = member this.Return(x) = Some x let expr = new ExprBuilder() I understand the purpose of methods Return, Zero and Combine, but I don't understand what is the…
sgnsajgon
  • 664
  • 2
  • 13
  • 56
3
votes
1 answer

the mechanics of let! in computation expressions

I am currently working on the computation expression series of the fabulous fsharpforfunandprofit website and I have a question regarding the lesson 4 "wrapped type" of the computation series. I have tried reading a bit further , but there's an…
Arthis
  • 2,283
  • 21
  • 32
3
votes
3 answers

F# "Stateful" Computation Expression

I'm currently learning F# and hitting a few stumbling blocks; I think a lot of it is learning to think functionally. One of the things I'm learning at the moment are computation expressions, and I want to be able to define a computation expression…
Clint
  • 6,133
  • 2
  • 27
  • 48
3
votes
3 answers

Retry monad and Zero construct

I am trying to use the Retry Monad I took from our beloved stack overflow: type RetryBuilder(max, sleep : TimeSpan) = member x.Return(a) = a member x.Delay(f) = f member x.Zero() = failwith "Zero" member x.Run(f) = …
Klark
  • 8,162
  • 3
  • 37
  • 61
3
votes
2 answers

What's wrong with my logger computational expression?

Below the computation expression I'm trying to implement. The value is wrapped in a tuple where the second item of the tuple is a list of strings representings the log entries along the way. type LoggerBuilder() = member this.Bind(vl : 'a *…
OnesimusUnbound
  • 2,886
  • 3
  • 30
  • 40
3
votes
2 answers

Wrangling TryWith in Computation expressions

(Having failed to 'grok' FParsec, I followed the advice I read somewhere and started trying to write a little parser myself. Somehow I spotted what looked like a chance to try and monadify it, and now I have N problems...) This is my 'Result' type…
Benjol
  • 63,995
  • 54
  • 186
  • 268
3
votes
2 answers

How do you create an F# workflow that enables something like single-stepping?

I'd like to create a builder that builds expressions that returns something like a continuation after each step. Something like this: module TwoSteps = let x = stepwise { let! y = "foo" printfn "got: %A" y let! z = y + "bar" …
James Moore
  • 8,636
  • 5
  • 71
  • 90
3
votes
2 answers

Why does this F# computation expression give a warning?

This code: type Result = Success of string type Tracer() = member x.Bind(p: Result, rest: (string -> Result)) = match p with | Success s -> rest s let tracer = new Tracer() let t = tracer { let! x = Success "yes!" let! y = Success…
James Moore
  • 8,636
  • 5
  • 71
  • 90
3
votes
1 answer

f# Computation expressions for code generation

While one finds a few examples on how to do compositional recursive descent Parsers with f# computation expressions, I tried to use them for the opposite. To create easily readable code to generate (c++) source files from some XML data. Yet, I am…
user2173833
  • 264
  • 1
  • 5
1 2 3
8 9