Skip to content

Instantly share code, notes, and snippets.

@SteveGilham
SteveGilham / gist:b882202f5682682e4541
Created April 26, 2015 14:15
Parse repeating elements (continuation based)
let rec ( ^* ) (generator: ('x option -> continuation) -> continuation, test)
(consumer: 'x list -> 'x option -> continuation) =
generator (function Some x -> if test x
then (generator, test) ^* (fun l -> consumer(x::l))
else consumer [] (Some x)
| None -> consumer [] None);;
@SteveGilham
SteveGilham / gist:fbe5ca8d532dd7ac7e1b
Created April 26, 2015 14:14
Continuation based lexer (partial)
type LispLex = Bra | Ket | Dot | Str of string | Num of int | Symbol of string
let alpha c = ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))
let digit c = (c >= '0') && (c <= '9')
let eqChar c c' = c = c'
let l2s l = String(List.toArray l)
let ( ^| ) p q = fun x -> (p x) || (q x)
// Read a symbol from the input
let readSymbol s char g =
(lexGetChar s, alpha ^| digit ^| eqChar '_') ^*
@SteveGilham
SteveGilham / gist:a649ae8e1c3f054314a6
Created April 26, 2015 14:14
Continuation based computation
type continuation = K of (unit -> unit)
step1: continuation -> continuation
end: continuation
program = step1 end : continuation
Return: t -> M t
Fail: M t
Bind: (t1 -> M t2) -> M t1 -> M t2
@SteveGilham
SteveGilham / gist:a656cb369e6f14c253f1
Created April 26, 2015 14:11
Monadic Bind as continuation
m >>= fun value1 ->
E1[value1] >>= fun value2 ->
E2[value2]
@SteveGilham
SteveGilham / gist:e3cb6e5801f0804cbfb7
Created April 26, 2015 14:11
Syntactic sugar (as in let!)
value1 := m;
value2 := E1[value1]
E2[value2]
@SteveGilham
SteveGilham / gist:ec4bc63b293169995b46
Created April 26, 2015 14:10
Hypothetical typeclass definition of monad Bind
let __binder__<'T, 'U, 'M when 'M : (static member bind : ('T -> 'U 'M ) -> 'T 'M -> 'U 'M)> (x: 'T 'M) (f : 'T -> 'U 'M) =
x |> 'M.bind f;;
let (>>=) = __binder__;;
let (>>) m1 m2 = m1 >>= fun () -> m2;;
let Apply m1 m2
= m1 >>= fun f ->
m2 >>= fun x ->
Return (f x);;
@SteveGilham
SteveGilham / gist:a58530b5c6432c5ce0b9
Created April 26, 2015 14:07
Function to function over monad
let Lift f m =
m >>= fun x ->
Return (f x);;