Skip to content

Instantly share code, notes, and snippets.

@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)))
@dyoo
dyoo / mergesort.rkt
Created November 12, 2012 00:24
mergesort using the same trick as SICP 2.64 to avoid intermediate random access vector
#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)
@dyoo
dyoo / in-string-rev.rkt
Created November 14, 2012 20:31
iterating reverse string
#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]
@dyoo
dyoo / zeller.rkt
Created November 19, 2012 22:50
Zeller's congruence for computing weekday from day, month, and year
#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:
@dyoo
dyoo / gray.rkt
Created November 23, 2012 00:17
Gray binary codes
#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)
@dyoo
dyoo / gist:4187010
Created December 2, 2012 04:50
example of values for raphie
#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)))
@dyoo
dyoo / gist:4198537
Created December 3, 2012 22:05
errno hack
#lang racket/base
(require ffi/unsafe)
(define (get-errno)
(get-ffi-obj "errno" (ffi-lib #f) _int))
@dyoo
dyoo / gist:4227342
Created December 6, 2012 19:13
xexpr example
#lang racket/base
(require xml)
(define an-xexpr
(xml->xexpr
(document-element
(read-xml (open-input-string "<p>hello, this is a &lt;happy&gt; string</p>")))))
an-xexpr
> (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...:
> (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