Racket Last Structs, Lambda, Module

Slides:



Advertisements
Similar presentations
Multiplying Polynomials
Advertisements

CSC 270 Nov. 22, 2005 Last Day of Scheme Dr. Stephen Bloch
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Racket Introduction CSC270 Pepper major portions credited to
2.8 Solving Linear Inequalities
Translate, Rotate, Matrix Pages Function Function Definition Calling a function Parameters Return type and return statement.
The extraordinary shape Circle. Why circle is extraordinary? Circle is much different shape from the others because:  It has no sides  Has infinity.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #12: Syntax and Semantics Prof. Adam M. Wittenstein
Chapter 2 - Algorithms and Design
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein
Procedures and Modular Programming Part # 2. Interface Block ► Functions do not have to be internal to main program ► They can be stand-alone ► In this.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Simplifying Radical Expressions Simplifying Radicals Radicals with variables.
JavaScript 101 Lesson 3: Variables and Arithmetic.
4.8 Polynomial Word Problems. a) Define the variable, b) Write the equation, and c) Solve the problem. 1) The sum of a number and its square is 42. Find.
Perimeter is the distance around an object. If you started at one point and went all the way around until you got to where you started, the perimeter.
Adding and Subtracting Decimals Intro to Algebra.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
12.7 Similar. Today we will… Return to the idea Of similar objects.
Module 3 Lesson 1 Investigating Perimeter and Area Formulas.
Multiplying Polynomials
SOLVING ALGEBRAIC EXPRESSIONS
CIRCLES Topic 7.3.
Haskell Chapter 7.
Introduction to Algorithms
Area By: Angie Eyring.
6.7 Circumference and Arc Length.
Chapter 2 Section 8.
Racket CSC270 Pepper major portions credited to
How to calculate the area of a circle.
Match the picture to the name.
Circles.
Perimeter and Area Word Problems (Using One Variable) Taught by the Bestest of all the besterest who are not bestless, Mr. Peter Richard.
Control structures Chapter 3.
Warm Up.
UNIT 8: 2-D MEASUREMENTS PERIMETER AREA SQUARE RECTANGLE PARALLELOGRAM
Pre-processor Directives
Concept. Skills Check 1-5 (I will scan it in at the end of class) Going over the quiz.
Calculate Areas of Rectangles, Triangles, Parallelograms and Circles
Chapter 2 Section 8.
Racket Introduction CSC270 Pepper
Activating Prior Knowledge – Notes
Using Racket to Display your puzzle
Circumference Definition: The distance around a circle. It’s a special word for perimeter. Picture:
Objective: Be able to add and subtract directed numbers.
Pass by Reference, const, readonly, struct
What Shapes Can You See in this Picture?
CIRCLES Topic 10.2.
The Metacircular Evaluator
Faculty of Computer Science & Information System
Goals of my lectures A way of adding types DSL-making “tools”
Introduction to Value-Returning Functions: Generating Random Numbers
What is an equation? An equation says that two things are equal. It will have an equals sign "=" like this: x + 2 = 6 This equation says: what is on the.
Multiplying Polynomials
Date Functions Part Two and Arithmetic Combinations of Functions
Circumference and Area: Circles
Circumference and Arc Length. Circumference and Arc Length.
Just Enough Java 17-May-19.
Writing Function Rules
Lambda Expressions Cases
Objective: Be able to add and subtract directed numbers.
CIRCLES Topic 7.3.
CIRCLES Topic 7.3.
Area and Volume How to … Calculate the area of a square or rectangle
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
CIRCLES Topic 7.3.
Presentation transcript:

Racket Last Structs, Lambda, Module CSC270 Pepper major portions credited to http://learnxinyminutes.com/docs/racket/

Structs ; Define structure: (struct student (name gpa age)) ; structure type definition: (define s1 (student "amy jones" 3.0 20)) ; use of one part of struct variable: (student-name s1) ; check type : (student? s1 )

Shape Structures (struct circle (radius)) (define c1 (circle 10)) (struct rect (length width)) (define r1 (rect 10 20)) (define r2 (rect 11 12))

Area Functions (define (area a-shape) (cond [(rect? a-shape) (* (rect-length a-shape) (rect-width a-shape))] [ (circle? a-shape) (* (expt (circle-radius a-shape) 2)pi)])) (area c1) (area r1) (area (rect 10 30)

Functions Passing in Functions First class functions (define (square num) (* num num)) (define (pow3 num) (* num num num)) (define (calc numberfunc) (numberfunc 10)) (calc square); (calc pow3); Plus Remember map: (define (add3 x) (+ x 3)) (map add3 '(1 2 3))

Lambda Functions (lambda () "Hello") ; surround with parentheses to call (( lambda () "Hello")) ; function with a parm (lambda (num) (+ num 3)) ((lambda (num) (+ num 3)) 5) All functions have an implied lambda

Define and Lambda ; assign a lambda function to a symbol (define Lhello-world ( lambda() "Hello")) ; same as (define (Lhello-world) "hello") (Lhello-world) (define Ladd3 ( lambda (num) (+ num 3))) ; second same as (define (Ladd3 num) (+ num 3)) (Ladd3 5) ; see how all functions have an implied lambda?

Functions Passing in Functions (define (triangleperm a b c) (+ a b c)) (define (equiTriangle a) (triangleperm a a a)); (calc equiTriangle); Or instead of defining equiTriangle, create an anonymous function: (calc (lambda (x) (triangleperm x x x)))

Map Lambda functions Define a function Use it in map (define (add3 x) (+ x 3)) (map add3 '(1 2 3)) Define that same function anonymously (map (lambda (i) (+ i 3)) '(1 2 3))

Adding New Commands EASY (define-syntax-rule (swap! x y) (let ([temp x]) (set! x y) (set! y temp))) (define x 3) (define y 4) (swap! x y) x y

Modules create it with (module moduleName itsBase) example (module cake racket/base) list functions the importer can use with (provide functionName) example (provide bake-cake) no indication of parameters needed To import a module : (require ModuleName) example (require picturing-programs)

Important Concepts Define data in parts : structure Functions can take in functions as parms On the fly functions give flexibility