0% found this document useful (0 votes)
8 views

Scheme 1

Uploaded by

eros amendolara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Scheme 1

Uploaded by

eros amendolara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

A Brief Introduction to Scheme (I)

Philip W. L. Fong
[email protected]

Department of Computer Science


University of Regina
Regina, Saskatchewan, Canada
Scheme

Scheme I – p.1/44
Scheme: Feature Set
A small dialect of Lisp
Support symbolic list processing
Garbage collected
Functional (although not purely functional)
Better than Lisp . . .
first-class procedures
tail-call optimization
continuations
user-defined syntactic extensions
the first Lisp dialect to support lexical scoping

⇒ best for building sophisticated interpreters

Scheme I – p.2/44
Scheme: What’s the Big Deal?
Why learn about functional programming with a Lisp
dialect . . .
Python has lambda expressions, etc
see also Ruby
XSLT is functional
its ancester DSSSL is a Scheme dialect
Scheme and Lisp are popular extension
languages
Emacs: a scriptable UNIX editor
Sawfish: an scriptable X11-based window
manager
Guile: the GNU extension language

Scheme I – p.3/44
Scheme: What’s the Big Deal?
Why learn about functional programming with a Lisp
dialect . . .

Lisp is worth learning for the profound


enlightenment experience you will have when
you finally get it; that experience will make you
a better programmer for the rest of your days,
even if you never actually use Lisp itself a lot.

Eric Raymond “How To Become a Hacker”

Scheme I – p.4/44
Scheme: What’s the Big Deal?
Why learn about functional programming with a Lisp
dialect . . .
MIT teaches Scheme to their 1st-year CS
students
Waterloo is following suit
SFU is beginning to teach Python to their 1st-year
CS students . . .
. . . because it helps them to learn functional
programming concepts such as lambda
expressions and higher-order functions.

Scheme I – p.5/44
Scheme: Standardization

ANSI/IEEE standard
The Revised 5 Report on the Algorithmic Language
Scheme [R5 RS]

Scheme I – p.6/44
Interacting with Scheme

Scheme I – p.7/44
The Scheme REPL
Scheme prompt:
>

OScheme I – p.8/44
The Scheme REPL
Scheme prompt:
> (* 2 (cos 0) (+ 4 6))

OScheme I – p.8/44
The Scheme REPL
Scheme prompt:
> (* 2 (cos 0) (+ 4 6))
20

OScheme I – p.8/44
The Scheme REPL
Scheme prompt:
> (* 2 (cos 0) (+ 4 6))
20
The read-evaluate-print loop (REPL):
loop
read an expression
evaluate the expression
print the result of evaluation
end loop
Evaluation:
evaluate
expression −−−−−−−−→ value

Scheme I – p.8/44
Scheme Syntax
Let’s examine the example in details:
> (* 2 (cos 0) (+ 4 6))
20

OScheme I – p.9/44
Scheme Syntax
Let’s examine the example in details:
> (* 2 (cos 0) (+ 4 6))
20
constants: 0, 2, 4, 6
identifiers: cos, +, *
structured forms: (...)
Brackets are not for grouping!
In C/C++, “((1))” is the same as “1”
In Scheme, “((1))” is NOT “1”
Brackets are for delimiting structured forms.
procedure application:(proc arg 1 ... arg n )
“+”: name of the addition procedure
case-insensitive: (cos 0) ≡ (COS 0)

Scheme I – p.9/44
Order of Evaluation
Order of Evaluation:When there are multiple
subexpressions, which subexpression should be
evaluated first?
Applicative Order:
(proc arg 1 ... arg n )
1. evaluate arg 1 , . . . , arg n
2. evaluate proc
3. apply proc to arg 1 , . . . , arg n

Scheme I – p.10/44
Numeric Procedures
Procedure Meaning
(+ x1 ... xn ) The sum of x1 , . . . , xn
(* x1 ... xn ) The product of x1 , . . . , xn
(- x y ) Subtract y from x
(- x) Minus x
(/ x y ) Divide x by y
(max x1 ... xn ) The maximum of x1 , . . . , xn
(min x1 ... xn ) The minimum of x1 , . . . , xn
(sqrt x) The square root of x
(abs x) The absolute value of x

