Title: Forms Writing your own procedures
1FormsWriting your own procedures
CS 480/680 Comparative Languages
2Calling 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
3Defining Functions
- You can define a symbol to be a function for
re-use later. - This is one difference between a symbol and an
ordinary variable in other languages - (define add2
- (lambda (x) ( x 2)))
- (add2 3) 5
The parameter x acts like a local variable within
the function definition.
4First 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 ? ltprocedureadd2gt (define a2
add2) (a2 3) 5 a2 ? ltprocedureadd2gt
5Parameters
- 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
6Variable 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.
7Sequencing
- 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
8Parameters 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
9Conditionals
(if test-expression then-branch
else-branch) 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 (gt p 70)
'safe 'unsafe) safe (if (lt p 90)
'low-pressure) low-pressure
Each branch has an implicit begin.
10Flexible 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-gtstring
term) (symbol? term) (symbol-gtstring
term) (null? term) (empty) else
unknown )
s work just like ()s in Scheme. They are
used here to make the code easier to read.
11Exercises
- Write a function that expects three numerical
arguments and returns the average - Write a function that expects a list of arguments
(any length gt 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)