Skip to content

Instantly share code, notes, and snippets.

@dyoo
dyoo / tompkins-paige-permute.rkt
Created October 12, 2012 22:29
A Racket implementation of the Tomkins-Paige permutation-generation algorithm
#lang racket/base
;; An implementation of the tompkins-paige algorithm
;;
;; http://en.wikipedia.org/wiki/Tompkins%E2%80%93Paige_algorithm
;;
;; Let P and c be arrays of length n with 1-based indexing
;; (i.e. the first entry of an array has index 1). The algorithm
;; for generating all n! permutations of the set {1,2,...,n} is
;; given by the following pseudocode:
raco setup: 0 making: tmp/racket/collects/2htdp (HtDP/2e Teachpacks)
image.rkt:37:9: module: identifier already imported from: (all-except "../mrlib/image-c...
at: render-image
in: "private/image-more.rkt"
context...:
/Users/dyoo/local/racket/collects/compiler/cm.rkt:350:0: compile-zo*
/Users/dyoo/local/racket/collects/compiler/cm.rkt:551:26
/Users/dyoo/local/racket/collects/compiler/cm.rkt:544:42
/Users/dyoo/local/racket/collects/compiler/cm.rkt:509:0: maybe-compile-zo
/Users/dyoo/local/racket/collects/compiler/cm.rkt:622:2: do-check
@dyoo
dyoo / typed-racket-bug.rkt
Created October 19, 2012 20:31
maybe a bug in typed racket?
#lang typed/racket/base
;; These are the primitives that we know how to inline.
(define-type KernelPrimitiveName/Inline (U '+
'-
'*
'/
'zero?
'add1
'sub1
@dyoo
dyoo / gist:3961723
Created October 26, 2012 21:39
playing with expand and bound-identifier=?
#lang racket/base
(require (for-template '#%kernel))
(define a-stx #'(define-values (f) (#%plain-lambda (x)
(let-values ([(x) 42]) x))))
(syntax-case a-stx ()
[(d-v (fun) (lam (id-0) (l-v (((id-1) v)) id-2)))
(begin
(printf "free-identifier=? ~s\n" (free-identifier=? #'id-1 #'id-2))
(printf "bound-identifier=? ~s\n" (bound-identifier=? #'id-1 #'id-2))
@dyoo
dyoo / inspecting-kernel.rkt
Created October 26, 2012 21:13
getting the names out of #%kernel
#lang racket/base
(require racket/set
rackunit)
;; kernel-variable-exports: (setof symbol)
;; Lists the runtime variable exports we inherit from '#%kernel
(provide (rename-out [result-from-module->exports
kernel-variable-exports]))
@dyoo
dyoo / huffman.rkt
Created November 2, 2012 19:45
huffman encoding
#lang racket
;; Building a huffman encoding tree.
(require data/heap)
;; A node is either an interior, or a leaf.
;; In either case, they record an item with an associated frequency.
(struct interior (freq left right) #:transparent)
(struct leaf (val freq) #:transparent)
@dyoo
dyoo / ordered-to-bst.rkt
Created November 2, 2012 20:49
ordered list to binary tree
#lang racket
;; The code of SICP Exercise 2.64, but rewritten to use define-values instead of let.
;; http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-16.html#%_thm_2.64
(struct tree (entry left right) #:transparent)
(define EMPTY-TREE '())
;; list->tree: (listof X) -> tree
;; Given an ordered list of elements, constructs the balanced binary tree.
@dyoo
dyoo / gist:4014787
Created November 5, 2012 01:38
right triangle example from jdc
#lang racket
(for*/list ([x (in-range 11)]
[y (in-range 11)]
[z (in-range 11)]
#:when (and (= (+ (* x x) (* y y)) (* z z)) (= (+ x y z) 24)))
(list x y z))
(for*/list ([x (in-range 11)]
[y (in-range 11)]
#:when (= 24 (+ x y (sqrt (+ (* x x) (* y y))))))
@dyoo
dyoo / join-list.rkt
Created November 9, 2012 00:29
an experiment with join lists
#lang racket/base
;; A JL is either a list, or a join-list
(struct join-list (x ;; JL
y ;; JL
size
))
(define threshold 50)
@dyoo
dyoo / count-inversions.rkt
Created November 12, 2012 00:01
Counts the number of inversions in a vector of numbers, using merge sort
#lang racket
(provide count-inversions count-inversions!)
;; A solution to Exercise 2.2d of CLRS. Counts the number of inversions
;; in a vector of numbers, using a modified merge sort.
;; count-inversions: vector -> number
(define (count-inversions v)
(count-inversions! (vector-copy v)))