Skip to content

Instantly share code, notes, and snippets.

View samth's full-sized avatar

Sam Tobin-Hochstadt samth

View GitHub Profile
@samth
samth / lambda-prop.rkt
Created April 21, 2011 20:05
lambda/prop in Racket
#lang racket
(struct proc/prop (f v)
#:property prop:procedure 0)
(define (procedure-property p [fail #f])
(if (proc/prop? p)
(proc/prop-v p)
fail))
@samth
samth / gist:1005012
Created June 2, 2011 18:48
`group', functionally
#lang racket
(require racket/generator
rackunit
rackunit/text-ui)
(define/contract (group seq group-size)
(sequence? natural-number/c . -> . sequence?)
(in-generator
(let-values ([(l final-chunk)
(for/fold ([l 0] [this-chunk null])
@samth
samth / gist:1058499
Created July 1, 2011 12:59
Jane Street problem
Welcome to Racket v5.1.1.6.
> (time (for/first ([i (in-naturals 1)]
#:when (regexp-match #rx"^(1|0)*$"
(number->string (* i 225))))
(* i 225)))
cpu time: 53687 real time: 53692 gc time: 1072
11111111100
>
@samth
samth / tf-idf.rkt
Created August 19, 2011 15:47
TF-IDF in Racket
#lang racket
(require unstable/dict)
(provide main)
;; Set[String]
(define stopwords (list->set (file->lines "./stopwords.txt")))
;; String -> List[String]
(define (tokenize raw-text) ;; Lowercases and splits on non-letters, non-numbers.
(filter-not (λ (e) (set-member? stopwords e))
@samth
samth / 2.rkt
Created August 27, 2011 21:31
Python Challenge problem #2
#lang at-exp racket
(define str
@string-append||{
;; giant wall of characters goes here
}||)
(define (frequencies l)
(for/fold ([h (hash)]) ([i l]) (hash-update h i add1 0)))
(define h (frequencies str))
(apply string
@samth
samth / poisson-ffi.rkt
Created September 12, 2011 21:10
Poisson-distributed random numbers in Typed Racket
#lang racket/base
;; translated from an algorithm in C from
;; Hyberts SG, Takeuchi K, Wagner G (2010) Poisson-gap sampling and
;; forward maximum entropy reconstruction for enhancing the resolution
;; and sensitivity of protein NMR Data. J Am Chem Soc 132:2145–2147
;; generate random Poisson-distributed numbers as given
;; by Donald E. Knuth (1969). Seminumerical Algorithms.
;; The Art of Computer Programming, Volume 2. Addison Wesley
@samth
samth / syntax.rkt
Created October 21, 2011 16:00
Programming with syntax in Typed Racket
#lang typed/racket
(: stx Syntax)
(define stx #'bar)
(syntax-case stx ()
[foo (identifier? #'foo) (symbol=? 'bar (syntax-e #'foo))]
[_ (error 'whoops)])
@samth
samth / explicit-failure.rkt
Created October 25, 2011 20:14
Producing explicit type errors for uncovered cases in Typed Racket
#lang typed/racket
(define-syntax (cond* stx)
(syntax-case stx ()
[(_ x clause ...)
#`(cond clause ... [else (typecheck-fail #,stx "incomplete coverage" #:covered-id x)])]))
(: f : (U String Integer) -> Boolean)
(define (f x)
(cond* x
@samth
samth / read-eval-test.rkt
Created October 30, 2011 17:47
Common Lisp's #. in Racket
#lang racket
#reader"read-eval.rkt"
(begin
#.(begin (define x 30) '(void))
'(1 2 #.x))
@samth
samth / def.rkt
Created November 20, 2011 16:58
Pattern-matching defintion
#lang racket
(require syntax/parse/define)
(define-simple-macro (def (f0:id p0 ...) rhs0:expr (~seq (f:id p ...) rhs:expr) ...)
(define f0 (match-lambda** [(p0 ...) rhs0] [(p ...) rhs] ...)))
(def (fact 0) 1 (fact 1) 1 (fact n) (* n (fact (- n 1))))
(fact 5)