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 void scanToken() { | |
char c = advance(); | |
switch (c) { | |
case '(': addToken(LEFT_PAREN); break; | |
case ')': addToken(RIGHT_PAREN); break; | |
case '{': addToken(LEFT_BRACE); break; | |
case '}': addToken(RIGHT_BRACE); break; | |
case ',': addToken(COMMA); break; |
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 void scanToken(char character) { | |
if (singleCharacterToken(character) != null) { | |
addToken(singleCharacterToken(character)); | |
return; | |
} | |
if (isComparisonOperator(character)) { | |
addComparisonOperatorTokenFor(character); | |
return; | |
} |
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 (p) (p)) | |
(define (test x y) | |
(if (= x 0) | |
0 | |
y)) | |
(test 0 (p)) | |
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 (find-smallest a b c) | |
(cond ((and (<= a b) (<= a c) a)) | |
((and (<= b c) (<= b c) b)) | |
((and (<= c a) (<= c b) c)) | |
) | |
) | |
(define (largest-squared-hypotenuse a b c) ( | |
cond ((= (find-smallest a b c) a) (+ (* b b) (* c c))) | |
((= (find-smallest a b c) b) (+ (* a a) (* c c))) |
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 (square x) (* x x)) | |
(define (average x y) | |
(/ (+ x y) 2)) | |
(define (sqrt x) | |
(define (improve guess) | |
(average guess (/ x guess))) | |
(define (good-enough? guess) | |
(< (abs (- (square guess) x)) (* 0.000001 x))) |
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 (sum-n n) | |
(if (= n 0) | |
0 | |
(+ n (sum-n (- n 1))) | |
) | |
) | |
(sum-n 5) ; 15 |
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 (sum-i n) | |
(define (iter n sofar) | |
(if (= n 0) | |
sofar | |
(+ (iter (- n 1) (+ n sofar))))) | |
(iter n 0) | |
) | |
(sum-i 5) ; 15 |
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 (sum term a next b) | |
(accumulate + 0 term a next b)) | |
(define (product term a next b) | |
(accumulate * 1 term a next b)) | |
(define (factorial n) | |
(product identity 1 inc n)) |
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 (square x) (* x x)) | |
(define (qb x) (* x x x)) | |
(define (average x y) | |
(/ (+ x y) 2)) | |
(define (sq-improve guess for-value) | |
(average guess (/ for-value guess))) | |
(define (qb-improve guess for-value) | |
(/ (+(/ for-value (square guess)) (* 2 guess)) 3)) |
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
; 1.41 | |
(define (double f) | |
(lambda (x) (f(f x)))) | |
(define (add-2-to x) (+ 2 x)) | |
(define (inc x) (+ 1 x)) | |
(define (square x) (* x x)) | |
((double add-2-to) 3) |