Skip to content

Instantly share code, notes, and snippets.

Avatar
🐧
Staying home

Sam Tobin-Hochstadt samth

🐧
Staying home
View GitHub Profile
@samth
samth / lambda-prop.rkt
Created Apr 21, 2011
lambda/prop in Racket
View lambda-prop.rkt
#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 Jun 2, 2011
`group', functionally
View gist:1005012
#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 Jul 1, 2011
Jane Street problem
View gist:1058499
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 Aug 19, 2011
TF-IDF in Racket
View tf-idf.rkt
#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 Aug 27, 2011
Python Challenge problem #2
View 2.rkt
#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 Sep 12, 2011
Poisson-distributed random numbers in Typed Racket
View poisson-ffi.rkt
#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 Oct 21, 2011
Programming with syntax in Typed Racket
View syntax.rkt
#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 Oct 25, 2011
Producing explicit type errors for uncovered cases in Typed Racket
View explicit-failure.rkt
#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 Oct 30, 2011
Common Lisp's #. in Racket
View read-eval-test.rkt
#lang racket
#reader"read-eval.rkt"
(begin
#.(begin (define x 30) '(void))
'(1 2 #.x))
@samth
samth / def.rkt
Created Nov 20, 2011
Pattern-matching defintion
View def.rkt
#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)