Getting Started - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Getting Started

Description:

(quote ( 2 3 4)) or '( 2 3 4) A quoted expression can be evaluated later with eval ... Multiple applications of car can be combined into one function call. ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 28
Provided by: davidwoo4
Category:
Tags: getting | started

less

Transcript and Presenter's Notes

Title: Getting Started


1
Getting Started
  • Lectures on
  • The Scheme Programming Language, 2nd Ed.
  • R. Kent Dybvig

2
Expressions
  • Expressions in Scheme have the form (E1,E2,,En)
    where E1 is an operator and E2En are evaluated
    as operands
  • ( 2 3 4 ) evaluates to 9
  • Scheme uses inner-most evaluation
  • ( 2 3 ( 2 3) 4) evaluates to 14

3
Quoting
  • Evaluation of an expression can be delayed by
    quoting
  • (quote ( 2 3 4)) or '( 2 3 4)
  • A quoted expression can be evaluated later with
    eval
  • (eval (quote ( 2 3 4)))
  • (eval ' ( 2 3 4))

4
Scheme Interpreter
  • The bottom screen in Dr. Scheme is implemented
    with a Read-Eval-Print loop
  • The top screen is used to develop longer programs
    and to save them
  • Programs in the top screen are evaluated when
    you click on the execute button

5
Define
  • Define is used to establish a binding between
    a variable and an object
  • (define x 3)
  • (define y abcde)
  • (define z '( 2 3 4))
  • Bindings can change during execution
  • (define z 8)

6
Dynamic Typing
  • Types are associated with values at run-time
  • Variables assume the type of the value to which
    they are bound during execution
  • Types associated with variables can change during
    execution

7
Garbage
  • An object created during execution has an
    unlimited lifetime or extent
  • All objects are created on a heap
  • Objects that are no longer bound to any variable
    can be garbage collected and the storage they
    occupy reused

8
Scheme Likes Lists
  • Scheme is a derivative of Lisp, another
    functional programming language
  • Lists are used for symbolic computation
  • Scheme has built in functions for working with
    lists
  • (list 1 2 3) creates a list with three items
  • (define x (list 1 2 3)) associates x with a list

9
Scheme Likes Lists
  • The car function returns the first element on a
    non-empty list.
  • (car (list 1 2 3)) returns 1
  • (car ' (1 2 3)) returns 1
  • (car ' ((1 2) 3 4)) returns (1 2)
  • (define x (list 1 2 3))
  • (car x) returns 1
  • ((car (list - /)) 3 4) returns 7

10
Multiple Car Operators
  • (car (car '((1 2) 3 4))) returns 1
  • Multiple applications of car can be combined into
    one function call. For example, two applications
    of car can be written as caar
  • (caar '((1 2) 3 4)) returns 1

11
Cdr
  • The cdr function accepts a non-empty list and
    returns the list derived from deleting the first
    element of the given list
  • (cdr (list 1 2 3)) returns (2 3)
  • (cdr '((1 2) 3 4) returns (3 4)
  • Multiple cdrs are possible
  • (cddr (1 2 3 4)) returns (3 4)

12
Cons
  • The cons function takes two arguments
  • The second argument is usually a list
  • Cons returns the list derived by adding the first
    argument onto the front of the given list
    (argument two)
  • (cons 1 (list 2 3 4)) returns (1 2 3 4)
  • (cons (list 1 2) (list 3 4) returns ((1 2) 3 4)

13
Cons
  • Cons builds dotted pairs
  • (cons 1 2) returns (1 . 2)
  • Dotted pairs are used to create lists
  • (list 1 2 3) returns (1 2 3)

1
2
1
2
3
14
Cons
  • (cons 1 (cons 2 (cons 3 ( )))) returns (1 2 3)
  • (cons 1 (cons 2 3)) returns (1 2 . 3)

1
2
3
1
2
3
15
Empty Lists
  • The empty list contains no elements
  • (list )
  • '( )
  • (cons 1 ' ( )) returns (1)
  • (cons 1 (list )) returns (1)
  • (cdr (list 1)) returns ( )

16
Lambda Builds Functions
  • Functions are created using lambda expressions
  • (lambda (x y) ( x y))
  • Applying the previous function
  • ((lambda (x y) ( x y)) 2 3) returns 5
  • (define add
  • (lambda (x y) ( x y)))
  • (add 2 3)

17
Square Function
  • (define square
  • (lambda (x)
  • ( x x)))
  • (square 3)
  • (square 4)

18
Creating Local Scope
  • Let expressions establish local variables and
    scope
  • (let ((var val) ...) exp1 exp2 ...)
  • (let ((x 3)(y 4)) ( x y) )
  • (let ((x 3)(y 4)) ( x y) ( x y))

19
Let for Simplification
  • ( ( 4 4) ( 4 4))   returns 32
  • ( 4 4) is evaluated two times
  • (let ((a ( 4 4)))   ( a a)) 
    returns 32

20
Nested Lets
  • Lets can be nested
  • (let ((a 4) (b -3))   (let ((a-squared ( a a))
             (b-squared ( b b)))    ( a-squared b-
    squared)))  returns 25

21
Shadowing
  • (let ((x 1))  (let ((x ( x 1)))    ( x x))) 
    returns 4
  • Inner x shadows the outer x

22
Free and Bound Variables
  • (let ((x 'a))
  • (let ((f (lambda (y) (list x y))))
  • (f 'b)))
  • X is free in the lambda expression (lambda (y)
    (list x y))
  • Y is bound in the lambda expression
  • (lambda (y) (list x y))

23
Bindings Carry Forward
  • (let ((f (let ((x 'a))
  • (lambda (y) (cons x y)))))
  • (f 'b))
  • Alternatively,
  • (define f (let ((x 'a))
  • (lambda (y) (cons x y))))
  • (f 'b)

24
Let as Syntatic Extension
  • Let can be defined in terms of lambda
  • (let ((x 'a))
  • (cons x x))
  • ((lambda (x) (cons x x)) 'a)

25
Variant Lambda Expressions
  • The formal parameter specification for lambda can
    be in any of the following three forms
  • a proper list of variables, (var1 ... varn)
  • a single variable, varr, or
  • an improper list of variables, (var1 ... varn .
    varr).

26
Parameter List as a Single Variable
  • (define shorten
  • (lambda x
  • (cdr x)))
  • (shorten 1 2 3) returns (2 3)
  • Parameters 1 2 3 are passed as a list which is
    referenced by x

27
Capturing Extra Parameters
  • (define shorten
  • (lambda (x y . z)
  • (cdr z)))
  • (shorten 1 2 3 4 5)
  • Extra parameters 3 4 5 are stored as a list in z
Write a Comment
User Comments (0)
About PowerShow.com