0% found this document useful (0 votes)
46 views3 pages

First Module

This document introduces functions in Scheme programming language. It explains that a function consists of a name, list of parameters, and a single body expression. It defines some example functions like f(x) = x^2 and g(x, y) = x + y. It also demonstrates how to apply functions like (g (g 1 3) (f 2)) and step through evaluating the expression. Finally, it notes that functions can be defined in many ways and the parameter names are meaningless; constants can be defined and used in any expression.

Uploaded by

Jackie Baek
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views3 pages

First Module

This document introduces functions in Scheme programming language. It explains that a function consists of a name, list of parameters, and a single body expression. It defines some example functions like f(x) = x^2 and g(x, y) = x + y. It also demonstrates how to apply functions like (g (g 1 3) (f 2)) and step through evaluating the expression. Finally, it notes that functions can be defined in many ways and the parameter names are meaningless; constants can be defined and used in any expression.

Uploaded by

Jackie Baek
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction to CS135 f(x,y) = x + y Name: f Parameters: eg. x, y Algebraic expressions using the parameters: eg. x + y Application of a function: eg.

f(1, 3) Evaluate innermost first, left to right.

Scheme g(1, 3) becomes (g 1 3) g( g(1,3),f(2)becomes (g (g 1 3) (f 2)) 3 2 + 4/5 becomes (+ ( - 3 2) (/ 4 5 )) (6-4)(3+2) becomes (* (- 6 4) (+ 3 2)) *extra brackets are harmful in Scheme. Brackets imply a function. Do not write (( + 2 2)) (10 + 4) * 3 / (15-5) becomes (/ ( * ( + 10 4) 3) (- 15 5))

Dr. Racket Definitions and interactions window Interactions (REPL: read, evaluate, print, loop): interpreted, not complied Ctrl + up arrow: repeat last line you typed 2^6 = 64 2^10 = 1024 Can store exact rational numbers (eg. 1/3), but not irrational (eg. sqrt 2) Integers in Scheme are unbounded Expressions whose values are not rational are flagged as being inexact (#) (5 * 14) infix notation (operation is in between the operands), Scheme is prefix Syntax errors: structure is wrong. Eg. (* + 3 4 2) (/ 25 0) not a syntax error, mathematical error

Defining functions

A function consists of: name, list of parameters, a single body expression. f(x) = x^2, g(x, y) = x + y become (dene (f x) ( x x)) (dene (g x y) (+ x y) define is a special form (not a function because all of its arguments are not evaluated). It binds a name to an expression (which uses the parameters that follow the name) Each parameter name has meaning only within the body of its function. The two uses of x are independent (define (h y z) (f (g y,z))) h(y, z) = f( g(y,z))

(g (g 1 3) (f 2)) (g ( + 1 3) (f 2)) substitute (g 4 ( f 2)) ( g 4 (* 2 2)) (g 4 4 ) (+ 4 4) 8 simplify substitute simplify substitute simplify

There are many ways to write the same function. The names of the parameters are meaningless. Constants (define k 3) (define p (* k k)) p=9 (define x 3) (define (f x y) (- x y)) (+ x x ) = 6 (f 5 x) = (f 5 3) = (- 5 3) = 2

(f 3 2) = (- 3 2) = 1 Can give meaningful names to useful values Constants can be used in any expression

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy