This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
; 3.22 | |
(define (make-queue) | |
(let ((front-ptr '()) | |
(rear-ptr '())) | |
(define (empty?) | |
(null? front-ptr)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 3.1 | |
(define (make-counter value) | |
(define (incr x) | |
(set! value (+ value x)) | |
value) | |
incr) | |
(define counter (make-counter 100)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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) |