Scheme Tutorial

# Scheme Tutorial

## Scheme Tutorial

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Scheme Tutorial

2. Goals • Combine several simple ideas into one compound idea to obtain complex ideas • Bring two ideas together to obtain relations • Seperate ideas from all other ideas that accompany them in real existence to obtain general ideas (this is called abstraction) by John Locke, An Essay Concerning Human Understanding (1690)

3. Features of LISP • Recursive Functions of Symbolic Expressions and Their Computation by Machine (John McCarthy, 1960) • LISP stands for LISt Processing • An interpreted language (not efficient) • Second oldest language (after FORTRAN) • Designed to solve problems in the form of symbolic differentiation & integration of algebraic expressions

4. Features of LISP • LISP’s ability • to represent procedures as data • to manipulate programs as data • LISP is currently a family of dialects (share most of the original features) • Scheme is a dialect of LISP (small yet powerful)

5. Characteristics of SCHEME • Supports functional programming - but not on an exclusive basis • Functions are first class data objects • Uses static binding of free names in procedures and functions • Types are checked and handled at run time - no static type checking • Parameters are evaluated before being passed - no lazyness

6. Elements of Language • Primitive expressions – simple expressions • Means of combination – compound expressions • Means of abstraction – compound objects can be named and manipulated as units

7. Scheme Expressions - 1 > 395 395 > (+ 137 349) 486 > (/ 10 6) 1.66667

8. Scheme Expressions - 2 Prefix Notation take arbitrary number of arguments > (+ 21 35 12 7) 75 > (* 25 4 12) 1200

9. Scheme Expressions - 3 Prefix Notation allow combinations to be nested > (+ (* 3 5) (- 10 6) ) 19 > (* (+ 3 (- (+ 2 1) 5) (/ 4 2)) (* 3 2)) Read – Eval – Print loop

10. Scheme Pretty-Printing > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) > (load “e1.scm”)

11. Scheme Naming > (define size 2) > size 2 > (* 5 size) 10 > (load “e2.scm”)

12. Compound Procedures > (define (square x) (* x x)) > (square 3) 9 (define (< name > < formal parameters >) < body > ) > (load “e3.scm”) Substitution model for procedure application

13. Conditional Expressions > (define (abs x) (cond ((> x 0) x) ((= x 0) 0) ((< x 0) (- x)))) > (define (abs x) (cond ((< x 0) (- x)) (else x)))

14. Conditional Expressions (cond (<p1> <e1>) (<p2> <e2>) : (<pN> <eN>)) p is predicate either true (non-nil) or false (nil) e is consequent expression returns the value of it

15. Conditional Expressions > (define (abs x) (if (< x 0) (- x) x)) (if <predicate> <consequent> <alternative>)

16. Logical Operators Primitive operators : < , > , = Logical operators : and , or , not > (define (>= x y) (or (> x y) (= x y))) Example: another definition of (>= x y) Example: 5 < x < 10

17. Procedures different than mathematical functions x = the y such that y  0 and y2 = x > (define (sqrt x) (the y (and (>= y 0) (= (square y) x)))) Mathematics – declarative (what is) Programs – imperative (how to) Square Roots by Newton’s Method > (load “e4.scm”) - 2 = 1.4142...

18. Square Roots by Newton’s Method break the problem into subproblems how to tell whether a guess is good enough how to improve a guess how to calculate the average of two numbers etc. each of the above tasks is a procedure

19. Square Roots by Newton’s Method sqrt sqrt-iter good-enough? improve square abs average > (load “e4.scm”)

20. Procedures – Black Box Abstractions (define (square x) (* x x)) (define (square x) (exp (+ (log x) (log x))))

21. Procedural Abstractions (define (square x) (* x x)) (define (square y) (* y y)) (define (square variable) (* variable variable)) parameter names that are local to the procedure bound variables – change throughout the procedure does not change the meaning of the procedure free variables – if a variable is not bound in the proc (define (good-enough? guess x) (< (abs (- (square guess) x)) .001))

22. Internal Definitions Encapsulation – hiding details > (load “e4.scm”) Nesting definitions – block structure > (load “e5.scm”) Lexical scoping – not necessary to pass x explicitly to internal procedures, so x becomes free variable in the internal definitions > (load “e6.scm”)

23. Procedures Procedure : a pattern for the local evolution of a computational process. At each step, the next state of the process is computed from its current state according to the rules of interpreting procedures.

24. Linear Recursion (factorial 6) (* 6 (factorial 5)) (* 6 (* 5 (factorial 4))) (* 6 (* 5 (* 4 (factorial 3)))) (* 6 (* 5 (* 4 (* 3 (factorial 2))))) (* 6 (* 5 (* 4 (* 3 (* 2 (factorial 1)))))) (* 6 (* 5 (* 4 (* 3 (* 2 1))))) (* 6 (* 5 (* 4 (* 3 2)))) (* 6 (* 5 (* 4 6))) (* 6 (* 5 24)) (* 6 120) 720 process does grow and shrink

25. Linear Recursion n! = n * (n-1) * (n-2) ... 2 * 1 n! = n * (n-1)! n! = n * ( (n-1) * (n-2)! ) n! = n * ( ... ( (n-1) * (n-2) * ... * 1! ) ) ... ) (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1)))))

26. Linear Iteration (factorial 6) (fact-iter 1 1 6) (fact-iter 1 2 6) (fact-iter 2 3 6) (fact-iter 6 4 6) (fact-iter 24 5 6) (fact-iter 120 6 6) (fact-iter 720 7 6) 720 process does not grow and shrink

