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
// Inspired by http://www.cs.utexas.edu/~wcook/Drafts/2006/MemoMixins.pdf | |
// State Monad combined with Continuation Monad (StateT Monad transformer) | |
type StateContMonad<'s, 'a, 'r> = StateContMonad of ('s -> ('s -> 'a -> 'r) -> 'r) | |
// Computation Builder | |
type StateContBuilder() = | |
member self.Return value = | |
StateContMonad (fun state k -> k state value) | |
member self.Bind(StateContMonad contStateMonad, f) = |
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
#r "FSharp.PowerPack.dll" | |
open System | |
// Generic container of 'T | |
// Also parameterized by 'TypeClass : (new : unit -> 'TypeClass) | |
// to implicit get a 'TypeClass instance (like passing the type class dictionary) | |
// The idea is to encode Type Classes with subtype polymorphism and OOP Classes | |
type Generic<'T, 'TypeClass when 'TypeClass : (new : unit -> 'TypeClass)> = interface end |
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
// Based on http://blogs.msdn.com/b/chrsmith/archive/2008/02/22/regular-expressions-via-active-patterns.aspx | |
open System | |
open System.Text.RegularExpressions | |
let (|Match|_|) (pat:string) (inp:string) = | |
let m = Regex.Match(inp, pat) in | |
if m.Success | |
then Some (List.tail [ for g in m.Groups -> g.Value ]) |
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) |
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
type FuncList<'a> = 'a list -> 'a list | |
type CPSFuncList<'a> = FuncList<'a> -> FuncList<'a> | |
// Monoid comprehension | |
type CPSFuncListBuilder() = | |
member self.Combine (first : CPSFuncList<'a>, second : CPSFuncList<'a>) : CPSFuncList<'a> = | |
(fun k -> second (fun tail -> first (fun tail' -> k tail') tail)) | |
member self.Zero() : CPSFuncList<'a> = (fun k tail -> k tail) | |
member self.Yield (value : 'a) : CPSFuncList<'a> = (fun k tail -> k (value :: tail)) | |
member self.YieldFrom (value : CPSFuncList<'a>) : CPSFuncList<'a> = value |
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
let rec iterate f value = | |
seq { yield value; | |
yield! iterate f (f value) } | |
let fixedPoint f initial = | |
iterate f initial | |
|> Seq.pairwise | |
|> Seq.pick (fun (first, second) -> | |
if first = second then Some first else None) |
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
open System.Collections | |
open System.Collections.Generic | |
// LazyList dataType + Monoid Structure | |
type LazyList<'T> = Empty | |
| Cons of 'T * (unit -> LazyList<'T>) | |
| Delay of (unit -> LazyList<'T>) | |
| Combine of LazyList<'T> * LazyList<'T> with | |
interface IEnumerable<'T> with | |
member self.GetEnumerator() = |
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
// http://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx | |
#time | |
#r "FSharp.PowerPack.dll" | |
open System | |
open System.Text | |
let format (words : seq<string>) = | |
let sb (value : string) = new StringBuilder(value) |
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
open System | |
open System.Net | |
open Microsoft.FSharp.Control.WebExtensions | |
let noiseWords = [|"a"; "about"; "above"; "all"; "along"; "also"; "although"; "am"; "an"; "any"; "are"; "aren't"; "as"; "at"; | |
"be"; "because"; "been"; "but"; "by"; "can"; "cannot"; "could"; "couldn't"; | |
"did"; "didn't"; "do"; "does"; "doesn't"; "e.g."; "either"; "etc"; "etc."; "even"; "ever"; | |
"for"; "from"; "further"; "get"; "gets"; "got"; "had"; "hardly"; "has"; "hasn't"; "having"; "he"; |
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
// http://research.microsoft.com/enus/um/people/simonpj/papers/hmap/hmap.ps | |
// Type safe conversion functions | |
let cast<'T, 'R> (v : 'T) : 'R = v :> obj :?> 'R | |
let mkT<'T, 'R> (f : 'T -> 'T) : 'R -> 'R = | |
if typeof<'T> = typeof<'R> then (fun (v : 'R) -> v |> cast |> f |> cast) else id | |
let mkQ (r : 'R) (q : 'B -> 'R) (a : 'A) : 'R = | |
if typeof<'A> = typeof<'B> then | |
a |> cast |> q | |
else r |