Scheme & Racket-Introduction: Session 4
Scheme & Racket-Introduction: Session 4
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.
A basic LISP implementation needs six functions: cons, car, cdr, equal,
atom, cond.
>(+ 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.)
> #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
> 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
> (square 3)
9
Defining Helper Functions
A Scheme program consists of a large number of functions.
> (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:
#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.
(define (! n)
(cond
[(zero? n) 1]
[else (* n (! (- n 1)))]
)
)
> (! 5)
120
Examples -
𝒏
Write the 𝒓
function in Scheme:
n
r
(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