; Day 1-2 code examples; ; Last updated March 10, 2014. 6 '6 ; The same thing. simple constants do not have to be quoted. (+ 6 3) ; Scheme uses fully-parenthesized prefix form for expressions. ; The first value in the list is the procedure; ; the remaining values are its arguments. six ; this variable has not yet been defined. (define six 6) (/ six 7) ; note that integer division is more like Maple than like Java. (quote six) ; quote is a "special form". We'll see others soon. 'six ; the symbol six '(+ six 7) ; a literal expression. Does not get evaluated. + ; In Scheme, procedures are "first class" data objects. (+ (/ 4 5) (/ 3 7)) (max six 5) ; standard math functions are pre-defined. See Summary of Forms in TSPL. (zero? (- 2 3)) (negative? (- 2 3)) (define my-favorite-number 37) (>= 17 6) * ; "operators" are not specical in Scheme. They are symbols bound to procedures. (+ 3 (if (< my-favorite-number) 4 5)) ; if is an expression that returns a value. (2 4) ; indeed it is not a procedure! (list 2 4) ; create a list. BOX DiAGRAM to show what the liat looks like. '(2 4) ; Another way of creating this list. (cons 3 (list 2 4)) ; You can see what happens '() ; the empty list (also known as "null"). Some versions of Scheme allow it to be input as (). (list) ; the same thing (cons 1 '()) ; cons makes a pair; 1 end null. (1 . nuull). Schme prints it as (1). (cons 1 '()) same as (list 1). (cons 1 2) ; Make an "improper list" (doesn't end with null) Scheme prints (1 . 2) (define a '(1 2 3)) (define b (list 1 2 3)) (null? a) (null? '()) (equal? a b) ; test for structural equality. Like Java equals() (eq? a b) ; are they actually the same object? Like Java == (= 4 5) ; test for numeric equality. (list-ref a 1) ; start counting with 0 (define va (list->vector a)) ; "vector" is Scheme's name for "array" va (vector-ref va 1) (= 5 (- 8 3)) ; test for numerical equality a (car a) ; first half of pair. (cdr a) ; second half of pair. (car (cdr a)) (cadr a) ; abbreviation for the above (cdr (cdr a)) (cddr a) (caddr a) (cdddr a) (cddddr a) ; end of the line! (cdddddr a) (cdr (cons 2 3)) (cdr (list 2 3)) ; see how cons is different than list? (let ([b 5] [c 7]) ; let is the "declare local variables" syntax. Square brackets are optional. (+ b C 6)) b ; note that the above let does not change the global b. (let ([a 5] [b 6]) (let ([b (+ 10 a)]) (+ a b))) (define times3 ; define our own procedure. (lambda (x) (* x 3))) ; lambda is the "create a procedure" form. (times3 5) (times3 (+ 2 7)) (times3 'a) (times3) times3 ;; ----------------------- Items skipped or covered quickly on Day 1: (define a '(1 2 3)) (define b (list 1 2 3) (define va (list->vector a)) ; "vector" is Scheme's name for "array" va (vector-ref va 1) (let ([b 5] [c 7]) ; let is the "declare local variables" syntax. Square brackets are optional. (+ b C 6)) b ; note that the above let does not change the global b. (let ([a 5] [b 6]) (let ([b (+ 10 a)]) (+ a b))) ; More on let later (define times3 ; define our own procedure. (lambda (x) (* x 3))) (define times6 (lambda (x) (* 2 (times3 x)))) (times6 4) (trace times6) (trace times3) (times6 4) (define abs ; It's predefined, but I want to illustrate "if". (lambda (x) (if (< x 0) (- 0 x) x))) (list (abs 4) (abs -4)) ;-------------------------------------------------------------------- ; Now it gets really interesting! (define make-adder ; a procrdure that takes a numeric argument (lambda (n) ; and creates and returns a new procedure. (lambda (m) (+ m n)))) (define add5 (make-adder 5)) add5 (add5 7) ((make-adder 8) 9) ; procedures can be anonymous. (((lambda (t) ; The ability to create procedures "on the fly" (lambda (s) ; without having to give them names (* s t))) ; will give us amazing new power. 4) 7) ;-------------------------------------------------------------------- (define fact ; define a recursive procedure. (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))) (fact 5) (fact 75) (define fact-tail ; A different approach to calculating factorials (lambda (n accum) ; accumulate the product as we go along. (if (zero? n) accum (fact-tail (- n 1) (* n accum))))) (define factorial (lambda (n) (fact-tail n 1))) (trace fact-tail) (trace fact) (trace factorial) (fact 4) (factorial 4) ; fact-tail is "tail-recursive". Scheme doesn't grow the stack. (untrace) (define len ; length of list (lambda (ls) (if (null? ls) 0 (+ 1 (len (cdr ls)))))) (len '(3 4 5 6))