1 / 19

Introduction to Functional Programming with Scheme

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

Download Presentation

Introduction to Functional Programming with Scheme

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Functional Programming with Scheme Carl Reynolds Carl Reynolds 2006

  2. 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

  3. 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

  4. 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

  5. REPL examples > 4 4 > "Carl" "Carl" > (define x 17) > x 17 > Carl Reynolds 2006

  6. REPL examples (cont.) > (+ x 8) 25 > (define y (- x 3)) > y 14 > Carl Reynolds 2006

  7. quote > x 17 > (quote x) ;don’t evaluate x x > 'x ;alternate syntax x > x 17 > Carl Reynolds 2006

  8. 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

  9. 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

  10. car > rides ((carl david chris) (ford vw nissan)) > > (car rides) (carl david chris) > (car (car rides)) carl > (caar rides) carl > Carl Reynolds 2006

  11. 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

  12. 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

  13. cons (cont.) > (define violations () ) > violations () > (cons 'my-first-ticket violations) (my-first-ticket) > Carl Reynolds 2006

  14. 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

  15. Writing your own functions( lambda (params) (body) ) >(define square (lambda ( x ) (* x x) ) ) > (square 8) 64 Carl Reynolds 2006

  16. Functions (cont.) > (define second (lambda (myList) (cadr myList)) ) > (second drivers) david > drivers (carl david chris) > Carl Reynolds 2006

  17. if > (define bigger (lambda (a b) (if (> a b) a b)) ) > (bigger 6 7) 7 > (bigger 5 2) 5 > Carl Reynolds 2006

  18. 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

  19. 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

More Related