27. Linear Iteration Product, counter = 1 do while counter < n product = counter * product counter = counter + 1 (define (factorial n) (fact-iter 1 1 n)) (define (fact-iter product counter max-count) (if (> counter max-count) product (fact-iter (* counter product) (+ counter 1) max-count)))

28. Tree Recursion Fibonacci numbers : 0, 1, 1, 2, 3, 5, 8, 13, 21, ... 0 if n = 0 Fib( n ) = 1 if n = 1 Fib(n-1) + Fib(n-2) otherwise (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2))))))

29. Tree Recursion Fib(5) Fib(4) Fib(3) Fib(3) Fib(2) Fib(2) Fib(1) Fib(2) Fib(1) Fib(1) Fib(0) Fib(1) Fib(0) Fib(1) Fib(0)

30. Tree Recursion For Fibonacci numbers, Use linear iteration instead of tree recursion (define (fib n) (fib-iter 1 0 n)) (define (fib-iter a b count) (if (= count 0) b (fib-iter (+ a b) a (- count 1))))

31. Exponentiation – linear recursion (define (expt b n) (if (= n 0) 1 (* b (expt b (- n 1)))))

32. Exponentiation – linear iteration (define (expt b n) (exp-iter b n 1)) (define (exp-iter b counter product) (if (= counter 0) product (exp-iter b (- counter 1) (* b product))))

33. Exponentiation – fast method bn = (bn/2)2 if n is even bn = b * bn-1 if n is odd (define (fast-exp b n) (cond ( (= n 0) 1) ( (even? n) (square (fast-exp b (/ n 2)))) (else (* b (fast-exp b (- n 1)))))) (define (even? n) (= (remainder n 2) 0))

34. Greatest Common Divisor GCD( a , b ) is defined to be the largest integer that evenly divides both a and b. GCD( a , b ) = GCD( b , r ) where r is the remainder of a / b. GCD(206, 40)=GCD(40,6)=GCD(6, 4)=GCD(4, 2)=GCD(2, 0)=2 (define (gcd a b) (if (= b 0) a (gcd b (remainder a b))))

35. Higher-Order Procedures Build abstractions by assigning names to common patterns (define (cube x) (* x x x)) Procedures that manipulate data i.e. accept data as argument and return data What about procedures that manipulate procedures i.e. accept procedures as argument and return procedures Higher-Order Procedures

36. Procedures as Parameters (define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b)))) (define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

37. Procedures as Parameters (define (<name> a b) (if (> a b) 0 (+ (<term> a) (<name> (<next> a) b))))

38. Procedures as Parameters (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (define (sum-cubes a b) (define (inc x) (+ x 1)) (sum cube a inc b))

39. Procedures using Lambda Lambda – define anonymous (lambda (x) (+ x 1)) (lambda (<formal-parameters>) <body>) (define (sum-cubes a b) (sum (lambda (x) (* x x x)) a (lambda (x) (+ x 1)) b))

40. Procedures using Lambda Lambda – define anonymous (define (plus4 x) (+ x 4)) (define plus4 (lambda (x) (+ x 4))

41. Lambda Calculus A lambda expression describes a "nameless" function Specifies both the parameter(s) and the mapping Consider this function: cube (x) = x * x * x Corresponding lambda expr: (x) x * x * x Can be "applied" to parameter(s) by placing the parameter(s) after the expression ((x) x * x * x)(3) The above application evaluates to 27

42. Lambda Calculus Based on notation  (lambda (l) (car (car l))) l is called a "bound variable"; think of it as a formal parameter name Lambda expressions can be applied ((lambda (l) (car (car l))) '((a b) c d))

43. Lambda Examples > (define x 6) > (lambda (x) (+ x 1)) > (define inc (lambda (x) (+ x 1))) > (define same (lambda (x) (x))) > (if (even? x) inc same) > ((if (even? x) inc same) 5) 6

44. Lambda Examples > ((lambda(x) (+ x 1)) 3) 4 > (define fu-lst (list (lambda (x) (+ x 1)) (lambda (x) (* x 5)))) > fu-lst (#<procedure> #<procedure>) > ((second fu-lst) 6) 30

45. Internal Definitions Internal definitions: the special form, LET (let ( (x ‘(a b c)) (y ‘(d e f)) ) (cons x y)) * Introduces a list of local names (use define for top-level entities, but use let for internal definitions) * Each name is given a value

46. Using Let to Define LocalVariables f(x,y) = x(1 + xy)2 + y(1 – y) + (1 + xy)(1 – y) a = 1 + xy b = 1 – y f(x,y) = xa2 + yb + ab

47. Using Let to Define LocalVariables (define (f x y) (define a (+ 1 (* x y))) (define b (– 1 y)) (+ (* x (square a)) (* y b) (* a b)))

48. Using Let to Define LocalVariables (define (f x y) (let ((a (+ 1 (* x y))) (b (– 1 y))) (+ (* x (square a)) (* y b) (* a b)))

49. Using Let to Define LocalVariables (define (f x y) ((lambda (ab) (+ (* x (square a)) (* y b) (* ab))) (+ 1 (* x y)) (– 1 y)))

50. Using Let to Define LocalVariables (let ((<var1> <exp1>) (<var2> <exp2>) : (<varN> <expN>)) <body>)