Skip to content

Instantly share code, notes, and snippets.

@ebresafegaga
ebresafegaga / nqueens.ml
Last active November 18, 2020 20:42
N Queens puzzle in OCaml
type pos = Position of int * int
let noAttack (Position (x, y)) (Position (i, j)) =
x <> i && y <> j && abs (x-i) <> abs (y-j)
let rec range n m =
if m < n
then []
else n :: range (n+1) m
@ebresafegaga
ebresafegaga / countdown.fs
Last active November 11, 2020 20:17
Countdown problem in F# with brute force
let rem x = List.filter ((=) x >> not)
let rec perms = function
| [] -> [[]]
| xs ->
xs
|> List.collect (fun x -> List.map (fun l -> x :: l) (perms (rem x xs)))
@ebresafegaga
ebresafegaga / Money.fs
Last active November 11, 2020 20:01
SEND + MORE = MONEY puzzle in F#
let rest = [0..9]
let first = [1..9]
type Letter = S | E | N | D | M | O | R | Y
let send = [S;E;N;D]
let more = [M;O;R;E]
let money = [M;O;N;E;Y]
@ebresafegaga
ebresafegaga / det.fs
Created November 11, 2020 20:10
Determinant of a matrix by pivotal condensation in F#
let rc l =
List.length l, List.length << List.head $ l
let ij i j l =
let cols, rows = rc l
if i >= rows || j >= cols then failwith "Out of bounds error"
else List.nth (List.nth l i) j
let mapRow i f l =
@ebresafegaga
ebresafegaga / Free.hs
Created November 17, 2020 02:53
Monads for free!
module Free where
data Term f a = Pure a
| Impure (f (Term f a))
instance Functor f => Functor (Term f) where
fmap f (Pure a) = Pure (f a)
fmap f (Impure g) = Impure (fmap (fmap f) g)
@ebresafegaga
ebresafegaga / zip_list.ml
Last active December 2, 2020 22:51
ZipList in OCaml with Jane Street's Core
open Core
module ZipList = struct
type 'a t = ZipList of 'a list
let return x =
(* A nice feature of Ocaml; just make sure the operation performed on the list
doesn't "recur" over the whole list. It has to be something like `take`, or a
`zip` with another finite list *)
let rec l = x :: l in
@ebresafegaga
ebresafegaga / cp.ml
Created December 6, 2020 15:38
Possibly the world’s first functional pearl (by David Barron and Christopher Strachey) - Cartesian product
open ListLabels
let product xss =
let f xs yss =
let g x xs =
let h a b = (x :: a) :: b in
List.fold_right ~f:h ~init:xs yss
in
List.fold_right ~f:g ~init:[] xs
in
@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)
@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 / 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)))