3 people like it.

Monad Bind 1 of 3

After watching this clip (http://www.youtube.com/watch?v=ZhuHCtR3xq8) on Youtube featuring Brian Beckman I wanted to try to sketch Brian's main argument that Monads' main purpose is function composition. I will post my sketch to http://rodhern.wordpress.com/2014/02/ . These snippets are the companion examples to the blog post.

Link to blog post

1: 
2: 
// Script example (1 of 3) for Monad blog post
// at http://rodhern.wordpress.com/2014/02/

Types

1: 
2: 
3: 
4: 
5: 
6: 
type A = int
type S = float
type B = S
type T = DateTime
type C = T
type U = string

Functions

1: 
2: 
3: 
let f (noWeeks: A): S = float (7 * noWeeks)
let g (noDays: B): T = today.AddDays noDays
let h (date: C): U = date.ToShortDateString ()

Function composition

1: 
2: 
3: 
4: 
5: 
6: 
let fghExplicit = fun x -> h (g (f (x)))

let compose (fun1, fun2) = fun arg -> fun2 (fun1 (arg))

let fghOne = compose(compose(f,g),h)
let fghTwo = compose(f,compose(g,h))

The results

1: 
2: 
3: 
let fghExplicitResult = fghExplicit myInputParam
let fghOneResult = fghOne myInputParam
let fghTwoResult = fghTwo myInputParam
type A = int

Full name: Script.A
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
type S = float

Full name: Script.S
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
type B = S

Full name: Script.B
type T = DateTime

Full name: Script.T
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
DateTime()
   (+0 other overloads)
DateTime(ticks: int64) : unit
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
   (+0 other overloads)
type C = T

Full name: Script.C
type U = string

Full name: Script.U
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val f : noWeeks:A -> S

Full name: Script.f
val noWeeks : A
val g : noDays:B -> T

Full name: Script.g
val noDays : B
val today : DateTime

Full name: Script.today


 Today's date and time for use in examples below.
DateTime.AddDays(value: float) : DateTime
val h : date:C -> U

Full name: Script.h
val date : C
DateTime.ToShortDateString() : string
val fghExplicit : x:A -> U

Full name: Script.fghExplicit
val x : A
val compose : fun1:('a -> 'b) * fun2:('b -> 'c) -> arg:'a -> 'c

Full name: Script.compose
val fun1 : ('a -> 'b)
val fun2 : ('b -> 'c)
val arg : 'a
val fghOne : (A -> U)

Full name: Script.fghOne
val fghTwo : (A -> U)

Full name: Script.fghTwo
val fghExplicitResult : U

Full name: Script.fghExplicitResult
val myInputParam : int

Full name: Script.myInputParam


 The test parameter I will use is "2" as in "two weeks from now".
val fghOneResult : U

Full name: Script.fghOneResult
val fghTwoResult : U

Full name: Script.fghTwoResult
Raw view Test code New version

More information

Link:http://fssnip.net/lB
Posted:10 years ago
Author:Robert Nielsen
Tags: monad , bind , example , learning f#