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))) |
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 | |
;; Implementation of a mergesort on lists, while avoiding intermediate | |
;; vector construction. Uses the same trick as that in SICP Exercise | |
;; 2.64 to keep track of the elements we haven't yet processed. | |
(provide mergesort) | |
(define (mergesort elts) |
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 | |
(define (in-string/rev s) | |
(define stop 'stop) | |
(define n (string-length s)) | |
(define i (sub1 n)) | |
(define (next!) | |
(cond | |
[(< i 0) | |
stop] |
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 | |
;; zeller's congruence | |
;; http://en.wikipedia.org/wiki/Zeller%27s_congruence | |
(require (for-syntax racket/base) | |
racket/unsafe/ops) | |
;; Gregorian version of Zeller's congruence is: |
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 | |
;; Gray binary code, from TAOCP 7.2.1.1. | |
;; | |
;; Gray codes are sequences where adjacent elements are different in only one position. | |
;; | |
;; The gray codes of length n can be generated with the following recursive rules: | |
;; | |
;; G_0 = epsilon | |
;; G_{n+1} = 0 G_n, 1 ((G_n)^r) |
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 | |
(define (get-first-value f) | |
(call-with-values f | |
(lambda the-values | |
(first the-values)))) | |
(get-first-value (lambda () (values 'a 'b 'c))) |
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 ffi/unsafe) | |
(define (get-errno) | |
(get-ffi-obj "errno" (ffi-lib #f) _int)) |
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 xml) | |
(define an-xexpr | |
(xml->xexpr | |
(document-element | |
(read-xml (open-input-string "<p>hello, this is a <happy> string</p>"))))) | |
an-xexpr |
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
> (require data/red-black/ordered-set) | |
> (define s (new-ordered-set)) | |
> (ordered-set-add! s "hi") | |
> (ordered-set-add! s "mye") | |
> (ordered-set-add! s "aloha") | |
> (for ([x s]) (printf "this says: ~s" x)) | |
this says: "aloha"this says: "hi"this says: "mye"> (ordered-set-contains?) | |
ordered-set-contains?: undefined; | |
cannot reference undefined identifier | |
context...: |
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
> (require data/red-black/ordered-set) | |
> (define s (new-ordered-set)) | |
> (ordered-set-add! s "hi") | |
> (ordered-set-add! s "mye") | |
> (ordered-set-add! s "aloha") | |
> (for ([x s]) (printf "this says: ~s" x)) | |
this says: "aloha"this says: "hi"this says: "mye" | |
> (ordered-set-contains?) | |
ordered-set-contains?: undefined; | |
cannot reference undefined identifier |