Skip to content

Instantly share code, notes, and snippets.

View chelseatroy's full-sized avatar

Chelsea Troy chelseatroy

View GitHub Profile
#lang racket
; 3.22
(define (make-queue)
(let ((front-ptr '())
(rear-ptr '()))
(define (empty?)
(null? front-ptr))
@chelseatroy
chelseatroy / state.scm
Last active October 30, 2019 21:12
Environments as Objects
; 3.1
(define (make-counter value)
(define (incr x)
(set! value (+ value x))
value)
incr)
(define counter (make-counter 100))
@chelseatroy
chelseatroy / dispatch.scm
Last active November 2, 2019 23:33
Types as Dispatch Procedures
(define (make-bob-box x y width height)
(define (dispatch message)
(cond ((eq? message 'width) width)
((eq? message 'height) height)
((eq? message 'type) 'bob-box)
(else (error "Bad method"))))
dispatch)
(define (make-alice-box x1 y1 x2 y2)
(define (dispatch message)
@chelseatroy
chelseatroy / bob_and_alice.scm
Last active November 2, 2019 23:47
Type Registry
#lang racket
(define (register h method tag function)
(hash-set! h (list tag method) function))
(define (lookup h key)
(hash-ref h key))
(define h (make-hash))
(register h 'alice-box 'width alice-width)
@chelseatroy
chelseatroy / scheme.py
Created October 30, 2019 19:23
An Interpreter
# scheme.py
#
# Challenge: Can you implement a mini-scheme interpreter capable of
# executing the following code:
env = {
'+' : lambda x, y: x + y,
'-' : lambda x, y: x - y,
'*' : lambda x, y: x * y,
'/' : lambda x, y: x / y,
'!=' : lambda x, y: x != y,
@chelseatroy
chelseatroy / derivative.scm
Last active October 31, 2019 14:50
Derivative
(define (menq item sequence)
(cond ((null? sequence) false)
((eq? item (car sequence)) sequence)
(else (memq item (cdr sequence)))))
(list 'a 'b 'c)
(equal? '(this is a list) '(this is a list))
(equal? '(this is a list) '(this '(is a) list))
@chelseatroy
chelseatroy / Parser.java
Created October 30, 2019 18:08
Comparison and Addition
private Expr comparison() {
Expr expr = addition();
while (consuming(GREATER, GREATER_EQUAL, LESS, LESS_EQUAL)) {
Token operator = previousToken();
Expr right = addition();
expr = new Expr.Binary(expr, operator, right);
}
return expr;
}
@chelseatroy
chelseatroy / collection_operations.scm
Created October 30, 2019 15:22
Fold Left and Fold Right
; 2.38
(define (fold-right op initial sequence)
(accumulate op initial sequence))
(define (fold-left op initial sequence)
(define (iter result rest)
(if (null? rest)
result
(iter (op result (car rest))
; 2.33
(define (accumulate op initial items) ; accumulate also referred to as "reduce"
(if (null? items)
initial
(op (car items) (accumulate op initial (cdr items)))))
; Defining collection operations in terms of accumulate
(define (append seq1 seq2)
@chelseatroy
chelseatroy / collection_operations.scm
Last active October 30, 2019 15:11
Collection Operations
; Collection Operations
(define (map proc items)
(if (null? items)
null
(cons (proc (car items))
(map proc (cdr items)))))
(define (filter condition items)
(if (null? items)