Scheme I – p.11/44
Exercise

Look up from [TSPL3] Chapter 6 the answers to the


following questions:
What procedure computes xn ?
What is the difference between the procedures
remainder and modulo?

Scheme I – p.12/44
Scheme Programs

Scheme I – p.13/44
lambda Expressions
An anonymous procedure:
(lambda (x) (* x 2))
(lambda ...) - a “name-less” procedure
(x) - parameter list
(* x 2) - procedure body
Applying the procedure:
> ((lambda (x) (* x 2)) 3)
6
The value of a procedure application is obtained by
1. substituting the actual arguments for the formal parameters in
the procedure body
2. evaluating the procedure body
Scheme I – p.14/44
Top-Level Definitions
> (define y 3)

OScheme I – p.15/44
Top-Level Definitions
> (define y 3)
> y
3

OScheme I – p.15/44
Top-Level Definitions
> (define y 3)
> y
3
> (define double
(lambda (x)
(* x 2)))

OScheme I – p.15/44
Top-Level Definitions
> (define y 3)
> y
3
> (define double
(lambda (x)
(* x 2)))
> (double 3)
6

OScheme I – p.15/44
Top-Level Definitions
> (define y 3)
> y
3
> (define double
(lambda (x)
(* x 2)))
> (double 3)
6
> (double (double 3))
12

Scheme I – p.15/44
Storing Programs in Files

Demonstration
Edit program
Syntax-check program
Run program

Scheme I – p.16/44
Functional Programming

Scheme I – p.17/44
Imperative Programming

Mutable global state


assignment statements modify variable bindings
Command = state transformer
command
state1 −−−−−−−−−→ state2

Program = sequence of commands

Scheme I – p.18/44
Functional Programming

No side effect allowed


No assignment statement!
Every expression denotes a unique value:
evaluate
expression −−−−−−−−→ value

Referential transparency
variable bindings do not mutate
evaluating an expression always yields the same
value

Scheme I – p.19/44
The Functional Fragment of Scheme

Scheme supports both the imperative and


functional paradigm of programming
In the first half of the course, we focus on the
functional fragement of Scheme.
i.e., programming with expressions that do not
produce side effects

Scheme I – p.20/44
Conditional Expressions

Scheme I – p.21/44
Boolean Values

Boolean values: #t, #f


Anything that is not #f is considered true.
A procedure that returns a boolean value is called a
predicate.

