Forms Writing your own procedures

Reviews
Shared by: Want You
Stats
views:
4
rating:
not rated
reviews:
0
posted:
2/26/2009
language:
English
pages:
0
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 The parameter x acts like a local variable within the function definition. 3 Scheme Forms 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  # (define a2 add2) (a2 3) » 5 a2  # 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 All parameters are collected into a list and assigned to a • Single symbol: (lambda 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) (if (< p 90) 'low-pressure) » safe » 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”] ) []’s work just like ()’s in Scheme. They are used here to make the code easier to read. Scheme Forms 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

Related docs
On your own …
Views: 5  |  Downloads: 0
POLICY AND PROCEDURES
Views: 0  |  Downloads: 0
Writing (Your Own) Letters of Recommendation
Views: 152  |  Downloads: 1
how to make your own business
Views: 104  |  Downloads: 11
Handbook,-Procedures,-and-Forms
Views: 0  |  Downloads: 0
writing a will
Views: 117  |  Downloads: 6
Writing Effective Policies and Procedures
Views: 5  |  Downloads: 3
work experience – own arrangements
Views: 0  |  Downloads: 0
first aid procedures
Views: 0  |  Downloads: 0
Own Business In Georgia
Views: 3  |  Downloads: 0
A Guide to Opening Your Own Center
Views: 2  |  Downloads: 0
Other docs by Want You
O Worship the King
Views: 181  |  Downloads: 1
Burnham v S C of CA
Views: 296  |  Downloads: 5
You Have Been Good
Views: 245  |  Downloads: 0
Furniture lease
Views: 338  |  Downloads: 12
Exercise for Your Bone Health
Views: 301  |  Downloads: 4
Ghen v Rich
Views: 332  |  Downloads: 2
Jesus Let Us Come to Know You
Views: 242  |  Downloads: 0
cd160
Views: 73  |  Downloads: 0
As the Deer Thirsts
Views: 292  |  Downloads: 1
MERGERS ACQUISITIONS Outline
Views: 2619  |  Downloads: 284
Ancient Words
Views: 433  |  Downloads: 5
dv125
Views: 140  |  Downloads: 0
dv150c
Views: 96  |  Downloads: 0
Option to grantor to repurchase
Views: 196  |  Downloads: 2