2 people like it.

Sum of Squares Monoid

This snippet defines a computation builder that sums the squares of float values. It includes Combine, Zero, Yield, Delay, and For operations.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
/// Define SumOfSquares computation builder
type SumOfSquaresMonoid() =
  /// Combine two values
  /// sm.Combine («cexpr1», b.Delay(fun () -> «cexpr2»))
  member sm.Combine(a,b) = a + b
  /// Zero value
  /// sm.Zero()
  member sm.Zero() = 0.0
  /// Return a value 
  /// sm.Yield expr
  member sm.Yield(a) = a
  /// Delay a computation
  /// sm.Delay (fun () -> «cexpr»))
  member sm.Delay f = f()
  /// For loop
  /// sm.For (expr, (fun pat -> «cexpr»))
  member sm.For(e, f) =
    Seq.fold(fun s x -> sm.Combine(s, f x)) (sm.Zero()) e

// Create an instance of each such monoid object
let sosm = new SumOfSquaresMonoid()

// Build a SumOfSquaresMonoid value(function)
let sumOfSquares x = sosm {for x in [1.0 .. 0.2 .. x] do yield x * x}

// Evaluation
sumOfSquares 10.2

// Result
// val it : float = 1819.84
Multiple items
type SumOfSquaresMonoid =
  new : unit -> SumOfSquaresMonoid
  member Combine : a:float * b:float -> float
  member Delay : f:(unit -> 'b) -> 'b
  member For : e:seq<'a> * f:('a -> float) -> float
  member Yield : a:'c -> 'c
  member Zero : unit -> float

Full name: Script.SumOfSquaresMonoid


 Define SumOfSquares computation builder


--------------------
new : unit -> SumOfSquaresMonoid
val sm : SumOfSquaresMonoid
member SumOfSquaresMonoid.Combine : a:float * b:float -> float

Full name: Script.SumOfSquaresMonoid.Combine


 Combine two values
 sm.Combine («cexpr1», b.Delay(fun () -> «cexpr2»))
val a : float
val b : float
member SumOfSquaresMonoid.Zero : unit -> float

Full name: Script.SumOfSquaresMonoid.Zero


 Zero value
 sm.Zero()
member SumOfSquaresMonoid.Yield : a:'c -> 'c

Full name: Script.SumOfSquaresMonoid.Yield


 Return a value
 sm.Yield expr
val a : 'c
member SumOfSquaresMonoid.Delay : f:(unit -> 'b) -> 'b

Full name: Script.SumOfSquaresMonoid.Delay


 Delay a computation
 sm.Delay (fun () -> «cexpr»))
val f : (unit -> 'b)
member SumOfSquaresMonoid.For : e:seq<'a> * f:('a -> float) -> float

Full name: Script.SumOfSquaresMonoid.For


 For loop
 sm.For (expr, (fun pat -> «cexpr»))
val e : seq<'a>
val f : ('a -> float)
module Seq

from Microsoft.FSharp.Collections
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State

Full name: Microsoft.FSharp.Collections.Seq.fold
val s : float
val x : 'a
member SumOfSquaresMonoid.Combine : a:float * b:float -> float


 Combine two values
 sm.Combine («cexpr1», b.Delay(fun () -> «cexpr2»))
member SumOfSquaresMonoid.Zero : unit -> float


 Zero value
 sm.Zero()
val sosm : SumOfSquaresMonoid

Full name: Script.sosm
val sumOfSquares : x:float -> float

Full name: Script.sumOfSquares
val x : float
Raw view Test code New version

More information

Link:http://fssnip.net/eq
Posted:12 years ago
Author:Joel Huang
Tags: computation builder , seq