Skip to content

Instantly share code, notes, and snippets.

View t0yv0's full-sized avatar
🎯
Focusing

Anton Tayanovskyy t0yv0

🎯
Focusing
View GitHub Profile
import Data.List
primes :: [Int]
primes = 2 : 3 : 5 : filter isPrime [7, 9 ..]
suspects :: Int -> [Int]
suspects n = takeWhile (\x -> x * x <= n) primes
isPrime :: Int -> Bool
isPrime n = all (\x -> n `mod` x /= 0) (suspects n)
type Prim =
| Add
| Sub
| Mul
| Div
| Eq
| Not
type Value =
| Bool of bool
type Box<'T1,'T2> = { Value : obj }
module List =
let Box (list: list<'T>) : Box<list<obj>,'T> = { Value = list }
let Unbox (x: Box<list<obj>,'T>) : list<'T> = x.Value :?> _
type IFunctor<'T> =
abstract member Map : ('T1 -> 'T2) -> Box<'T,'T1> -> Box<'T,'T2>
let ListFunctor =
class Functor f where
fmap :: (a -> b) -> f a -> f b
instance Functor [] where
fmap = map
module DFA =
let rec nextState current token =
match current.Transitions.TryGetValue token with
| true, state -> state
| _ ->
let rec loop current next =
if Set.isEmpty current then getState next else
let min = Set.minElement current
let current = Set.remove min current
module DFA =
open System.Collections.Generic
type State<'T> =
{
IsMatch : bool
States : Set<NFA.State<'T>>
Transitions : Dictionary<'T,State<'T>>
}
module NFA =
let Compile regex =
let k = ref 0
let next () =
incr k
!k
let rec compile regex matched =
match regex with
| Choose (a, b) ->
module NFA =
[<CustomEquality>]
[<CustomComparison>]
type State<'T> =
| Match
| Branch of Pair<'T>
| Read of int * ('T -> bool) * State<'T>
member this.Key =
module NFA =
type State<'T> =
| Match
| Branch of State<'T> * State<'T>
| Read of ('T -> bool) * State<'T>
type Regex<'T> =
| Choose of Regex<'T> * Regex<'T>
| Concat of Regex<'T> * Regex<'T>
| Empty
| Filter of ('T -> bool)
| Star of Regex<'T>