Skip to content

Instantly share code, notes, and snippets.

View camoy's full-sized avatar

Cameron Moy camoy

View GitHub Profile
@camoy
camoy / keybase.md
Created June 13, 2015 02:47
keybase.md

Keybase proof

I hereby claim:

  • I am camoy on github.
  • I am camoy (https://keybase.io/camoy) on keybase.
  • I have a public key whose fingerprint is CC10 FC49 A531 78CF 4DAA 0CAF 1CE9 059A 203F CAE1

To claim this, I am signing this object:

@camoy
camoy / prize_box.md
Last active June 5, 2020 00:23
Solving a logic puzzle in Prolog.

A logic puzzle called "Prize Box" from the ASCM problem solving competition:

Three boxes, 3 signs: Only one box has the prize; n is odd if and only if two signs are true. Sign A says "n is even and box 1 has the prize." Sign B says "n + 4 is a multiple of 3 or box 3 has the prize." Sign C says "n is odd or box 2 has the prize." Given that n is a single digit positive integer where every digit is equally likely, which

@camoy
camoy / reachability.hs
Last active June 20, 2018 04:50
Graph reachability in four lines of Haskell (requires set-monad package)
import qualified Data.Set.Monad as S
fix f init = until (\x -> x == f x) f init
close rstar = fix (\s -> s `S.union` (rstar s))
rstar g s = do { x <- s; (x, y) <- g; pure y }
reachable g = close (rstar g)
-- g1 = S.fromList [(1, 2), (2, 3), (3, 1), (4, 1)]
-- reachable g1 (S.singleton 1) == S.fromList [1, 2, 3]
@camoy
camoy / z.ml
Created July 13, 2018 02:35
OCaml adapation of (Y Y) Works
(* This is an OCaml adaptation of (Y Y) Works! by Felleisen
and Friedman. An updated version of the same essay is
available in the Little Schemer, Chapter 9. To run the
examples you have to enable the -rectypes flag. *)
(* Let's define the length function. *)
let rec length xs =
match xs with
| [] -> 0
@camoy
camoy / list.ml
Last active September 14, 2018 05:55
OCaml List Lecture (9/13/18)
(* Exercise: Write the class definition (no methods) for a linked list in
Java. *)
(* Bad Approach:
class List<T> {
T data;
List<T> next;
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (assert pred)
(or pred (amb)))
(define amb-queue (make-parameter #f))
(define amb-escape (make-parameter #f))
(define-syntax-rule (begin-amb fail body ...)
(let/cc k
@camoy
camoy / amb.rkt
Last active July 13, 2019 21:53
McCarthy's `amb` without mutable state.
(define-values (success-tag fail-tag)
(values (make-continuation-prompt-tag 'success)
(make-continuation-prompt-tag 'fail)))
(define-syntax amb
(syntax-rules ()
[(_ alt ...)
(call/comp
(λ (k)
(abort
@camoy
camoy / return.rkt
Last active July 26, 2019 06:13
Implementation of a return construct in OCaml with exceptions and references.
let with_return f x =
let r = ref None in
try
f (fun v -> r := (Some v); raise Not_found) x
with _ ->
match !r with
| None -> raise Not_found
| Some x -> x
let til_first_even =
#lang racket/base
(require racket/contract)
(define must-return/c
(make-contract
#:name 'must-return/c
#:late-neg-projection
(λ (blm)
(λ (f neg)
@camoy
camoy / violin.rkt
Created January 6, 2022 02:17
violin plot
#lang racket/base
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; provide
(provide violin)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; require