Last active
August 29, 2023 05:45
-
-
Save navicore/3cec1dcddb68b15e39079d115411dfa9 to your computer and use it in GitHub Desktop.
scratch file while re-learning scheme in racket
This file contains 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 reciprocal | |
(lambda (n) | |
(if (= n 0) | |
"oops!" | |
(/ 1 n)))) |
This file contains 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 | |
(require malt) | |
(define rank | |
(lambda (t) | |
(ranked t 0))) | |
(define ranked | |
(lambda (t a) | |
(cond | |
((scalar? t) a) | |
(else (ranked (car t) (add1 a)))))) | |
(define shape | |
(lambda (t) | |
(cond | |
((scalar? t) (list)) | |
(else (cons (length t) (shape (car t))))))) | |
(rank '[[[2][9]][4][7]]) | |
(shape '[[[2][9]][4][7]]) | |
(rank '[[[1 2][3 4]]]) | |
(rank '[[[1 2 2.2][3 4 4.4]]]) | |
(shape '[[[1 2 2.2][3 4 4.4]]]) | |
(rank '[[1 2 2.2][3 4 4.4]]) | |
(shape '[[1 2 2.2][3 4 4.4]]) |
This file contains 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 | |
(require malt) | |
(define pie 3.14) | |
(define a-radius 8.4) | |
(define an-area (* pie (* a-radius a-radius))) | |
(lambda (r) | |
(* pie | |
(* r r))) | |
(define area-of-circle | |
(lambda (r) | |
(* pie | |
(* r r)))) | |
(define area-of-rectangle | |
(lambda (width) | |
(lambda (height) | |
(* width height)))) | |
((area-of-rectangle 3.0) 2.0) | |
(cond | |
((= pie 4) 28) | |
((< pie 4) 33) | |
(else 17)) | |
(define abs | |
(lambda (x) | |
(cond | |
((< x 0) (- 0 x)) | |
(else x)))) | |
(abs -2) | |
(abs 2) | |
(define silly-abs | |
(lambda (x) | |
(let ((x-is-negative (< x 0))) | |
(cond | |
(x-is-negative (- 0 x)) | |
(else (x)))))) | |
(silly-abs -2) | |
(define remainder | |
(lambda (x y) | |
(cond | |
((< x y) x) | |
(else (remainder (- x y) y))))) | |
(remainder 13 4) | |
(define add | |
(lambda (n m) | |
(cond | |
((zero? m) n) | |
(else (add1 (add n (sub1 m))))))) | |
(add 5 3) | |
(define line | |
(lambda (x) | |
(lambda (theta) | |
(+ (* (ref theta 0) x) (ref theta 1))))) | |
((line 8) '(4 6)) | |
((line 8) (list 1.0 0.0)) | |
((line 7.3) '(1.0 0.0)) | |
(define line-xssquare | |
[2.0 1.0 4.0 3.0]) | |
(define line-ys | |
[1.8 1.2 4.2 3.3]) | |
(car '(a b c)) | |
(cdr '(a b c)) | |
(cons 'a '(b c)) | |
(cons (car '(a b c)) | |
(cdr '(d e f))) | |
(define square | |
(lambda (n) | |
(* n n))) | |
(square 5) | |
(load "reciprocal.ss") | |
(reciprocal 1/10) | |
(+ 1/2 1/2) | |
(quote (1 2 3 4 5)) | |
(quote (+ 3 4)) | |
(let ((x 2)) | |
(+ x 3)) | |
(let ((x 2) (y 3)) | |
(+ x y)) | |
(let ([x 3] [y 4]) | |
(+ x y)) | |
(let ([+ *]) | |
(+ 2 3)) | |
(let ([double (lambda (x) (+ x x))]) | |
(list | |
(double 12) | |
(double (* 3 7)) | |
)) | |
(let ([double-cons (lambda (x) (cons x x))]) | |
(double-cons 'a)) | |
(let ([double-any (lambda (f x) (f x x))]) | |
(list | |
(double-any + 12) | |
(double-any + 13) | |
(double-any cons 'b)) | |
) | |
(let ([x 'a]) (cons x x)) | |
; is the same as | |
((lambda (x) (cons x x)) 'a) | |
(car '(1 2 3 4)) | |
(cdr '(1 2 3 4)) | |
(cadr '(1 2 3 4)) | |
(cddr '(1 2 3 4)) | |
(define abs | |
(lambda (n) | |
(if (< n 0) | |
(- 0 n) | |
n))) | |
(abs 77) | |
(abs -77) | |
(null? '(a b c)) | |
(define reciprocal | |
(lambda (n) | |
(if (and (number? n) (not (= 0 n))) | |
(/ 1 n) | |
"oops!"))) | |
(reciprocal 5) | |
(reciprocal 0) | |
(define reciprocal | |
(lambda (n) | |
(if (and (number? n) (not (= 0 n))) | |
(/ 1 n) | |
(error "imporoper argument" n)))) | |
(reciprocal 5) | |
(reciprocal 0) | |
(define sign | |
(lambda (n) | |
(cond | |
[(< n 0) -1] | |
[(> n 0) 1] | |
[else 0]))) | |
(sign -1) | |
(sign -20) | |
(sign 20) | |
(sign 0) | |
(define goodbye | |
(lambda () | |
(goodbye))) | |
(goodbye) | |
(define length | |
(lambda (ls) | |
(if (null? ls) | |
0 | |
(+ (length (cdr ls)) 1)))) | |
(length '(a b c d)) | |
(length '(a b c)) | |
(scalar? 1) | |
(scalar? -1) | |
(scalar? -1.0000009) | |
(length '[1 2 3]) | |
(length '[[1 2 3][4 5 6.6]]) | |
(cons 'a '[b c]) | |
; (define rank | |
; (lambda (t) | |
; (cond | |
; ((scalar? t) 0) | |
; (else (add1 (rank (car t))))))) ; this is the wrapped version | |
(define rank | |
(lambda (t) | |
(ranked t 0))) | |
(define ranked | |
(lambda (t a) | |
(cond | |
((scalar? t) a) | |
(else (ranked (car t) (add1 a)))))) | |
(define shape | |
(lambda (t) | |
(cond | |
((scalar? t) (list)) | |
(else (cons (length t) (shape (car t))))))) | |
(rank '[[[2][9]][4][7]]) | |
(shape '[[[2][9]][4][7]]) | |
(rank '[[[1 2][3 4]]]) | |
(rank '[[[1 2 2.2][3 4 4.4]]]) | |
(shape '[[[1 2 2.2][3 4 4.4]]]) | |
(rank '[[1 2 2.2][3 4 4.4]]) | |
(shape '[[1 2 2.2][3 4 4.4]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment