Forms Writing your own procedures - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Forms Writing your own procedures

Description:

(area 3 6) ' 18 (area 3) procedure area: expects 2 arguments, given 1: 3 ... All parameters are collected into a list and assigned to a. Dotted pair: (lambda (a b c. ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 12
Provided by: Michael1759
Category:
Tags: all | area | codes | forms | list | of | procedures | writing

less

Transcript and Presenter's Notes

Title: Forms Writing your own procedures


1
FormsWriting your own procedures
CS 480/680 Comparative Languages
2
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

3
Defining 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.
4
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 ? ltprocedureadd2gt (define a2
add2) (a2 3) 5 a2 ? ltprocedureadd2gt
5
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

6
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.
7
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

8
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
9
Conditionals
(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.
10
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-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.
11
Exercises
  • 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)
Write a Comment
User Comments (0)
About PowerShow.com