Scheme I – p.22/44
Example: absolute-value
(
−x if x < 0
abs(x) =
x otherwise

(define absolute-value
(lambda (x)
(if (negative? x)
(- x)
x)))

Scheme I – p.23/44
Conditional Form if
(if test-expression
true-expression
false-expression)

1. Evaluate test-expression.
2. If test-expression evaluates to true then evaluate
true-expression and return its value.
3. Otherwise, evaluate false-expression and return its
value.

Scheme I – p.24/44
Numeric Predicates

Predicate Meaning
(zero? x) x is zero
(positive? x) x is positive
(negative? x) x is negative
(even? x) x is even
(odd? x) x is odd

Scheme I – p.25/44
Relational Predicates

Predicate Meaning
(= x y ) x is equal to y
(< x y ) x is less than y
(< x y ) x is greater than y
(<= x y ) x is no greater than y
(>= x y ) x is no less than y

Scheme I – p.26/44
Type Predicates

Predicate Meaning
(number? x) x is a number
(boolean? x) x is a Boolean value
(i.e., #t, #f)

Scheme I – p.27/44
Example: sign

1
 if x > 0
sign(x) = 0 if x = 0

−1 otherwise
(define sign
(lambda (x)
(cond
((positive? x)
1)
((zero? x)
0)
(else
-1))))

Scheme I – p.28/44
Conditional Form cond
(cond
(test 1 expr 1 )
(test 1 expr 2 )
...
(test n expr n )
(else default ))

1. Evaluate test 1 , test 2 , . . . , test n in the order they


appear.
2. If some test i evaluates to true, then stop evaluating
the rest of the test expressions, but instead evaluate
expr i and return its value.
3. Otherwise, evaluate default and return its value.

Scheme I – p.29/44
More Conditional Forms
Forms Meaning
(or x1 ... xn ) Logical or
(and x1 ... xn ) Logical and
(not x) Logical negation
These are not procedures.
Example: (or x1 ... xn )
Evaluate x1 , . . . , xn in the order they appear
If any of the arguments evaluates to true, return
true rightaway without evaluating the rest of the
arguments.
Otherwise, return #f.

Scheme I – p.30/44
Exercise

Rewrite (not x ) into an equivalent expression using


only the if form.
Rewrite (and x 1 ... x n ) into an equivalent
expression using only the cond form.
The definition of and in terms of cond is only an
approximation. Check out [TSPL3] Sect. 5.3 to see
why.

Scheme I – p.31/44
Equational Reasoning

Scheme I – p.32/44
Equational Reasoning

We grow up knowing equational reasoning by


heart:
(1 + 2) × (3 − 4)
= 3 × (3 − 4)
= 3 × −1
= −3
Because of referential transparency, the behavior of
functional programs can be understood using
equational reasoning!

Scheme I – p.33/44
Example: double

(double (double 3))


= (double ((lambda (x) (* x 2)) 3))
= (double (* 3 2))
= (double 6)
= ((lambda (x) (* x 2)) 6)
= (* 6 2)
= 12

Scheme I – p.34/44
Example: absolute-value

(absolute-value -3)
= ((lambda (x) (if (negative? x) (- x) x)) -3)
= (if (negative? -3) (- -3) -3)
= (if #t (- -3) -3)
= (- -3)
= 3

Scheme I – p.35/44
Recursion

Scheme I – p.36/44
Recursion vs Iteration

In imperative programming languages, repetition can


be achieved by iterative constructs such as for or
while.
In functional programming languages, repetition is
mainly achieved by recursion.

Scheme I – p.37/44
Example: triangular (1)
triangular(n) = 1 + 2 + . . . + n
(
1 if n = 1
=
n + triangular(n − 1) otherwise

(define triangular
(lambda (n)
(if (= n 1)
1
(+ n (triangular (- n 1))))))

Scheme I – p.38/44
Example: triangular (2)
> (trace triangular)
> (triangular 3)
|(triangular 3)
| (triangular 2)
| |(triangular 1)
| |1
| 3
|6
6
> (untrace triangular)

Scheme I – p.39/44
Example: triangular (3)
(triangular 3)
= ((lambda (n)
(if (= n 1) 1 (+ n (triangular (- n 1)))))
3)
= (if (= 3 1) 1 (+ 3 (triangular (- 3 1))))
= (if #f 1 (+ 3 (triangular (- 3 1))))
= (+ 3 (triangular (- 3 1)))
= (+ 3 (triangular 2))
= (+ 3 (if (= 2 1) 1 (+ 2 (triangular (- 2 1)))))
= (+ 3 (+ 2 (triangular (- 2 1))))
= (+ 3 (+ 2 (triangular 1)))
= (+ 3 (+ 2 (if (= 1 1) 1 (+ 1 (triangular (- 1 1))))))
= (+ 3 (+ 2 1))
= (+ 3 3)
= 6
Scheme I – p.40/44
Example: power (1)
(
n 1 if n = 0
x =
x × xn−1 if n > 0
(define power
(lambda (x n)
(if (zero? n)
1
(* x (power x (- n 1))))))

Scheme I – p.41/44
Example: power (2)
> (power 2 4)
|(power 2 4)
| (power 2 3)
| |(power 2 2)
| | (power 2 1)
| | |(power 2 0)
| | |1
| | 2
| |4
| 8
|16
16

Scheme I – p.42/44
Exercise

Use equational reasoning to trace the execution of


(power 2 4).

Scheme I – p.43/44
Lecture Summary
This lecture and the next cover [TSPL3] Chap. 1–2.
In this lecture . . .
REPL
the numeric fragment of Scheme
functional programming with Scheme
equational reasoning
lambda expression
top-level definition
Boolean values
conditional forms
simple examples of recursion

Scheme I – p.44/44

You might also like