0% found this document useful (0 votes)
86 views29 pages

Scheme: Variant of LISP: cs480 (Prasad) L12Scm 1

Scheme is a dialect of Lisp with influences from ALGOL. It features simple syntax, automatic garbage collection, and list processing capabilities. Scheme is commonly used for symbolic computing tasks like parsing, compiling, interpreting, and natural language processing due to its flexibility and ability to handle symbolic data through lists. Key language elements include literals, variables, procedure calls, definitions, conditionals, symbols, pairs, lists, and special forms. Core data types are symbols and lists, and evaluation is based on recursively evaluating nested lists.

Uploaded by

cecsdistancelab
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views29 pages

Scheme: Variant of LISP: cs480 (Prasad) L12Scm 1

Scheme is a dialect of Lisp with influences from ALGOL. It features simple syntax, automatic garbage collection, and list processing capabilities. Scheme is commonly used for symbolic computing tasks like parsing, compiling, interpreting, and natural language processing due to its flexibility and ability to handle symbolic data through lists. Key language elements include literals, variables, procedure calls, definitions, conditionals, symbols, pairs, lists, and special forms. Core data types are symbols and lists, and evaluation is based on recursively evaluating nested lists.

Uploaded by

cecsdistancelab
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Scheme : variant of LISP

cs480(Prasad)

L12Scm

Scheme
Scheme = LISP + ALGOL
symbolic list manipulation block structure; static scoping

Symbolic Computation
Translators
Parsers, Compilers, Interpreters.

Reasoners
Natural language understanding systems Database querying

Data / Text Processors


emacs
cs480(Prasad) L12Scm 2

Striking Features
Simple and uniform syntax Support for convenient list processing (Automatic) Garbage Collection Environment for Rapid Prototyping

Intrinsically generic functions Dynamic typing (flexible but inefficient) Compositionality through extensive use of lists. (minimizing impedance mismatch)
cs480(Prasad) L12Scm 3

Expressions
Literals Literals Variables
Identifier represents a variable. Variable reference denotes the value of its binding. x 5 ref

Variables

Procedure calls

numerals(2), strings( abc ), boolean( #t ), etc.

cs480(Prasad)

L12Scm

Scheme Identifiers
E.g., y, x5, +, two+two, zero?, list->string, etc (Illegal) 5x, y)2, ab c, etc Identifiers
reserved keywords variables Not case sensitive

pre-defined functions/constants ordinary

cs480(Prasad)

functions = procedures
L12Scm 5

Procedure Call (application)

cs480(Prasad)

L12Scm

Simple Scheme Expressions


(+ (+ 12 13) = 25 12 13) = 1.0
(positive? 25) = #t (negative? (- 1 0)) = #f (expt 2 10) = 1024 (sqrt 144) = 12 (string->number 12 8) = 10 (string->number ABC) = #f
7

(+ 2.2+1.1i 2.2+1.1i )

= 4.4+2.2i (* 2.2+1.1i 0+i ) = (-1.1+2.2i) (< 2.2 3 4.4 5) = #t

cs480(Prasad)

L12Scm

Scheme Evaluation Rule (REPL)


Expression
blank separated, parentheses delimited, nested list structure

Evaluation
Evaluate each element of the outerlist recursively. Apply the result of the operator expression (of function type) to the results of zero or more operand expressions.
cs480(Prasad) L12Scm 8

Lists
Ordered sequence of elements of arbitrary type (Heterogeneous)
Empty List : () 3-element list : (a b 3) Nested list : (1 (2.3 x) (a) =/= (a a) (a b) =/= (b a) ( a ) =/= ( ( a ) )

4)

Supports meta-programming
program manipulating programs
cs480(Prasad) L12Scm 9

Special Forms
Definition
(define <var> <expr>)

