This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
OlderNewer