0 people like it.

yin-yang puzzle in F#

yin-yang puzzle, based on https://groups.google.com/forum/#!msg/comp.lang.scheme/Fysq_Wplxsw/awxEZ_uxW20J

 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: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
// yin-yang puzzle, based on https://groups.google.com/forum/#!msg/comp.lang.scheme/Fysq_Wplxsw/awxEZ_uxW20J

type Cont<'T, 'R> = ('T -> 'R) -> 'R

let return' : 'T -> Cont<'T, 'R> =
    fun v k -> k v

let bind : Cont<'T, 'R> -> ('T -> Cont<'V, 'R>) -> Cont<'V, 'R> = 
    fun cont f k -> cont (fun v -> f v k)

let callCC : (('T -> Cont<'V, 'R>) -> Cont<'T, 'R>) -> Cont<'T, 'R> =
    fun f k -> f (fun v -> (fun _ -> k v)) k 

type ContBuilder() = 
    member self.Return v = return' v
    member self.Bind(cont, f) = bind cont f
    member self.ReturnFrom cont = cont

let cont = new ContBuilder()

type Rec<'R> = Rec of K<'R>
and K<'R> = Rec<'R> -> Cont<Rec<'R>, 'R>

let yin () = 
    cont {
        let! k = callCC (fun k -> cont { return Rec k })
        printfn ""
        return k
    }
let yang () =
    cont {
        let! k = callCC (fun k -> cont { return Rec k })
        printf "*"
        return k
    } 

let yinyang () = 
    cont {
        let! (Rec k) = yin()
        let! k' = yang()
        return! k k'
    }

yinyang () (fun _ -> ())
(*
 **
 ***
 ****
 *****
 ******
 *******
 ..........*)
val return' : v:'T -> k:('T -> 'R) -> 'R

Full name: Script.return'
type Cont<'T,'R> = ('T -> 'R) -> 'R

Full name: Script.Cont<_,_>
val v : 'T
val k : ('T -> 'R)
val bind : cont:Cont<'T,'R> -> f:('T -> Cont<'V,'R>) -> k:('V -> 'R) -> 'R

Full name: Script.bind
val cont : Cont<'T,'R>
val f : ('T -> Cont<'V,'R>)
val k : ('V -> 'R)
val callCC : f:(('T -> Cont<'V,'R>) -> Cont<'T,'R>) -> k:('T -> 'R) -> 'R

Full name: Script.callCC
val f : (('T -> Cont<'V,'R>) -> Cont<'T,'R>)
Multiple items
type ContBuilder =
  new : unit -> ContBuilder
  member Bind : cont:Cont<'b,'c> * f:('b -> Cont<'d,'c>) -> Cont<'d,'c>
  member Return : v:'e -> Cont<'e,'f>
  member ReturnFrom : cont:'a -> 'a

Full name: Script.ContBuilder

--------------------
new : unit -> ContBuilder
val self : ContBuilder
member ContBuilder.Return : v:'e -> Cont<'e,'f>

Full name: Script.ContBuilder.Return
val v : 'e
member ContBuilder.Bind : cont:Cont<'b,'c> * f:('b -> Cont<'d,'c>) -> Cont<'d,'c>

Full name: Script.ContBuilder.Bind
val cont : Cont<'b,'c>
val f : ('b -> Cont<'d,'c>)
member ContBuilder.ReturnFrom : cont:'a -> 'a

Full name: Script.ContBuilder.ReturnFrom
val cont : 'a
val cont : ContBuilder

Full name: Script.cont
Multiple items
union case Rec.Rec: K<'R> -> Rec<'R>

--------------------
type Rec<'R> = | Rec of K<'R>

Full name: Script.Rec<_>
Multiple items
type K<'R> = Rec<'R> -> Cont<Rec<'R>,'R>

Full name: Script.K<_>

--------------------
type K<'R> = Rec<'R> -> Cont<Rec<'R>,'R>

Full name: Script.K<_>
type K<'R> = Rec<'R> -> Cont<Rec<'R>,'R>

Full name: Script.K<_>
val yin : unit -> Cont<Rec<'a>,'a>

Full name: Script.yin
val k : Rec<'a>
val k : (Rec<'a> -> Cont<Rec<'a>,'a>)
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val yang : unit -> Cont<Rec<'a>,'a>

Full name: Script.yang
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
val yinyang : unit -> Cont<Rec<'a>,'a>

Full name: Script.yinyang
val k : K<'a>
val k' : Rec<'a>
Raw view Test code New version

More information

Link:http://fssnip.net/7Tp
Posted:7 years ago
Author:NIck Palladinos
Tags: callcc , continuations