Conditional
(if <test> <then> <else>)! > (define false #f) > (if (symbol? a) (zero? 5) (/ 10 0) )

cs480(Prasad)

L12Scm

10

Symbols
Identifiers treated as primitive values.
Distinct from identifiers that name variables in program text. Distinct from strings (sequence of characters).

Some meta-programming primitives


quote symbol?

cs480(Prasad)

L12Scm

11

Symbolic Data : quote function

cs480(Prasad)

L12Scm

12

Pairs
(cons a b)
a (a . (b . ())) b Printed as: (a . b)

(cons

(cons

b ()) )

Printed as: (a b)

a
cs480(Prasad) L12Scm

()

13

(contd)
(cons a (cons a (cons b ())))
Printed as: (a a b)

a a
cs480(Prasad) L12Scm

()

14

List Functions
Operations
car, cdr, cons, null?, ... list, append, ... cadr, caddr, caaar, ...

Expressions
( length (quote (quote a)) ) ( length a ) = 2 ( length quote ) (cadar X) = (car (cdr (car X)))
cs480(Prasad) L12Scm 15

(define xl (a b c))
{ allocate storage for the list and initialize xl}

car, first :
(car xl)

list -> element


= = a (b c)

cdr :

list -> list { non-destructive } element x list -> list


= (a b c)
16

(cdr xl)

cons :

(cons a (b c))
cs480(Prasad) L12Scm

For all non-empty lists xl: (cons (car xl) (cdr xl)) = xl For all x and lists xl: (car (cons x xl)) = x (cdr (cons x xl)) = xl car : first element of the outermost list. cdr : list that remains after removing car.
(cons () ()) (cons () ())
cs480(Prasad) L12Scm

= ?? = (())
17

null? :

list -> boolean


= = = #t #f #f

(null? ()) (null? (a)) (null? 25)

list :

elements x ... -> list


=
(a (a) ab)

(list a (a) ab)

append :

list x ...-> list


=
(a 0)

(append () (list a) (0))

{ variable arity functions }


cs480(Prasad) L12Scm 18

Role of parentheses

cs480(Prasad)

L12Scm

19

Equivalence Test
(eq? (cons 3 ()) (cons 3 ()))
= #f

(define a (cons 3 ())) (define b (cons 3 ())) (eq? a b) (define c a) (eq? a c)


cs480(Prasad) L12Scm

= #f

= #t

20

Equivalent Scheme Expressions


( (car (list append list)) (cons a ()) (1 2 3) ) = ( append (a) (1 2 3) ) = (a 1 2 3) ( (cdr (cons car cdr)) (cons car cdr) ) = ( cdr = cdr
cs480(Prasad)

(car

. cdr)

L12Scm

21

Procedural vs Functional Style


procedural = imperative

cs480(Prasad)

L13FP

22

Abstracting Expressions

cs480(Prasad)

L13FP

23

Procedural/Imperative vs Functional
Program: a sequence of instructions for a von Neumann m/c. Computation by instruction execution. Repetition: Iteration. Modifiable or updateable variables.
cs480(Prasad)

Program: a collection of function definitions (m/c independent). Computation by term rewriting. Repetition: Recursion. Assign-only-once variables.
L13FP 24

Functional Style : Illustration


Definition : Equations sum(0) = 0 sum(n) = n + sum(n-1) Computation : Substitution and Replacement sum(2) = 2 + sum (2-1) = = 3
cs480(Prasad) L13FP 25

Paradigm vs Language
Procedural Style
i := 0; sum := 0; while (i < n) do begin i := i + 1; sum := sum + i end;

Functional Style
func sum(n:int) : int; begin if n = 0 then 0 else n + sum(n-1) end;

Storage efficient

No Side-effect
L13FP 26

cs480(Prasad)

Role of Variables
Imperative (read/write) i 0 1 sum 0 1 Functional (read only) 3 n1 2 n2 1 n3 2 3
6 3 1

3 6

... ... sum1 sum2 sum3

cs480(Prasad)

L13FP

27

Bridging the Gap


A tail recursive definition can be automatically optimized for space by translating it into an equivalent while-loop.
func sum(n : int, r : int) : int; begin if n = 0 then r else sum(n-1, n+r) end

So, Scheme does not have loop-constructs.


cs480(Prasad) L13FP 28

Iteration vs Recursion Recursion = Iteration + Stack Iteration = Tail Recursion


cs480(Prasad) L13FP 29

You might also like