Created
July 17, 2011 20:13
-
-
Save palladin/1088009 to your computer and use it in GitHub Desktop.
Hughes's FuncList
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// for more info http://www.cs.tufts.edu/~nr/cs257/archive/john-hughes/lists.pdf | |
type FuncList<'a> = 'a list -> 'a list | |
// Monoid comprehension | |
type FuncListBuilder() = | |
member self.Combine (first : FuncList<'a>, second : FuncList<'a>) : FuncList<'a> = (first << second) | |
member self.Zero() : FuncList<'a> = id | |
member self.Yield (value : 'a) : FuncList<'a> = fun tail -> value :: tail | |
member self.YieldFrom (value : FuncList<'a>) : FuncList<'a> = value | |
member self.Delay ( f : unit -> FuncList<'a>) : FuncList<'a> = (fun tail -> f () tail) | |
let funcList = new FuncListBuilder() | |
// example | |
let rec reverse list = | |
match list with | |
| [] -> funcList.Zero() | |
| x :: xs -> funcList { yield! reverse xs; yield x } | |
reverse [1..10] [] // returns [10; 9; 8; 7; 6; 5; 4; 3; 2; 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment