1 / 19

Scheme & Functional Programming

Scheme & Functional Programming. (+ 23 41) >> 64 ( - 1000 334) >> 666 (* 25 4 12) >> 1200 (+ (* 3 5) (- 10 6)) >> 19. Data Types. #t for true and #f for false Characters: #a is ‘a’, #space is ‘ ’ Strings “Hello World!” define variables: (define greeting “Hello World!”).

Download Presentation

Scheme & Functional Programming

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. Scheme & Functional Programming

  2. (+ 23 41) >> 64 (- 1000 334) >> 666 (* 25 4 12) >> 1200 (+ (* 3 5) (- 10 6)) >> 19

  3. Data Types • #t for true and #f for false • Characters: #\a is ‘a’, #\space is ‘ ’ • Strings “Hello World!” • define variables: (define greeting “Hello World!”)

  4. (define num 2) (* 5 num) >> 10 (define (square x) (* x x)) (square 9) >> 81 (square (+ 5 4)) >> 81 (square (square 3)) >> 81

  5. (define (abs x) (if (< x 0) (- x) x)) • and, or, not (and (> x 5) (< x 10))

  6. (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1)))))

  7. Local variables: let • (let (list of bindings) (what to compute)) (let ((x 2) (y 3)) (* x y)) • Problem: can’t use x later in bindingslist • let* (let* ((x 2 ) (y (- x 2)) (*x y))

  8. Functions as arguments Want to calculate: b Σ f(n) n=a for any functions f and next: (define (sum f a next b) (if (> a b) 0 (+ (f a) (sum f (next a) next b)))) • if (inc a) is a+1, and (cube x) is x3 what is (sum cube 1 inc 5)

  9. lambda Functions • Remember anonymous functions in Python? That was a functional-programming feature. • (sum cube 1 inc 5) • (sum (lambda (x) (* x x x)) 1 (lambda (x) (+ 1 x)) 5)

  10. filter • (filter <function> <list>) • (filter positive? (list 1 -1 2 -2 3 -3))

  11. Lists (define one-through-four (list 1 2 3 4))> one-through-four> (1 2 3 4)

  12. Head (car) and Tail (cdr) (car one-through-four)1(cdr one-through-four)(2 3 4)(car (cdr one-through-four))2

  13. Construct a list (cons) (cons 10 one-through-four)(10 1 2 3 4)(cons 5 one-through-four)(5 1 2 3 4)

  14. append • Can append two lists into one • This is different than cons

  15. Empty List Keyword null is an empty list. Check for empty list:  (if (null? items)

  16. Quoting (define a 1)(define b 2)(list a b)(1 2)(list 'a 'b)(a b)(list 'a b)(a 2)

  17. Put it in a file • I know what you’re thinking….How do I make a comment? (Semicolon for in-lines) ; Hello World (define (hello) (begin (display “Hello World!”) (newline)))

  18. Scheme is interpreted… • but we can load a file all at once: • (load “hello.ss”)

  19. Practice • Sum up a list • “double up” a list – for example, (doubleUp (list 1 2 3 4)) should give(1 1 2 2 3 3 4 4) • zipUp two lists - do pairings, for example, (zipUp (list 1 2 3) (list 4 5 6)) should give ((1 4) (2 5) (3 6))

More Related