Presentation is loading. Please wait.

Presentation is loading. Please wait.

Forms Writing your own procedures CS 480/680 – Comparative Languages.

Similar presentations


Presentation on theme: "Forms Writing your own procedures CS 480/680 – Comparative Languages."— Presentation transcript:

1 Forms Writing your own procedures CS 480/680 – Comparative Languages

2 Scheme Forms2 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 Scheme Forms3 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 Scheme Forms4 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  #

5 Scheme Forms5 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 Scheme Forms6 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 Scheme Forms7 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 Scheme Forms8 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 Scheme Forms9 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 (> p 70) 'safe 'unsafe) » safe (if (< p 90) 'low-pressure) » low-pressure Each branch has an implicit begin.

10 Scheme Forms10 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.

11 Scheme Forms11 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)


Download ppt "Forms Writing your own procedures CS 480/680 – Comparative Languages."

Similar presentations


Ads by Google