0% found this document useful (0 votes)
16 views35 pages

Scheme & Racket-Introduction: Session 4

The document provides an introduction to Scheme, a programming language based on LISP, covering its foundational concepts such as S-expressions, function definitions, and various data types including numbers, strings, and booleans. It explains how to define variables and functions, as well as control structures like conditionals and boolean operators. Additionally, it includes examples of common functions and operations in Scheme, illustrating how to perform calculations and manipulate data.

Uploaded by

pouriakhamseh
Copyright
© © All Rights Reserved
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)
16 views35 pages

Scheme & Racket-Introduction: Session 4

The document provides an introduction to Scheme, a programming language based on LISP, covering its foundational concepts such as S-expressions, function definitions, and various data types including numbers, strings, and booleans. It explains how to define variables and functions, as well as control structures like conditionals and boolean operators. Additionally, it includes examples of common functions and operations in Scheme, illustrating how to perform calculations and manipulate data.

Uploaded by

pouriakhamseh
Copyright
© © All Rights Reserved
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/ 35

Scheme & Racket- Introduction

Session 4
Background
 Scheme is based on LISP which was developed by John McCarthy in the
mid 50s.

 LISP stands for LIST Processing, not Lots of Irritating Silly Parentheses.

 Functions and data share the same representation: S-Expressions.

 A basic LISP implementation needs six functions: cons, car, cdr, equal,
atom, cond.

 Scheme was developed by Sussman and Steele in 1975.


