;; This is a transcript from CS 314 lecture, 2/4/2004, Prof DeCarlo Welcome to DrScheme, version 205. Language: Standard (R5RS). > (lambda (x) (+ x x)) # > ((lambda (x) (+ x x)) 4) 8 > ((lambda (x) (+ x x)) (* 2 (+ 3 5))) 32 > (define double (lambda (x) (+ x x))) # > (double 5) 10 > (double (+ 3 7)) 20 > (define x (+ 3 7)) > x 10 > (double x) 20 > (define x 3) > x 3 > (define x '((a b (b c)))) > x ((a b (b c))) ;; This is an alternative form for defining a function ;; without saying "lambda" > (define (double x) (+ x x)) > (quote 3) 3 > (quote a) a > 'a a > a . reference to undefined identifier: a > x ((a b (b c))) > + # > double # > quote quote: bad syntax in: quote > (quote 3 3) quote: bad syntax (wrong number of parts) in: (quote 3 3) ;; Note that quote is a special form, and the arguments are ;; NOT evaluated first -- unlike + > (quote 3) 3 > (+ 3) 3 > (quote a) a > (+ a) . reference to undefined identifier: a ;; Here is a lambda with more than one argument > (define wow (lambda (x y) (+ x y 1))) > (wow 4 10) 15 ;; You can't directly make a function that just ;; implements quote -- if you do it this way, then it ;; actually quotes the x! (This is pretty strange, so ;; don't worry if it's confusing -- it did come up in ;; class, though) > (define myquote (lambda (x) (quote x))) > (quote a) a > a . reference to undefined identifier: a > (myquote a) . reference to undefined identifier: a > myquote # ;; Even though x is bound to 3, it still returns the ;; quoted symbol 'x -- in this case, the 3 is just thrown away > (myquote 3) x > 3 3 > a . reference to undefined identifier: a > (quote x) x > (quote b) b > x ((a b (b c))) ;; here it is again, but this time using the symbol 'blah instead ;; of 'x which got a little confusing > (lambda (x) 'x) # > (lambda (x) 'blah) # > ((lambda (x) 'blah) 3) blah > (myquote a) . reference to undefined identifier: a > (quote a) a > (define a 1) > (myquote a) x > (myquote 1) x ;; Building lists.. > (list a b c) . reference to undefined identifier: b > (list 'a 'b 'c) (a b c) > (quote (a b c)) (a b c) > (list 3 59 1) (3 59 1) > (car (list 3 59 1)) 3 > (cdr (list 3 59 1)) (59 1) > (define x (list 3 59 1)) > (car x) 3 > (cdr x) (59 1) > (car (cdr x)) 59 > (cdr (cdr x)) (1) > (car (cdr (cdr x))) 1 > (cdr (cdr (cdr x))) () ;; Can't apply car and cdr to an empty list > (car ()) . car: expects argument of type ; given () > (cdr ()) . cdr: expects argument of type ; given () > (define x '(1 . 2)) > x (1 . 2) > (car x) 1 > (cdr x) 2 > (cons 1 2) (1 . 2) > (cons 1 (list 2)) (1 2) > (cons 1 ()) (1) > (cons 1) . cons: expects 2 arguments, given 1: 1 > (cons 1 2 3) . cons: expects 2 arguments, given 3: 1 2 3 ;; Let's try to make the list (1 2 3) using only cons... > '(1 2 3) (1 2 3) > (cons 1 2) (1 . 2) > (cons 1 (list 2 3)) (1 2 3) > (cons 1 (cons 2 3)) (1 2 . 3) > (cons 1 (cons 2 (cons 3 ()))) (1 2 3) > (list 1 2 3) (1 2 3) ;; some predicates availableon lists > (null? (list 1)) #f > (null? ()) #t > (list? (list 1)) #t > (list? '(1 . 2)) #f > (cons 1 '(2 3)) (1 2 3) > (cons 1 (quote (2 3))) (1 2 3) ;; boolean operators > (or #t #t #f) #t > (and #t #t #f) #f > (or #t) #t > (and #t) #t > (not #t) #f > (not #f) #t ;; and/or can return the value of certain arguments ;; when they aren't #t's -- see the slides for a description > (or 3 #t) 3 > (and 3 5 #t 27) 27 > (and 3 #f #t 27) #f > (or #f #f) #f > (or #f 3) 3 > (null? ()) #t > (symbol? 'a) #t > (symbol? 3) #f