Skip to content

Instantly share code, notes, and snippets.

@ebresafegaga
ebresafegaga / misc.ml
Created November 24, 2021 23:35
Random Code
let subsequence longer shorter =
let rec inner l s =
match l, s with
| l :: ls, s :: ss when l = s -> inner ls ss
| _ :: ls , _ :: _ -> inner ls shorter
| [], _ -> false
| _, [] -> true
in
inner longer shorter
@ebresafegaga
ebresafegaga / rec.rkt
Created May 12, 2021 15:03
Recursive types in Typed Racket
#lang typed/racket
(define-type Hungry (Rec X (Natural -> X)))
(: f Hungry)
(define (f _) f)
(define-type (Stream X) (Rec A (Void -> (List Natural A))))
; U-combinator (self application)
@ebresafegaga
ebresafegaga / compress.rkt
Created April 25, 2021 12:33
Probably buggy implementation of Lempel-Lev-Welch compression algorithm
#lang racket
(define (compress uncompressed)
(let ([table (for*/hash ([i (in-range 256)]
[str (in-value (string (integer->char i)))])
(values str i))])
(for*/fold ([w ""]
[size 256]
[result null]
[tbl table] #:result `(,result . ,table))
@ebresafegaga
ebresafegaga / views.rkt
Created April 25, 2021 12:30
View patterns in Racket
#lang racket/base
(require (rename-in racket/match)
(for-syntax racket/base))
(define-match-expander zero
(λ (stx) #'0)
(λ (stx) #'0))
(define-match-expander succ
(* (('a * 'b as 'a) * 'b) list -> 'a *)
let rec maximum xs =
let aux xs =
match xs with
| [(a,b)] -> (a,b)
| (a, b) :: xs ->
let (a', b') = maximum xs in
if b > b' then
(a, b)
@ebresafegaga
ebresafegaga / fork.ml
Created February 19, 2021 20:41
Fork combinator?
let fork (f, g) x = (f x, g x)
let unzip list = fork (List.map fst, List.map snd) list
@ebresafegaga
ebresafegaga / unix_fun.ml
Created February 19, 2021 20:37
Using OCaml unix library
let (>>) f g a = a |> f |> g
let list_all = Sys.readdir >> Array.to_list >> List.filter Sys.is_directory
let rec read_all handle =
match Unix.readdir handle with
| "." | ".." -> read_all handle
| item -> item :: read_all handle
| exception End_of_file -> []
@ebresafegaga
ebresafegaga / Y.ml
Last active May 12, 2021 15:10
Fix point combinator
type 'a recc = In of ('a recc -> 'a)
let out (In f) = f
let u f = out f f
let y g =
u (In (fun f -> g (out f f)))
@ebresafegaga
ebresafegaga / exist.ml
Created December 20, 2020 11:56
Encoding existential types in OCaml with universal types
(* A stack with an existential/abstract type 't *)
type 'a stack =
Stack: { to_list: 't -> 'a list;
of_list: 'a list -> 't;
empty: 't;
push: 'a -> 't -> 't;
pop: 't -> ('t * 'a) option } ->
'a stack
@ebresafegaga
ebresafegaga / Motive.idr
Last active December 19, 2020 22:05
It's all about the motive, baby
import Data.Vect
-- This is a paramorphism (aka primitive recursion) on Vectors
foldr : (motive : (k : Nat) -> Vect k a -> Type) ->
((l : Nat) -> (x : a) -> (xs : Vect l a) -> motive l xs -> motive (S l) (x :: xs)) ->
motive Z [] ->
(vec : Vect n a) ->
motive n vec
foldr {n = Z} motive step base [] = base
foldr {n = S k} motive step base (x :: xs) = step k x xs (foldr motive step base xs)