Skip to content

Instantly share code, notes, and snippets.

@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 / 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
(* (('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 / 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
@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 / 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 / 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 / transpose.ml
Created December 19, 2021 19:27
Traspose now!
let rec transpose xss =
match xss with
| [] :: _ | [] -> []
| xs :: xss ->
let hds = List.map List.hd xss in
let tls = List.map List.tl xss in
let hd = List.hd xs in
let tl = List.tl xs in
(hd :: hds) :: transpose (tl :: tls)
let rec min count counts =
if count = 0 then 0
else if not (List.contains count counts) then
0
else
1 + min (count - 1) counts
let minDeletions (str : string) =
let step m elem =
@ebresafegaga
ebresafegaga / match.fs
Last active February 14, 2022 19:22
Leetcode wildcard matching solution in F#. https://leetcode.com/problems/wildcard-matching/
type pattern =
| Char of char * pattern
| Any of pattern
| Wildcard of pattern
| End
let parsePattern (str: string) =
let str = [for s in str do s]
let folder c pat =
match c with