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
#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: |
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
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 |
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
#lang typed/racket/base | |
;; These are the primitives that we know how to inline. | |
(define-type KernelPrimitiveName/Inline (U '+ | |
'- | |
'* | |
'/ | |
'zero? | |
'add1 | |
'sub1 |
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
#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)) |
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
#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])) |
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
#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) |
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
#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. |
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
#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)))))) |
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
#lang racket/base | |
;; A JL is either a list, or a join-list | |
(struct join-list (x ;; JL | |
y ;; JL | |
size | |
)) | |
(define threshold 50) |
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
#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))) |