Skip to content

Instantly share code, notes, and snippets.

View samth's full-sized avatar

Sam Tobin-Hochstadt samth

View GitHub Profile
@samth
samth / gist.rkt
Created March 19, 2012 01:13
Post images to imgur with Racket
#lang racket
(require net/url net/uri-codec net/base64 json)
(define f #f)
(command-line #:args (file)
(set! f file))
(define file-val (file->string f))
@samth
samth / for-syntax-for-template.rkt
Created March 20, 2012 16:50
Using submodules to avoid extra files
#lang racket
(module m racket
(define-for-syntax x 1)
(provide (for-syntax x)))
(require 'm)
(define-syntax (mac stx) (datum->syntax #'here x))
@samth
samth / qsort.rkt
Created March 24, 2012 20:48
Higher-order functions in C and Racket via the FFI
#lang racket
(require ffi/unsafe srfi/67)
(define-fun-syntax _unptr
(syntax-rules () [(_ t) (type: _pointer pre: (x => (ptr-ref x t)))]))
(define qsort
(get-ffi-obj 'qsort #f
(_fun (l : (_list io _int len))
(len : _int = (length l))
(defn analysis->map
"Convert Java Object expr into nested maps"
; result type:
; (rec X (U {:op :def
; :env {:source Object
; :line Object}
; :var Var}
; {:op :if
; :env {:source Object
; :line Object}
@samth
samth / sieve.rkt
Created April 30, 2012 16:04
Prime Sieve
#lang typed/racket
;; Adapted from http://stackoverflow.com/questions/10384875/why-is-clojure-much-faster-than-mit-scheme-for-equivalent-functions
(: sieve : Positive-Index -> (Listof Integer))
(define (sieve n)
(let ((#{root : Index} (assert (round (inexact->exact (sqrt n))) index?))
(#{a : (Vectorof Boolean)} (make-vector n #f)))
(: cross-out : Integer Fixnum Fixnum -> 0)
(define (cross-out t to dt)
@samth
samth / fp.dot
Created May 2, 2012 15:01 — forked from fogus/fp.dot
early influence graph of fp languages -- this is not meant to be a complete time line. I'm mostly concerned with the root and inner nodes.
digraph G {
KRC -> Miranda;
ML -> Miranda;
KRC -> SASL;
SASL -> Miranda;
Miranda -> Haskell;
Hope -> Haskell;
Hope -> ML;
Clean -> Haskell;
FP -> Haskell;
@samth
samth / clj_port.rkt
Created May 17, 2012 14:27 — forked from frenchy64/clj_port.rkt
clojure.core/isa? port to Typed Racket
;(defn isa?
; "Returns true if (= child parent), or child is directly or indirectly derived from
; parent, either via a Java type inheritance relationship or a
; relationship established via derive. h must be a hierarchy obtained
; from make-hierarchy, if not supplied defaults to the global
; hierarchy"
; {:added "1.0"}
; ([child parent] (isa? global-hierarchy child parent))
; ([h child parent]
; (or (= child parent)
@samth
samth / http-client.rkt
Created May 31, 2012 15:40
A simple http client in Racket by p4bl0
#lang racket/base
;; By http://www.reddit.com/user/p4bl0 at http://paste.fulltxt.net/FZ4-HeULc
;; `racket -il readline -t http-client.rkt`
;; then you can use (M "/path/...") where M can be get, post, put, delete.
;; When authenticated, you can logout using (logout).
;; The host and port can be changed using set!.
(require racket/tcp
@samth
samth / ints.py
Created June 5, 2012 12:27
Chris Leary's benchmark in Racket
import random
import sys
def main():
if len(sys.argv) != 2:
print >> sys.stderr, "Usage: %s <elem_count>" % sys.argv[0]
exit(-1)
count = int(sys.argv[1])
random.seed()
#lang typed/racket
(define d (read))
(match d
[(? number? d) (+ 5 d)]
[(? string? d) (string-append "hello " d)]
[_ (printf "unknown: ~a\n" d)])