S-Expressions
An S-Expression is a balanced list of parentheses.
More formally, an S-expression is
1. a literal (i.e., number, boolean, symbol, character,
string, or empty list.
2. a list of s-expressions.

Literals are sometimes called atoms.


S-Expressions _ Examples
S-Expressions as Trees
 An S-expression can be seen as a linear representation
of tree-structure:
S-Expressions as Function Calls
 A special case of an S-expression is when the first element of a list is a
function name.

 Such an expression can be evaluated.

>(+ 4 5)
9
> (add-five-to-my-argument 20)
25
> (draw-a-circle 20 45)
#t
S-Expressions as Functions
 As we will see, function definitions are also S-expressions:

(define (Fahrenheit-2-Celsius f)
(* (- f 32) 5/9)
)

 So, Scheme really only has one syntactic structure, the S-expression,
and that is used as a data-structure (to represent lists, trees, etc), as
function definitions, and as function calls.
Function Application
 In general, a function application is written like this:
(operator arg, arg2... arg.)

 The evaluation proceeds as follows:


1. Evaluate operator. The result should be a function F.
2. Evaluate
, , ... ,
to get
, , ... ,
3. Apply F to , , ... , .
Function Application - Examples
> (+ 4 5)
9
> (+ (+ 5 6) 3)
14
> 7
7
> (4 5 6)
eval: 4 is not a function
> #t
#t
Atoms - Numbers
Scheme has
 Fraction (5/9)
 Integers (5435)
 Complex numbers (5+2i)
 Inexact reals (#i3.14159265)
> (+ 5 4)
9
> (+ (* 5 4) 3)
23
> (+ 5/9 4/6)
1.2
> (5/9)
0.5
Atoms - Numbers
> (+ 5/9 8/18)
1
> 5+2i
5+2i
> (+ 5+2i 3-i)
8+1i
> ( * 236542164521634 3746573426573425643)
886222587860913289285513763860662
> pi
#i3.141592653589793
> e
#i2.718281828459045
> (* 2 pi)
#i6.283185307179586
Atoms - Numbers
 Scheme tries to do arithmetic exactly, as much as possible.

 Any computations that depend on an inexact value becomes inexact.

 Scheme has many built in mathematical functions:

> (sqrt 16)


4
> (sqrt 2)
#i1.4142135623730951
> (sin 45)
#i0.8509035245341184
> (sin (/ pi 2))
#i1.0
Atoms Strings
 A string is enclosed in double quotes.

> (display "hello")


hello
> "hello"
"hello"
> (string-length "hello")
5
> (string-append "hello" " " "world!")
"hello world! "
Atoms - Booleans
true is written #t.
false is written #f.

> #t
True
> #f
false
> (display #t)
#t
> (not #t)
false
Identifiers
 Unlike languages like C and Java, Scheme allows identifiers to contain
special characters, such as ! $ % & * + . / : < = > ? @ - Identifiers
should not begin with a character that can begin a number..
 This is a consequence of Scheme's simple syntax.
 You couldn't do this in Java because then there would be many ways to
interpret the expression x-5+Y.

Legal Illegal
h-e-l-l-o 3Dmax
give-me! -stance
CNN
Defining Variables
 define binds an expression to a global name:
(define name expression)

(define PI 3.14)
> PI
3.14

(define High-School-PI (/ 22 7))

> High-School-PI
3.142857
Defining Functions
 define binds an expression to a global name:
(define (name arg1 arg2...) expression)
 arg1 arg2... are formal function parameters.
(define (f) 'hello)

> (f)
hello

(define (square x) ( * x x))

> (square 3)
9
Defining Helper Functions
 A Scheme program consists of a large number of functions.

 A function typically is defined by calling other functions, so called


helper or auxiliary functions.

(define (square x) (* x x))

(define (cube x) ( * x (square x)))

> (cube 3)
27
Defining Helper Functions
 Sometimes you don't want an expression to be evaluated.
For example, you may want to think of (+ 4 5) as a list of three
elements +, 4, and 5, rather than as the computed value 9.
 (quote (+ 4 5)) prevents (+ 4 5) from being evaluated. You can also
write '(+ 4 5).
> (display (+ 4 5))
9
> (display (quote (+ 4 5)))
( + 4 5)
> (display ‘ (+ 4 5))
(+ 4 5)
Scheme so Far
 A function is defined by
(define (name arguments) expression)
A variable is defined by
(define name expression)
 Strings are inclosed in double quotes, like "this“. Common operations
on strings are
• (string-length string)
• (string-append list-of-strings)
 Numbers can be exact integers, inexact reals, fractions, and complex.
Integers can get arbitrarily large.
 Booleans are written #t and #f.
Comparison Functions
 Boolean functions (by convention) end with a ?.
 We can discriminate between different kinds of numbers:

> (complex? 3+4i)


#t
> (complex? 3)
#t
> (real? 3)
#t
> (real? -2.5 + 0.0i)
#t
> (rational? 6/10)
Comparison Functions…

#t
> (rational? 6/3)
#t
> (integer? 3+0i)
#t
> (integer? 3.0)
#t
> (integer? 8/4)
#t
Tests on Numbers
 Several of the comparison functions can take multiple arguments.
 ( < 4 5 6 7 9 234) returns true since the numbers are monotonically
increasing.
> (< 4 5)
true
> (< 4 5 6 7 9 234)
true
> (> 5 2 1 3)
false
> (= 1 1 1 1 1)
true
> (<= 1 2 2 2 3)
true
Tests on Numbers…
> (>= 5 5)
true
> (zero? 5)
false
> (positive? 5)
true
> (negative? 5)
false
> (odd? 5)
true
> (even? 5)
false
Conditionals — If
 If the test-expression evaluates to +f (False) return the value of the
then-expression, otherwise return the value of the else-expression:
(if test-expression
then-expression
else-expression
)
 Up to language level "Advanced Student" if-expressions must have two
parts.

 Set the language level to Standard (R5RS) to get the standard Scheme
behavior, where the else-expression is optional.
Conditionals — If…
> (define x 5)
> (if (= x 5) 2 4)
2
> (if (< x 3)
(display "hello")
(display "bye"))
bye
> (display
(if (< x 3) "hello" "bye"))
bye
If it's not False (#f), it's True (#t)
 Any value that is not false, is interpreted as true.
NOTE: In DrScheme this depends on which language level you set. Up to
"Advanced Student", the test-expression of an if must be either #t or #f.
 Set the language level to Standard (R5RS) to get the standard Scheme
behavior:
> (if 5 "hello" "bye")
"hello"
> (if #f "hello" "bye")
"bye"
> (if #f "hello")
> (if #t "hello")
"hello"
Boolean Operators
 and and or can take multiple arguments.
and returns true if none of its arguments evaluate to False.
 or returns true if any of its arguments evaluates to True.

> (and (< 3 5) (odd? 5) (inexact? (cos 32)))


#t
> (or (even? 5) (zero? (- 5 5)))
#t
> (not 5)
#f
> (not #t)
#f
Boolean Operators…
 In general, any value that is not #f is considered true.
and and or evaluate their arguments from left to right, and stop as soon
as they know the final result.
 The last value evaluated is the one returned.

> (and "hello")


"hello"
> (and "hello" "world")
"world"
> (or "hello" "world")
"hello"
Conditionals - cond
 cond is a generalization of if :
(cond
(cond-expression1 result-expression1)
(cond-expression2 result-expression2)

(else else-expression))

 Each cond-expression, is evaluated in turn, until one evaluates to not False.


> (cond
((< 2 3) 4)
((= 2 3) 5)
(else 6))
4
Conditionals – cond…
 To make this a bit more readable, we use square brackets around the
cond-clauses:
(cond
[cond-expr1 result-expr1]
[cond-expr2 result-expr2]

[else else-expression])
> (cond (#f 5] [#t 6])
6
> (cond
[( = 4 5 ) "hello"]
[( > 4 5 ) "goodbye"]
[( < 4 5 ) "see ya!"])
"see ya!"
Conditionals – case…
(define (classify n)
(case n
[(2 4 8 16 32) "small power of 2"] [
[(2 3 5 7 11) "small prime number"]
[else "some other number"]
)
)
> (classify 4)
"small power of 2“
> (classify 3)
"small prime number"
> (classify 2)
"small power of 2"
> (classify 32476)
"some other number "
Examples - !n
 Write the factorial function !n:

(define (! n)
(cond
[(zero? n) 1]
[else (* n (! (- n 1)))]
)
)
> (! 5)
120
Examples -
𝒏
 Write the 𝒓
function in Scheme:
n
r

 Use the factorial function from the last slide.

(define (choose n r)
( / (! n) (* ( ! r) (! (− n r))))
)
> (choose 5 2)
10
Examples – (sum m n)
 Write a function (sum m n) that returns the sum of the integers
between m and n, inclusive.
(define (sum m n)
(cond
((= m n) m)
[else (+ m (sum (+ 1 m) n))]
)
)
> (sum 1 2)
3
> (sum 1 4)
10

You might also like