Skip to content

Instantly share code, notes, and snippets.

View soegaard's full-sized avatar

Jens Axel Søgaard soegaard

View GitHub Profile
@soegaard
soegaard / tagged-begin.rkt
Created July 9, 2012 22:09
tagged-begin (labels, goto and return) for Racket
#lang racket
(provide tagged-begin)
;;; INTRODUCTION
; This is a little macro that resembles the Common Lisp tagbody construct
; <http://www-2.cs.cmu.edu/Groups/AI/html/hyperspec/HyperSpec/Body/speope_tagbody.html#tagbody>
; See also "Applications of Continuations" of Daniel P. Friedman.
;;; MOTIVATION
@soegaard
soegaard / gist:3182110
Created July 26, 2012 13:46
for/first in terms of for/fold without let/ec
#lang racket
(define-syntax (my-for/first stx)
(syntax-case stx ()
[(_ (for-clause ...) . defs+exprs)
(syntax/loc stx
(let-values ([(val _)
(for/fold ([val #f] [stop? #f])
(for-clause ... #:unless stop?)
(begin
(define x (let () . defs+exprs))
@soegaard
soegaard / gist:3182213
Created July 26, 2012 14:04
What is the expected result of the following?
(for/fold ([val #f] [stop? #f])
(#:unless stop?
[x '(1 3 5 7 4 9 6)]
#:when (even? x))
(values x #t))
@soegaard
soegaard / require-typed.rkt
Created August 5, 2012 18:15
require/typed
#lang typed/racket
(require/typed typed/racket
[integer-sqrt/remainder (Natural Natural -> Natural)])
integer-sqrt/remainder
----
racket-math/racket/collects/racket/contract/private/blame.rkt:89:0: integer-sqrt/remainder: broke its contract
promised a procedure that accepts 2 mandatory arguments without any keywords, produced: #<procedure:integer-sqrt/remainder>
in: (recursive-contract
@soegaard
soegaard / pacman
Created August 26, 2012 21:12
Pacman and Pinky
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname pacman) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "2htdp") (lib "universe.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "2htdp") (lib "universe.ss" "teachpack" "2htdp")))))
;;; Inspired by http://www.khanacademy.org/cs/chompy-and-friends/882986876
(define PACMAN-RADIUS 50)
(define pacman
(overlay/offset
@soegaard
soegaard / example.rkt
Created August 26, 2012 21:34
Example
#lang racket
(define-syntax (foo stx)
(syntax-case stx ()
[(foo x)
(let ([a (+ 1 2)])
(with-syntax ([a a])
#'(+ x a)))]))
(foo 39)
@soegaard
soegaard / lc53.rkt
Created November 17, 2012 12:47
Implementation of lc53
#lang racket/base
(define m (- (expt 2 32) 5))
(define a (- (expt 2 32) 333333333))
(define rand
(let ([xn 1])
(λ ()
(set! xn (modulo (* a xn) m))
xn)))
@soegaard
soegaard / screenshot.rkt
Created December 2, 2012 16:11
How to take a screenshot on OS X using the Racket FFI
#lang racket
;;;
;;; How to take a screenshot on OS X
;;; -- Jens Axel Søgaard
;;;
;;; This gist illustrates how to work with OS X
;;; system calls in order to take a screen shot.
;;;
@soegaard
soegaard / capture-window.rkt
Created December 2, 2012 22:20
Capture image of window in OS X
#lang racket
;;;
;;; How to take a screenshot on OS X
;;; -- Jens Axel Søgaard
;;;
;;; This gist illustrates how to work with OS X
;;; system calls in order to take a screen shot.
;;;
(require racket/draw
#lang racket/base
(require ragg/examples/simple-line-drawing/lexer
ragg/examples/simple-line-drawing/grammar
syntax/parse
(for-syntax syntax/parse)
(for-syntax racket/base))
(define-syntax (define-datums stx)
(syntax-parse stx