Skip to content

Instantly share code, notes, and snippets.

View mflatt's full-sized avatar

Matthew Flatt mflatt

View GitHub Profile
@mflatt
mflatt / functional.rkt
Created January 16, 2014 17:13
Programs by Jay and Matthew at Lambda Lounge Utah, 14 Jan 2014: * "functional.rkt" is from Jay's introduction to functional programming * "web.rkt" and "page.rkt" are from Matthew's explanation of Racket macros & modules
#lang racket
(require rackunit)
;; A singly linked list is either
;; - NULL
;; - pointer to data and a pointer a sll
(struct node (data-ptr next-ptr))
(define n4 (node 4 null))
### Keybase proof
I hereby claim:
* I am mflatt on github.
* I am mflatt (https://keybase.io/mflatt) on keybase.
* I have a public key whose fingerprint is E7B8 3F92 02CB 9F4D D41F 27BA 5F91 2E16 6255 DF85
To claim this, I am signing this object:
#lang racket
(module property racket
(provide quote-syntax/add-example-property
id)
(define-for-syntax compile-time (current-inexact-milliseconds))
(define-syntax (quote-syntax/add-example-property stx)
(syntax-case stx ()
(define ephemeron-key car)
(define ephemeron-value cdr)
(define (show v) (write v) (newline))
(define (check-same a b)
(unless (eq? a b)
(error 'check-same "failed")))
(define gdn (make-guardian))
(with-output-to-file "testfile.ss"
(lambda ()
(printf "(\n")
(let loop ([i 1000])
(unless (zero? i)
(printf "x ")
(loop (sub1 i))))
(printf ")\n"))
'truncate)
(let ([M 100]
[N 1000000])
(define (report step) (printf "\n~a\n" step))
(report "baseline: plain loop")
(time
(let loop ([j 0])
(cond
[(= j N) 'done]
[else
#lang racket/base
(define M 100)
(define N 1000000)
;; Baseline tail-call loop
(time
(let loop ([j 0])
(cond
[(= j N) 'done]
#lang racket/base
(module s racket/base
(require racket/contract/base)
(provide (contract-out [f (-> integer? integer?)])
g)
(define (f x) x)
;; Simulate the expansion of `contract-out`:
@mflatt
mflatt / gist:379d032487e1951e4470263d87415b1d
Created August 9, 2018 12:45
Curry using an arity-mask API
#lang racket/base
(module curry-old racket/base
(provide curry curryr)
(define (make-curry right?)
;; The real code is here
(define (curry* f args kws kvs)
(unless (procedure? f)
(raise-argument-error (if right? 'curryr 'curry) "procedure?" f))
@mflatt
mflatt / gist:f7313e1561210be56313903fd75c6607
Created July 28, 2019 14:22
statement trees based on binary operators
Designated "statement" grouping operators with precedence from
strongest to weakest:
, => = & | :
Examples, showing intended use followed by full parenthesization:
define : f(s) =
printf("hi ~a\n", s),
"hi"