Scheme Forms - The Bioinformatics Research Group
Download
Report
Transcript Scheme Forms - The Bioinformatics Research Group
CS 480/680 – Comparative Languages
Forms
Writing your own procedures
Calling Functions
Recall that a scheme form is a list:
(function arg1 arg2 arg3 …)
(+ 2 5 3)
(number? 3) etc.
You can write your own function using the
special form lambda
• (lambda (x) (+ x 2)) is a function, just
like +
• ((lambda (x) (+ x 2)) 5) » 7
Scheme Forms
2
Defining Functions
You can define a symbol to be a function for reuse later.
• This is one difference between a symbol and an
ordinary variable in other languages
• (define add2
(lambda (x) (+ x 2)))
(add2 3) » 5
Scheme Forms
The parameter x acts like a
local variable within the
function definition.
3
First Class Variables
In Scheme, procedures are first class variables
• You can assign to them, pass them as parameters,
and otherwise use them just like any other variable
(add2 3) » 5
add2 #<procedure:add2>
(define a2 add2)
(a2 3) » 5
a2 #<procedure:add2>
Scheme Forms
4
Parameters
Procedures can have multiple parameters:
(define area
(lambda (length breadth)
(* length breadth)))
(area 3 6) » 18
(area 3) procedure area: expects
2 arguments, given 1: 3
There are several ways to create procedures that
expect a variable number of arguments…
Scheme Forms
5
Variable numbers of arguments
Three ways to specify parameters:
• List of parameters: (lambda (a b) …)
Function expects exactly two arguments
• Single symbol: (lambda a …)
All parameters are collected into a list and assigned to a
• Dotted pair: (lambda (a b c . d) …)
Must have at least three arguments
First three are assigned to a, b, and c
The remaining arguments are collected as a list and
assigned to d
See args.scheme.
Scheme Forms
6
Sequencing
Generally, a function definition calls a single
function and returns a single value
• Another way to say this is that the definition part of
lambda accepts a single form
• The arguments to the function might be other
functions
Begin groups together a set of subforms to be
executed in sequence. The return value is that
of the last subform.
• Lambda actually includes an implicit begin
Scheme Forms
7
Parameters and values
In Scheme, parameters are passed by value
(define add2 (lambda (someval)
(begin
(set! someval (+ 2 someval))
(display someval) (newline))
)
)
(define a 5)
(add2 a) 7
a 5
Scheme Forms
8
Conditionals
(if test-expression
then-branch
else-branch)
Each branch has an implicit
begin.
If test-expression evaluates to true (ie, any value other than #f), the “then”
branch is evaluated. If not, the “else” branch is evaluated.
The “else” branch is optional.
(define p 80)
(if (> p 70)
'safe
'unsafe)
» safe
(if (< p 90)
'low-pressure)
» low-pressure
Scheme Forms
9
Flexible conditionals
The cond form can have as many tests as
needed
• The first one that evaluates to something other than
#f is evaluated
(cond [(number? term)
(number->string term)]
[(symbol? term)
(symbol->string term)]
[(null? term)
(“empty”)]
[else “unknown”]
)
Scheme Forms
[]’s work just like ()’s in
Scheme. They are used
here to make the code
easier to read.
10
Exercises
Write a function that expects three numerical
arguments and returns the average
Write a function that expects a list of arguments
(any length > 3) and returns the third item in the
list
Write a function that expects at least three
arguments. The function should print the first
two arguments to stdout, and return the third
argument (all the rest should be thrown away)
Scheme Forms
11