1 / 46

SCHEME

SCHEME. By: Krista and Brett. What is Scheme?. Best known for its functional style of programming One of two main dialects of Lisp Developed in 1975 MIT AI lab Gerald Jay Sussman Guy L. Steele, Jr. Based on a minimalist philosophy Less is more 50 page language standard. Simplicity

marnin
Download Presentation

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. SCHEME By: Krista and Brett

  2. What is Scheme? • Best known for its functional style of programming • One of two main dialects of Lisp • Developed in 1975 • MIT AI lab • Gerald Jay Sussman • Guy L. Steele, Jr. • Based on a minimalist philosophy • Less is more • 50 page language standard

  3. Simplicity Language itself is not very large Fairly easy to understand syntax NOT left to right Ex. (+ 2 3) Expressiveness Challenging language to fully understand Full potential requires lots of study and practice Highly portable Block structured Lexically scoped Abstract power makes it easy to separate system specific optimizations from reusable code Dynamic type checking Efficiency Interpreter/compiler can be small, fast, and highly reliable Reliability High Language Characteristics

  4. Uniform evaluation rules, and uniform treatment of data types Does NOT have the concepts of: Pointers Un-initialized variables Specialized looping constructs Explicit storage management All data types are equal What one can do to one data type, one can do to all data types Multiparadigm- (including object-oriented) allows users to better match their solution style to the style of the problems to be solved. Its formal underpinnings make reasoning about programs much easier. Language Characteristics cont

  5. Syntax • Keywords • Variables • Structured forms • Constant data • Numbers • Characters • Strings • Quoted vectors, lists, symbols, etc • Whitespace • Comments

  6. Syntax cont • Keywords, variables, symbols = identifiers • a-z • A-Z • 0-9 • ~!@$%^&_?!.+-*/<=>: • ABCDE=abcde=AbCdE=same identifier; case unimportant with identifiers • Vectors: preceded by #( terminated by ) • Ex. #(I am a vector) • Strings: enclosed by “” ex. “I am a string” • Characters: preceded by #\ ex. #\z • Case important for strings and characters • Numbers: integers, ratios, floating-point, scientific notation, complex- rectangular or polar notation

  7. Basic Data Types (all equal) • Atom: string of characters beginning with a letter, digit or special character other than “(“ or “)” parentheses • Numbers: 1, 2.9, 20e10 • Characters: #\a #\x • Symbols: food, yum • Booleans: #t #f • Compound Objects • List- ordered set of elements consisting of atoms or other lists • enclosed by parenthesis • Ex (list 1 ‘a 3/2) => (1 a 3/2) • Vectors (arrays) • #(1 2 ”fun" #\x) => #4(1 2 “fun” #\x) • Strings “I am a string” => “I am a string”

  8. Basic Data Types • Unprintable Data Types: • Procedures (functions) • Continuations (gotos) • Ports (opened file) • Predicates- procedures that return true (#t) or false (#f); end in ? • (null? ()) => #t • (string? (+ 1 2)) => #f

  9. Memory and Garbage Collection • Four kinds of memory: • Stack that is used for recursive procedure calls. • Heap that is used for dynamically allocated objects, like cons cells and strings. Storage used for objects in the heap that become unreferenced is eventually reclaimed by garbage collection. • Constant space that is used for allocated objects, like the heap. Unlike the heap, storage used for objects in constant space is not reclaimed by garbage collection. Constant space is used for objects that are essentially permanent, like procedures in the runtime system. • Extra storage that is used by the microcode • Storage required to hold contents of an object-> dynamically allocated as necessary and retained until no longer needed • Automatically deallocated using a garbage collector • Garbage collector periodically recovers storage used by inaccessible objects • Simple atomic values, ie. small integers, characters, booleans, and empty list: • Immediate values -> no allocation or deallocation overhead

  10. Lists:The Basic Structured Data Type • null? - returns #t if list is empty, #f if not • list- returns a list constructed from its arguments • length-returns the length of the list • reverse- returns the list in reverse order • append- returns the concatenation of two lists

  11. Functions to Manipulate Lists: • car returns the head of the list • (car ‘(1 2 3)) => 1 • cdr (“coulder”) returns the rest of the list; everything after the head • (cdr ‘(1 2 3)) => (2 3) • cons joins the head to the rest of the list • (cons 1 ‘(2 3)) => (1 2 3)

  12. Predicates • (< 1 2) => #t • (>= 3 4) => #f • (list (< 5 9)(= 3(/ 6 2))(> 3 4)) => (#t #t #f) • (null? ‘()) => #t • (= 1 2) => #f • boolean? pair? procedure? symbol? number? string? null? zero? odd? even?

  13. Equivalence Predicates • = can be used to test the equality of two numbers • eq? can be used for testing sameness of symbols, which are identical if they are spelled exactly the same way • returns #t if its arguments are identical in every possible way, otherwise it returns #f • (eq? 'foo 'foo) => #t (eq? 'foo 'fooo) => #f • eqv? defines an equivalence between objects • returns #t if the objects compared can be regarded as the same object • Ex. (eqv? (* 2 2) (sqrt 16)) => #t • equal? compares the contents of pairs, vectors and strings and applies eqv? on other objects • Ex. (equal? 3 (/ 6 2)) => #t

  14. Conditional Expressions • Using if • (if test-expression (if (< x y) then-branch “x < y" else-branch) “x >= y")) • Using cond • (cond (condition1 consequent1) (condition2 consequent2) . . . (else alternative)) (cond ((> x y) 'greater) ((< x y) 'less) (else 'equal)))

  15. Lambda • Use to create a function • Does not give its function a name • Ex. (lambda (formal parameters) (body)) • ((lambda (x) (* x x)) 5) => 25

  16. Objects • Scheme supports many types of data values, or objects, including characters, strings, symbols, lists or vectors of objects, and a full set of numeric data types, including complex, real, and arbitrary-precision rational numbers. • All objects are first-class data values-> retained indefinitely • may be passed freely as arguments to procedures • returned as values from procedures • combined to form new objects • in contrast with many other languages where composite data values such as arrays are either statically allocated and never deallocated, allocated on entry to a block of code and unconditionally deallocated on exit from the block, or explicitly allocated and deallocated by the programmer.

  17. Bindings • When procedures are created, the bindings of all variables occurring within the bodies, excluding formal parameters, are retained with the procedures. • After a procedure is applied to a sequence of actual parameters • The formal parameters are bound to the actual parameters • The retained bindings are restored • The body is evaluated

  18. Local Binding - let • let is the keyword for local variable binding • Expressions are all outside the scope of the variables • No ordering is applied for evaluating expressions • Therefore, choose let when values are independent of the variables and ordering is unimportant

  19. let(notice: simultaneous declaration) • Swapping doesn’t require a temporary variable… • (let ((n 7) (m 2)) • (let ((n m) (m n)) • (list n m))) =>(2 7)

  20. Local Binding – let* • let* is very similar to let • Except expressions are evaluated in a left to right sequential order • Each of these expressions is also within the scope of the variables to the left. • Any let* expression can be converted to a set of nested let expressions • Choose let* when ordering is important

  21. let*(notice: order importance) • (let ((a 6)) • (let* ((a 4) • (b a) • (c (* b 2))) • (/ a c))) => 1/2

  22. let vs. let* • (let ((a 6)) • (let ((a 4) • (b a) • (c (* b 2))) • (/ a c))) => Error: reference to undefined identifier: b

  23. Local Binding - letrec • Again, letrec is similar to let and let* • Except all expressions are evaluated with the scope of all the variables • Allows for the definition of mutually recursive procedures • Evaluation order is unspecified like with let • Choose letrec over let or let* when there is a circular dependency on the variables

  24. letrec • (define (fib n) • (letrec ((fib-num (lambda (n a b) • (if (= n 0) • a • (fib-num (- n 1) b (+ a b)))))) • (fib-num n 0 1))) • (fib 0) => 0 • (fib 1) => 1 • (fib 2) => 1 • (fib 3) => 2 • (fib 4) => 3 • (fib 5) => 5 • (fib 6) => 8 • (fib 11) => 89 • (fib 20) => 6765

  25. Global Binding – define • “top level” – often outside the scope of lambda, let, let*, letrec • Always visible except when shadowed by a local binding • Allows for either syntax definitions or variable definitions • Any set of definitions can be enclosed and grouped by begin

  26. define – syntax definition • (define cube • (lambda (a)(* (* a a) a))) • (cube 4) => 64

  27. define – variable definition(notice: scoping rules) • (define fun1 • (lambda (n) • (* n 10))) • (let ((i 2) • (j 5)) • (define fun1 • (lambda (k) • (* (+ k i) j))) • (fun1 5)) => 35 • (fun1 5) => 50

  28. Assignment – set! • set! is used to alter an already existing binding within a certain scope • Assignments first evaluate an expression then assigns the variable to the new value • Useful for: • implementing changes in state such as in boolean variables • Caching values

  29. set! • (define coin-flip • (let ((heads #t)) • (lambda () • (set! heads (not heads)) • (if heads "heads" "tails")))) • (coin-flip) => “tails” • (coin-flip) => “heads” • (coin-flip) => “tails”

  30. Scheme Day 2 By: Krista and Brett

  31. Quick Scheme Review from Monday • Developed in 1975- used mostly for AI • Minimalist philosophy- less is more • Basics- atoms, lists, expressions (everything is an expression and has a value), data types (all equal), ; comments • Operations to manipulate lists • car, cdr, cons, list, append, reverse, null?, length

  32. Quick Scheme Review from Monday • Predicates • End in ? • Return #t or #f • Equivilance predicates • = test the equality of two numbers; • eq? testing sameness of symbols • eqv? defines an equivalence between objects • equal? compares the contents of pairs, vectors & strings & applies eqv? on other objects • Control Structures • and, or, if, cond • Programming • Lambda, let, let*, letrec, define, set

  33. Scheme • REMEMBER… - SCHEME IS A MULTIPARADIGM LANGUAGE • CAN BE SEEN AS: • FUNCTIONAL • IMPERATIVE • OBJECT ORIENTED

  34. Scheme as a Functional Language • “Evaluate an expression and use the resulting value for something” • Treats computation as the evaluation of mathematical functions- avoids state and mutable data • Emphasizes the application of functions • Based on Lambda calculus-> uses functions • Higher order/First class- can accept functions as arguments and results of other functions

  35. Scheme as a Imperative Language • “First do this and next do that” • The most fundamental imperative Scheme construct is the assignment set! • Other imperative constructs: • (begin x1 ... xn) • The iterative do control structure • The input output procedures • The list, string, and vector mutators • As a notational convention, most imperative abstractions in Scheme ends with "!” • Emphasizes changes in state

  36. Scheme as a Object-Oriented Language • See a closure as an object • A function definition can be interpreted as a class, and that a function call can play the role of an object. In other words, certain lambda expressions will be regarded as classes, and certain closures will be seen as objects. • Due to the first class status of functions, and due to the use of static binding of free names, it is possible to interpret a closure as an object • With this interpretation, it is possible to regard certain function definitions as classes

  37. Closures • A function object represents a function at run time. A function object is created as the value of a lambda expression • A function object is also known as a closure. • Functions as closures: • Capturing of free names in the context of the lambda expression • Static binding of free names • A closure is represented as a pair of function syntax and values of free names • A closure is a function that captures the bindings of free variables in its lexical context. • A value is bound initially, and each call after changes the current value-> does not revert back to initial value • Do not rebind to the initial value each time the function is called

  38. set! • (define coin-flip • (let ((heads #t)) • (lambda () • (set! heads (not heads)) • (if heads "heads" "tails")))) • (coin-flip) => “tails” • (coin-flip) => “heads” • (coin-flip) => “tails”

  39. Continuations • A closure which: • Represents the “future” of a computation from a given point • Never returns to its caller • Usually expects one argument- the value to be returned from the point at which the continuation was created • First class functions • Can be invoked numerous times • Can be used to create nearly any control-flow structure • Wide variety of control abstractions: • Go-tos, mid-loop exits, multilevel returns, exceptions, iterators, call-by-name parameters, coroutines

  40. Continuations cont • Call-with-current-continuation call/cc • Takes a single argument (function) f, calls f, passing as argument a continuation c (a closure) that captures the current program counter and referencing environment. At any point in the future, f can call c to reestablish the saved environment.

  41. Continuations example (define return #f) (+ 1 (call/cc (lambda (test) (set! return test) 1))) => 2 (return 8) => 9 (return 15) => 16

  42. Currying • Curried functions are very useful building blocks in the functional paradigm • Lisp’s parenthesis notation can make currying somewhat difficult in Scheme • The inclusion of parentheses is important because it distinguishes the application of one argument to a curried function from the application of two arguments to a non-curried function (define (curry2 f) (lambda(x) (lambda(y) (f x y)))) (define (uncurry2 f) (lambda (x y) ((f x) y)))

  43. Iterators • Iteration is Scheme uses the special form for-each and do • (define sub-vect (for-each (lambda x y) (display (- x y) (newline)))) (sub-vect (list 3 6) (list 1 3) => 2 => 3 • (define fact (lambda (n) (do ((i n (- i 1)) (a 1 (* a i))) ((zero? i) a)))) (fact 2) => 2 (fact 4) => 24

  44. Classroom Exercises • 1) Convert the following arithmetic into a Scheme expression and evaluate: • (7 * 4 – 8) / 10 • (/ (- (* 7 4) 8) 10) => 2 • (1.7 + 15) * (5 – 6.3 / 2.1) • (* (+ 1.7 15) (- 5 (/ 6.3 2.1)) => 33.4

  45. Classroom Exercises • 2) What results from the following predicates? • (integer? 29) => #t • (integer? 4.5) => #f • (real? 4.0+0.0i) => #t • (complex? 3.3-2.0i) => #t • (vector? “xyz”) => #f • (vector? ‘#(x y z)) => #t

  46. Classroom Exercises • 3) Determine the value of this expression and explain how it is derived: • (let ((a 18)) (* a (let ((a (/ a 9))) (+ a a))))) => 72 We come up with 72 because a is 18 outside the second let and a becomes 2 (divided by 9) inside it. We then add 2 to itself which yields 4 and then multiply by the outside a (4 * 18 = 72)

More Related