Skip to content

Instantly share code, notes, and snippets.

@antonhornquist
Last active August 16, 2019 14:54
Show Gist options
  • Save antonhornquist/446f89e74ea36eb3520022b4e0d15738 to your computer and use it in GitHub Desktop.
Save antonhornquist/446f89e74ea36eb3520022b4e0d15738 to your computer and use it in GitHub Desktop.

// 1. Overview of Scheme

(* 5 8) // ==> 40

// 2.1 Identifiers

// Extended alphabetic characters may be used within identifiers as if they were letters. The following are extended alphabetic characters:

    • . * / < = > ! ? : $ % _ & ~ ^

// The following identifiers are syntactic keywords, and should not be used as variables:

=> do or and else quasiquote begin if quote case lambda set! cond let unquote define let* unquote-splicing delay letrec

// 2.2 Whitespace and comments

;;; The FACT procedure computes the factorial ;;; of a non-negative integer. (define fact (lambda (n) (if (= n 0) 1 // Base case: return 1 (* n (fact (- n 1))))))

#f

// 3.4 Disjointness of types

// No object satisfies more than one of the following predicates:

boolean? symbol? char? vector? pair? number? string? procedure?

// 4.1.1 Variable references

(define x 28) x // ==> 28

// 4.1.2 Literal expressions

(quote a) // ==> a (quote #(a b c)) // ==> #(a b c) TODO: returns (vector (a b c)) (quote (+ 1 2)) // ==> (+ 1 2)

// r4rs essential syntax: '

'a // ==> a '#(a b c) // ==> #(a b c) TODO: returns (vector (a b c)) '() // ==> () '(+ 1 2) // ==> (+ 1 2) '(quote a) // ==> (quote a) ''a // ==> (quote a)

'"abc" // ==> "abc" "abc" // ==> "abc" '145932 // ==> 145932 145932 // ==> 145932 '#t // ==> #t #t // ==> #t

// 4.1.3 Procedure calls

(+ 3 4) // ==> 7 ((if #f + *) 3 4) // ==> 12

// 4.1.4 Lambda expressions

(lambda (x) (+ x x)) // ==> a procedure ((lambda (x) (+ x x)) 4) // ==> 8

(define reverse-subtract (lambda (x y) (- y x))) (reverse-subtract 7 10) // ==> 3

(define add4 (let ((x 4)) (lambda (y) (+ x y)))) (add4 6) // ==> 10

((lambda x x) 3 4 5 6) // ==> (3 4 5 6) ((lambda (x y . z) z) // TODO 3 4 5 6) // ==> (5 6)

// 4.1.5 Conditionals

(if (> 3 2) 'yes 'no) // ==> yes (if (> 2 3) 'yes 'no) // ==> no (if (> 3 2) (- 3 2) (+ 3 2)) // ==> 1

// 4.1.6 Assignments

(define x 2) (+ x 1) // ==> 3 (set! x 4) // ==> unspecified (+ x 1) // ==> 5

// 4.2.1 Conditionals

// essential syntax: cond ...

(cond ((> 3 2) 'greater) ((< 3 2) 'less)) // ==> greater

(cond ((> 3 3) 'greater) ((< 3 3) 'less) (else 'equal)) // ==> equal

(cond ((assv 'b '((a 1) (b 2))) => cadr) // TODO: => (else #f)) // ==> 2

// essential syntax: case ...

(case (* 2 3) ((2 3 5 7) 'prime) ((1 4 6 8 9) 'composite)) ==> composite (case (car '(c d)) ((a) 'a) ((b) 'b)) ==> unspecified (case (car '(c d)) ((a e i o u) 'vowel) ((w y) 'semivowel) (else 'consonant)) ==> consonant

// essential syntax: and ...

(and (= 2 2) (> 2 1)) // ==> #t (and (= 2 2) (< 2 1)) // ==> #f (and 1 2 'c '(f g)) // ==> (f g) (and) // ==> #t

// essential syntax: or ...

(or (= 2 2) (> 2 1)) // ==> #t (or (= 2 2) (< 2 1)) // ==> #t (or #f #f #f) // ==> #f (or (memq 'b '(a b c)) (/ 3 0)) // ==> (b c)

// 4.2.2 Binding constructs

(let ((x 2) (y 3)) (* x y)) // ==> 6

(let ((x 2) (y 3)) (let ((x 7) (z (+ x y))) (* z x))) // ==> 35

(let ((x 2) (y 3)) (let* ((x 7) (z (+ x y))) (* z x))) // ==> 70

(letrec ((even? // TODO (lambda (n) (if (zero? n) #t (odd? (- n 1))))) (odd? (lambda (n) (if (zero? n) #f (even? (- n 1)))))) (even? 88)) // ==> #t

// 4.2.3 Sequencing

(define x 0)

(begin (set! x 5) (+ x 1)) // ==> 6

(begin (display "4 plus 1 equals ") // TODO: strings with whitespace (display (+ 4 1))) // ==> unspecified and prints 4 plus 1 equals 5

// 4.2.4 Iteration

(do ((vec (make-vector 5)) // TODO: do (i 0 (+ i 1))) ((= i 5) vec) (vector-set! vec i i)) // ==> #(0 1 2 3 4)

(let ((x '(1 3 5 7 9))) // TODO: do (do ((x x (cdr x)) (sum 0 (+ sum (car x)))) ((null? x) sum))) // ==> 25

(let loop ((numbers '(3 -2 1 6 -5)) // TODO: named let (nonneg '()) (neg '())) (cond ((null? numbers) (list nonneg neg)) ((>= (car numbers) 0) (loop (cdr numbers) (cons (car numbers) nonneg) neg)) ((< (car numbers) 0) (loop (cdr numbers) nonneg (cons (car numbers) neg))))) // ==> ((6 1 3) (-5 -2))

// 4.2.6 Quasiquotation

(list ,(+ 1 2) 4) // ==> (list 3 4) (let ((name 'a)) (list ,name ',name)) // ==> (list a (quote a)) (a ,(+ 1 2) ,@(map abs '(4 -5 6)) b) // TODO: @ used instead of ,@ // ==> (a 3 4 5 6 b) ((foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons))) // TODO: dotted pairs // ==> ((foo 7) . cons) `#(10 5 ,(sqrt 4) ,@(map sqrt '(16 9)) 8) // ==> #(10 5 2 4 3 8) // TODO: quotes as (vector

(a (b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f) // TODO // ==> (a (b ,(+ 1 2) ,(foo 4 d) e) f) (let ((name1 'x) // TODO (name2 'y)) (a (b ,,name1 ,',name2 d) e)) // ==> (a (b ,x ,'y d) e)

(quasiquote (list (unquote (+ 1 2)) 4)) // ==> (list 3 4) '(quasiquote (list (unquote (+ 1 2)) 4)) // ==> `(list ,(+ 1 2) 4) // TODO i.e., (quasiquote (list (unquote (+ 1 2)) 4))

// 5.2.1 Top level definitions

(define add3 (lambda (x) (+ x 3))) (add3 3) // ==> 6 (define first car) (first '(1 2)) // ==> 1

// 5.2.2 Internal definitions

(let ((x 5)) (letrec ((foo (lambda (y) (bar x y))) // TODO: letrec (bar (lambda (a b) (+ (* a b) a)))) (foo (+ x 3))))

// 6.1 Booleans

#t // ==> #t #f // ==> #f '#f // ==> #f

(not #t) // ==> #f (not 3) // ==> #f (not (list 3)) // ==> #f (not #f) // ==> #t (not '()) // ==> #f (not (list)) // ==> #f (not (quote nil)) // ==> #f

(boolean? #f) // ==> #t (boolean? 0) // ==> #f (boolean? '()) // ==> #f

// 6.2 Equivalence predicates

// essential procedure: eqv? obj1 obj2

(eqv? 'a 'a) // ==> #t (eqv? 'a 'b) // ==> #f (eqv? 2 2) // ==> #t (eqv? '() '()) // ==> #t (eqv? 100000000 100000000) // ==> #t (eqv? (cons 1 2) (cons 1 2)) // ==> #f (eqv? (lambda () 1) (lambda () 2)) // ==> #f (eqv? #f 'nil) // ==> #f (let ((p (lambda (x) x))) (eqv? p p)) // ==> #t

(eqv? "" "") // ==> unspecified (eqv? '#() '#()) // ==> unspecified (eqv? (lambda (x) x) (lambda (x) x)) // ==> unspecified (eqv? (lambda (x) x) (lambda (y) y)) // ==> unspecified

(define gen-counter (lambda () (let ((n 0)) (lambda () (set! n (+ n 1)) n)))) (let ((g (gen-counter))) (eqv? g g)) // ==> #t (eqv? (gen-counter) (gen-counter)) // ==> #f (define gen-loser (lambda () (let ((n 0)) (lambda () (set! n (+ n 1)) 27)))) (let ((g (gen-loser))) (eqv? g g)) // ==> #t (eqv? (gen-loser) (gen-loser)) // ==> unspecified

(letrec ((f (lambda () (if (eqv? f g) 'both 'f))) // TODO (g (lambda () (if (eqv? f g) 'both 'g))) (eqv? f g))

) // ==> unspecified

(letrec ((f (lambda () (if (eqv? f g) 'f 'both))) // TODO (g (lambda () (if (eqv? f g) 'g 'both))) (eqv? f g))

) // ==> #f

(eqv? '(a) '(a)) // ==> unspecified (eqv? "a" "a") // ==> unspecified (eqv? '(b) (cdr '(a b))) // ==> unspecified (let ((x '(a))) (eqv? x x)) // ==> #t

// r4rs essential procedure: eq? obj1 obj2 (eq? 'a 'a) // ==> #t (eq? '(a) '(a)) // ==> unspecified (eq? (list 'a) (list 'a)) // ==> #f (eq? "a" "a") // ==> unspecified (eq? "" "") // ==> unspecified (eq? '() '()) // ==> #t (eq? 2 2) // ==> unspecified (eq? #\A #\A) // ==> unspecified TODO: # chars (eq? car car) // ==> #t (let ((n (+ 2 3))) (eq? n n)) // ==> unspecified (let ((x '(a))) (eq? x x)) // ==> #t (let ((x '#())) (eq? x x)) // ==> #t (let ((p (lambda (x) x))) (eq? p p)) // ==> #t

// essential procedure: equal? obj1 obj2

(equal? 'a 'a) // ==> #t (equal? '(a) '(a)) // ==> #t (equal? '(a (b) c) '(a (b) c)) // ==> #t (equal? "abc" "abc") // ==> #t (equal? 2 2) // ==> #t (equal? (make-vector 5 'a) (make-vector 5 'a)) // ==> #t (equal? (lambda (x) x) (lambda (y) y)) // ==> unspecified

// 6.3 Pairs and lists

TODO

(a b c d e)

(a . (b . (c . (d . (e . ()))))) // TODO

(a b c . d)

(a . (b . (c . d)))

(define x (list 'a 'b 'c)) (define y x) y // ==> (a b c) (list? y) // ==> #t (set-cdr! x 4) // ==> unspecified x // ==> (a . 4) (eqv? x y) // ==> #t y // ==> (a . 4) (list? y) // ==> #f (set-cdr! x x) // ==> unspecified (list? x) // ==> #f

// essential procedure: pair? obj

(pair? '(a . b)) // ==> #t TODO (pair? '(a b c)) // ==> #t (pair? '()) // ==> #f (pair? '#(a b)) // ==> #f TODO

// essential procedure: cons obj1 obj2

(cons 'a '()) // ==> (a) (cons '(a) '(b c d)) // ==> ((a) b c d) (cons "a" '(b c)) // ==> ("a" b c) (cons 'a 3) // ==> (a . 3) // TODO: presentation (cons '(a b) 'c) // ==> ((a b) . c) // TODO: presentation

// essential procedure: car pair

(car '(a b c)) ==> a (car '((a) b c d)) ==> (a) (car '(1 . 2)) ==> 1 (car '()) ==> error TODO

// essential procedure: cdr pair

(cdr '((a) b c d)) ==> (b c d) (cdr '(1 . 2)) ==> 2 (cdr '()) ==> error TODO

// essential procedure: set-car! pair obj

(define (f) (list 'not-a-constant-list)) TODO: lambdas (define (g) '(constant-list)) (set-car! (f) 3) ==> unspecified (set-car! (g) 3) ==> error

// essential procedure: caar pair // essential procedure: cadr pair // ... // essential procedure: cdddar pair // essential procedure: cddddr pair

(define caddr (lambda (x) (car (cdr (cdr x)))))

// essential procedure: list? obj

(list? '(a b c)) // ==> #t (list? '()) // ==> #t (list? '(a . b)) // ==> #f TODO (let ((x (list 'a))) TODO (set-cdr! x x) TODO (list? x)) // ==> #f

// essential procedure: list obj ...

(list 'a (+ 3 4) 'c) // ==> (a 7 c) (list) // ==> ()

// essential procedure: length list

(length '(a b c)) // ==> 3 (length '(a (b) (c d e))) // ==> 3 (length '()) // ==> 0

// essential procedure: append list ...

(append '(x) '(y)) // ==> (x y) (append '(a) '(b c d)) // ==> (a b c d) (append '(a (b)) '((c))) // ==> (a (b) (c))

(append '(a b) '(c . d)) // ==> (a b c . d) TODO (append '() 'a) // ==> a TODO

// essential procedure: reverse list

(reverse '(a b c)) // ==> (c b a) (reverse '(a (b c) d (e (f)))) // ==> ((e (f)) d (b c) a)

// procedure: list-tail list k

(define list-tail (lambda (x k) (if (zero? k) x (list-tail (cdr x) (- k 1)))))

// essential procedure: list-ref list k

(list-ref '(a b c d) 2) ==> c (list-ref '(a b c d) (inexact->exact (round 1.8))) // ==> c

// essential procedure: memq obj list // essential procedure: memv obj list // essential procedure: member obj list

(memq 'a '(a b c)) // ==> (a b c) (memq 'b '(a b c)) // ==> (b c) (memq 'a '(b c d)) // ==> #f (memq (list 'a) '(b (a) c)) // ==> #f TODO (member (list 'a) '(b (a) c)) // ==> ((a) c) (memq 101 '(100 101 102)) // ==> unspecified TODO (memv 101 '(100 101 102)) // ==> (101 102)

// essential procedure: assq obj alist // essential procedure: assv obj alist // essential procedure: assoc obj alist

(define e '((a 1) (b 2) (c 3))) (assq 'a e) // ==> (a 1) (assq 'b e) // ==> (b 2) (assq 'd e) // ==> #f (assq (list 'a) '(((a)) ((b)) ((c)))) // ==> #f (assoc (list 'a) '(((a)) ((b)) ((c)))) // ==> ((a)) (assq 5 '((2 3) (5 7) (11 13))) // ==> unspecified (assv 5 '((2 3) (5 7) (11 13))) // ==> (5 7)

// 6.4 Symbols

// essential procedure: symbol? obj

(symbol? 'foo) // ==> #t (symbol? (car '(a b))) // ==> #t (symbol? "bar") // ==> #f (symbol? 'nil) // ==> #t (symbol? '()) // ==> #f (symbol? #f) // ==> #f

// essential procedure: symbol->string symbol

(symbol->string 'flying-fish) // ==> "flying-fish" (symbol->string 'Martin) // ==> "martin" (symbol->string (string->symbol "Malvina")) // ==> "Malvina"

// essential procedure: string->symbol string

(eq? 'mISSISSIppi 'mississippi) TODO // ==> #t (string->symbol "mISSISSIppi") // ==> // the symbol with name "mISSISSIppi" (eq? 'bitBlt (string->symbol "bitBlt")) TODO // ==> #f (eq? 'JollyWog (string->symbol (symbol->string 'JollyWog))) // ==> #t (string=? "K. Harper, M.D." TODO (symbol->string (string->symbol "K. Harper, M.D."))) // ==> #t

// 6.5 Numbers

TODO

// 6.6 Chars TODO

// essential procedure: char->integer char // essential procedure: integer->char n

(char<=? a b) => #t and (<= x y) => #t

// and x and y are in the domain of integer->char, then

(<= (char->integer a) (char->integer b)) ==> #t

(char<=? (integer->char x) (integer->char y)) ==> #t

// 6.7 Strings

"The word "recursion" has many meanings."

// essential procedure: string-set! string k char

(define (f) (make-string 3 #*)) (define (g) "***") (string-set! (f) 0 #?) ==> unspecified (string-set! (g) 0 #?) ==> error (string-set! (symbol->string 'immutable) 0 #?) ==> error

// 6.8 Vectors

'#(0 (2 2 2 2) "Anna") // TODO // ==> #(0 (2 2 2 2) "Anna")

// essential procedure: vector-ref vector k

(vector-ref '#(1 1 2 3 5 8 13 21) TODO 5) // ==> 8 (vector-ref '#(1 1 2 3 5 8 13 21) TODO (inexact->exact (round (* 2 (acos -1))))) // ==> 13

// essential procedure: vector-set! vector k obj

(let ((vec (vector 0 '(2 2 2 2) "Anna"))) (vector-set! vec 1 '("Sue" "Sue")) vec) // ==> #(0 ("Sue" "Sue") "Anna")

(vector-set! '#(0 1 2) 1 "doe") // ==> error ; constant vector

// essential procedure: vector->list vector // essential procedure: list->vector list

(vector->list '#(dah dah didah)) // ==> (dah dah didah) (list->vector '(dididit dah)) // ==> #(dididit dah)

// 6.9 Control features

// essential procedure: procedure? obj

(procedure? car) // ==> #t (procedure? 'car) // ==> #f (procedure? (lambda (x) (* x x))) // ==> #t (procedure? '(lambda (x) (* x x))) // ==> #f (call-with-current-continuation procedure?) TODO // ==> #t

// essential procedure: apply proc args // procedure: apply proc arg1 ... args

(apply + (list 3 4)) // ==> 7 TODO

(define compose (lambda (f g) (lambda args (f (apply g args)))))

((compose sqrt *) 12 75) // ==> 30

// essential procedure: map proc list1 list2 ...

(map cadr '((a b) (d e) (g h))) // ==> (b e h)

(map (lambda (n) (expt n n)) '(1 2 3 4 5)) // ==> (1 4 27 256 3125) TODO

(map + '(1 2 3) '(4 5 6)) // ==> (5 7 9)

(let ((count 0)) (map (lambda (ignored) (set! count (+ count 1)) count) '(a b c))) // ==> unspecified

// essential procedure: for-each proc list1 list2 ...

(let ((v (make-vector 5))) (for-each (lambda (i) (vector-set! v i (* i i))) '(0 1 2 3 4)) v) // ==> #(0 1 4 9 16)

// procedure: force promise

TODO

// essential procedure: call-with-current-continuation proc

TODO

// 6.10 Input and output

TODO

// 6.10.4 System interface

TODO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment