(define x #t) (boolean? x) (string? x) (number? x) (list? x) (list? '(a b c)) (not (list? '(a b c))) (not (list? (quote (a b c)))) ; bind names to values using let ; (let (pairs) (expr)) (let ((a 3.0) (b 4) (square (lambda (x) (* x x))) (plus +)) (sqrt (plus (square a) (square b)))) ; ERROR (let (x 4) (* x x)) ; OKAY (let ((x 4)) (* x x)) ; scope is second argument ONLY (let ((x 3)) (let ((x 5) (y x)) (+ x y))) ; ==> 8 (let ((x 3)) (let* ((x 5) (y x)) (+ x y))) ; ==> 10 ; using let* widens the scope of a variable ; declaration to the ones that follow in ; left-to-right order ; define creates a global binding for a name (define xyz 123.45) (let ((xyz 234.56)) (+ xyz xyz)) xyz ; scheme is statically scoped ; when a function is called, the interpreter uses ; the referencing environment PLUS bindings for the ; parameters and evaluates the expressions of the ; function in the order given (the last value is ; returned by the function). (define (grade x) (cond ( (null? x) (display "-") ) ; ( (list? x) (grade (car x)) ) ; ( (list? x) (grade (car x)) (grade (cdr x)) ) ( (list? x) (grade (car x)) (if (> (length x) 1) (grade (cdr x)) ) ) ( (>= x 90) (display "A\n") ) ( (>= x 80) (display "B") ) ( (>= x 70) (display "C") ) ( (>= x 60) (display "D") ) ; (#t (display "F")) (else (display "F")) )) (grade ()) (grade '(98 87)) (grade '(98 87 (99 98))) 5/6 (define x 5/6) (* x 2) ; modulo function returns remainder after integer division (modulo 7 3) ; modulo to support a and/or b as a list (define (mod a b) (cond ((null? a) '()) ((null? b) '()) ((and (list? a) (list? b)) (display "FILL IN THE BLANK...\n")) ((list? a) (cons (mod (car a) b) (mod (cdr a) b))) ((list? b) (cons (modulo a (car b)) (mod a (cdr b)))) (else (modulo a b)) )) (mod 7 3) (mod '(7 3) '(8 4)) (mod '(7 3) 9) (mod 9 '(7 3)) ; map a function to a series of values ; (reminiscent of Python) (map * '(2 4 6) '(3 5 7)) (define (mod_map a b) (cond ((null? a) '()) ((null? b) '()) ((and (list? a) (list? b)) (map modulo a b)) ((list? a) (map modulo a (list_n b (length a)))) ((list? b) (map modulo (list_n a (length b)) b)) (else (modulo a b)) )) (mod_map '(17 13) '(8 7)) (mod_map 17 '(8 7)) (mod_map '(17 13) 7) (define (list_n a n) (if (<= n 1) (list a) (cons a (list_n a (- n 1))))) (list_n 'c 5) (list_n '(a b c) 5) ; cons always expects 2 arguments (cons 'a '(b c)) (cons 'a '(b c) '(d e)) ; list takes 0 or more arguments (list) (list 'a) (list '(a b)) (list 'a 'b 'c) ; append combines elements from multiple lists (append '(a b)) (append '(a b) '(c d)) (append '(a) '(b c) '(d e)) ; sum (define (sum n) (if (< n 1) 0 (if (= n 1) 1 (+ n (sum (- n 1)))))) (define (sum_list_long n) (if (= n 1) '(1) (cons (sum n) (sum_list_long (- n 1))))) (define (sum_list_long_reverse n) (if (= n 1) (list 1) (append (sum_list_long_reverse (- n 1)) (list (sum n))))) ; sequence (define (sequence n) (if (= n 0) '() (append (sequence (- n 1)) (list n)))) (define (sum_list n) (map sum (sequence n))) ; factorial (define (factorial n) (if (= n 0) 1 (* n (factorial (- n 1))) )) (define (f_list n) (map factorial (sequence n))) (define (rotate_left a) (append (cdr a) (list (car a)))) (rotate_left '(a b c d)) (define (last a) (cond ((= (length a) 1) (car a)) (else (last (cdr a))) )) (last '(a b c d)) (define (except_last a) (cond ((> (length a) 2) (cons (car a) (except_last (cdr a)))) (else (list (car a))))) (except_last '(a b c d)) (define (rotate_right a) (cons (last a) (except_last a))) (rotate_right '(a b c d)) (reverse '(a b c)) ; equivalence ; = eq? eqv? equal? ; use = for numbers (eqv? 10 10) ;#t (eqv? 10 20) ;#f (eqv? 10 (- 20 10)) ;#t (define s1 "string") (define s2 s1) (eqv? s1 s2) ;#t (eqv? s1 "string") ;#f ; use equal? to recursively compare lists, strings, etc. (equal? s1 s2) ;#t (equal? s1 "string") ;#t (equal? '(a b c) '(a b c)) ;#t (equal? '(a b c) (cons 'a '(b c))) ;#t