scheme by: krista and brett. what is scheme? best known for its functional style of programming one...

Post on 26-Dec-2015

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

Language Characteristics • 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 cont

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

Syntax

• Keywords• Variables• Structured forms• Constant data

– Numbers– Characters– Strings– Quoted vectors, lists, symbols, etc– Whitespace– Comments

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

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”

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

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

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

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)

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?

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

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)))

Lambda

• Use to create a function

• Does not give its function a name

• Ex. (lambda (formal parameters) (body))– ((lambda (x) (* x x)) 5) => 25

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.

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

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

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)

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

let*(notice: order importance)

• (let ((a 6))• (let* ((a 4)• (b a)• (c (* b 2)))• (/ a c))) => 1/2

let vs. let*

• (let ((a 6))• (let ((a 4)• (b a)• (c (* bb 2)))• (/ a c))) =>

Error: reference to undefined identifier: b

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

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

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

define – syntax definition

• (define cube• (lambda (a)(* (* a a) a)))• (cube 4) => 64

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

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

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”

Scheme Day 2

By: Krista and Brett

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

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

Scheme

• REMEMBER…- SCHEME IS A MULTIPARADIGM LANGUAGE- CAN BE SEEN AS:

- FUNCTIONAL- IMPERATIVE- OBJECT ORIENTED

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

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

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

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

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”

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

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.

Continuations example

(define return #f)

(+ 1 (call/cc (lambda (test)

(set! return test) 1))) => 2

(return 8) => 9(return 15) => 16

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)))

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

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

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

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)

top related