190 likes | 313 Views
Introduction to Functional Programming with Scheme. Carl Reynolds. Expressions. Atoms Numbers like 5 or 6.79 Symbols like x , / , + , or find-parents Strings like “Bach” or “Carl” Lists ( enclosed within parentheses ) () null list. Data Types. Variables do not have type
E N D
Introduction to Functional Programming with Scheme Carl Reynolds Carl Reynolds 2006
Expressions • Atoms • Numbers like 5 or 6.79 • Symbols like x, /, +, or find-parents • Strings like “Bach” or “Carl” • Lists • ( enclosed within parentheses ) • () null list Carl Reynolds 2006
Data Types • Variables do not have type • A variable can hold any type of data • Values have type • Integer • Floating point • String • List Carl Reynolds 2006
Read-Eval-Print Loop--REPL • Number evaluates to itself • String evaluates to itself • Symbol evaluates to the value of the variable • List • First element is evaluated as a function, and following elements as arguments to the function Carl Reynolds 2006
REPL examples > 4 4 > "Carl" "Carl" > (define x 17) > x 17 > Carl Reynolds 2006
REPL examples (cont.) > (+ x 8) 25 > (define y (- x 3)) > y 14 > Carl Reynolds 2006
quote > x 17 > (quote x) ;don’t evaluate x x > 'x ;alternate syntax x > x 17 > Carl Reynolds 2006
Lists > (list 8 9 2) ;make a list (8 9 2) > (list 8 x y 88 x) (8 17 14 88 17) > (list a b c d) [Repl(15)] Error: variable d is not bound. Type (debug) to enter the debugger. > (list 'a 'b 'c 'd) (a b c d) > Carl Reynolds 2006
Lists of lists > (define car-list (list 'ford 'vw 'nissan)) > (define drivers (list 'carl 'david 'chris)) > (define rides (list drivers car-list)) > rides ((carl david chris) (ford vw nissan)) >drivers (carl david chris) > car-list (ford vw nissan) > Carl Reynolds 2006
car > rides ((carl david chris) (ford vw nissan)) > > (car rides) (carl david chris) > (car (car rides)) carl > (caar rides) carl > Carl Reynolds 2006
cdr (“coulder”) > (car rides) (carl david chris) > (cdr (car rides)) (david chris) > (car (cdr rides)) (ford vw nissan) > (cdr (car (cdr rides))) (vw nissan) > (cdr rides) ((ford vw nissan)) > (cdr (cdr rides)) () > (cdadr rides) (vw nissan) > Carl Reynolds 2006
cons > (cons 'honda car-list) (honda ford vw nissan) > car-list (ford vw nissan) > (define car-list (cons 'honda car-list)) > car-list (honda ford vw nissan) > (cons 'bmw car-list) (bmw honda ford vw nissan) > Carl Reynolds 2006
cons (cont.) > (define violations () ) > violations () > (cons 'my-first-ticket violations) (my-first-ticket) > Carl Reynolds 2006
append > (define more-cars (list 'chevy 'buick 'saab)) > (append car-list more-cars) (honda ford vw nissan chevy buick saab) > car-list (honda ford vw nissan) > more-cars (chevy buick saab) > Carl Reynolds 2006
Writing your own functions( lambda (params) (body) ) >(define square (lambda ( x ) (* x x) ) ) > (square 8) 64 Carl Reynolds 2006
Functions (cont.) > (define second (lambda (myList) (cadr myList)) ) > (second drivers) david > drivers (carl david chris) > Carl Reynolds 2006
if > (define bigger (lambda (a b) (if (> a b) a b)) ) > (bigger 6 7) 7 > (bigger 5 2) 5 > Carl Reynolds 2006
cond (define how-hot-is-it (lambda ( degrees ) (cond ( ( < degrees 32 ) 'freezing ) ( ( and (> degrees 68 ) (< degrees 80) ) 'comfortable ) ( ( >= degrees 90 ) 'too-hot ) ( #t 'hard-to-say ) ) ) ) > (how-hot-is-it 77) comfortable > (how-hot-is-it 44) hard-to-say > Carl Reynolds 2006
Recursion (define listSum (lambda (n) (cond ((null? n) 0) ((null? (cdr n)) (car n) ) ( else (+ (car n) (listSum2 (cdr n)))) ))) > (listsum '(3 6 12 3)) 24 > (listsum '(4)) 4 > (listsum '() ) 0 > Carl Reynolds 2006