Skip to content

Instantly share code, notes, and snippets.

View camoy's full-sized avatar

Cameron Moy camoy

View GitHub Profile
#lang effect/racket
;;
;; require
;;
(unsafe-require "aux.rkt")
;;
;; 0 (introduction)
#lang racket
(require (for-syntax racket/base
syntax/id-table
syntax/parse
syntax/parse/lib/function-header))
(module+ test (require rackunit))
(begin-for-syntax
(define raw-sc (make-syntax-introducer))
#lang racket
(provide
(contract-out
[remember/c (-> (-> any/c any/c) chaperone-contract?)]))
(require meta)
(module+ test (require rackunit))
(define (remember/c convert)
#lang racket
(provide
(contract-out
[canonicalize/c
(-> (-> any/c any/c) contract?)]))
(module+ test (require rackunit))
(define (canonicalize/c convert)
@camoy
camoy / README.md
Last active January 24, 2022 07:35
Miller-Rabin liars

Primality tests like the Fermat test and the Miller-Rabin test rely on so-called "witnesses." In the case of Fermat, if a^{p-1} = 1 (mod p), for some a, then p is probably prime. The a is called a Fermat witness. However, if a composite passes the test for a given a, then a is called a Fermat liar. The same principle holds for Miller-Rabin, although the equation is slightly more complicated.

Which numbers are the most honest? Which ones are the most lying? Run racket main.rkt to produce a bunch of heat maps that show the most honest (dark) and most lying (bright) numbers. This one shows for the numbers 2 to 1024:

Miller-Rabin liars

@camoy
camoy / violin.rkt
Created January 6, 2022 02:17
violin plot
#lang racket/base
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; provide
(provide violin)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; require
#lang racket/base
(require racket/contract)
(define must-return/c
(make-contract
#:name 'must-return/c
#:late-neg-projection
(λ (blm)
(λ (f neg)
@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 =
@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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(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