Procedural vs functional programming

Size: px
Start display at page:

Download "Procedural vs functional programming"

Transcription

1 2 procvsfuncprog2.nb Procedural vs functional programming Basic examples with Mathematica Hugo Touchette School of Mathematical Sciences Queen Mary, University of London Programming Seminar ate: 27 October 2010 Ideas Ë Act on lists or structures Ë Avoid loops Ë Use pure functions Ë Think global/operators/composition (vs local/sequential) Ë on't care too much about space/memory Basic example Problem: Sum integers from 1 to ü irect answer In[15]:= Sum@i, 8i, < Out[15]=

2 procvsfuncprog2.nb 3 4 procvsfuncprog2.nb In[16]:= s = 0; s = s + i, 8i, <; In[18]:= s Out[18]= I In[19]:= integerlist = Table@i, 8i, <; In[20]:= Fold@Ò1 + Ò2 &, 0, integerlist Out[20]= II In[21]:= Plus üü integerlist Out[21]= ü Time comparison test1@n_ := Module@8i<, Sum@i, 8i, n< test2@n_ := Module@8s, i<, s = 0; s = s + i, 8i, n<; s test3@n_ := Plus üü Table@i, 8i, n< iscreteplot@8timing@test1@n@@1, Timing@test2@n@@1, Timing@test3@n@@1<, 8n, 1000, , 1000<, Filling Ø None, Joined Ø True, Frame Ø True, PlotStyle Ø Thick Everything is an expression In[22]:= êê Hold êê FullForm Out[22]//FullForm= Hold@Plus@1, 2, 3, 4 In[23]:= 81, 2, 3, 4< êê FullForm Out[23]//FullForm= List@1, 2, 3, 4 In[24]:= Plus üü 81, 2, 3, 4< Out[24]= 10 In[25]:= Apply@Plus, 81, 2, 3, 4< Out[25]= 10 In[26]:= Times üü 81, 2, 3, 4< Out[26]= 24 In[27]:= Apply@Times, 81, 2, 3, 4< Out[27]= 24 In[28]:= f@x ê. f Ø g Out[28]= g@x In[29]:= MyMean@l_ := Plus üü l ê Length@l In[30]:= MyMean@81, 2, 3, 4< Out[30]= 5 2 Pure functions Ë Pure functions (Mathematica) Ë Anonymous functions Ë Lambda functions (Lambda calculus, Lisp, Python) In[31]:= Ò^2 &@c Out[31]= c 2 In[32]:= Function@u, u^2@c Out[32]= c 2 In[33]:= Ò^2 & êü 8a, b, c< Out[33]= 9a 2, b 2, c 2 = In[34]:= Map@Ò^2 &, 8a, b, c< Out[34]= 9a 2, b 2, c 2 = In[35]:= f êü Ha + b + cl Out[35]= f@a + f@b + f@c In[36]:= HÒ@@1 + Ò@@2 &L êü 88a, b<, 8c, d<< Out[36]= 8a + b, c + d< In[37]:= Nest@Ò^c &, x, 3 Out[37]= IHx c L c M c

3 procvsfuncprog2.nb 5 6 procvsfuncprog2.nb Example: Newton-Raphson iteration In[38]:= NewtonZero@f_, x0_ := FixedPoint@Ò - f@ò ê f'@ò &, x0 In[39]:= NewtonZero@BesselJ@2, Ò &, 5.0 Out[39]= ü Another example In[40]:= EmitSound@Sound@SoundNote@Ò,.1, "Percussion" & êü Range@-32, 35 Composition and pipeline ü Prefix In[41]:= füx Out[41]= f@x In[42]:= güfüx Out[42]= g@f@x ü Postfix (pipeline) In[43]:= x êê f Out[43]= f@x In[49]:= Exp êü data; êê Timing Out[49]= , Null< In[50]:= Exp@data; êê Timing Out[50]= , Null< In[51]:= ans = data; k = Length@data; While@k > 0, ans@@k = Exp@data@@k; k--; ; êê Timing Out[53]= , Null< Application: Mandelbrot set Mandelbrotset0@x_, y_ := ModuleA8z, t = 0, c = x + I y<, z = c; WhileAHAbs@z < 2.0L && Ht < 100L, z = z 2 + c; t++e; t E; ensityplot@mandelbrotset0@x, y, 8x, -1.75, 0.75<, 8y, -1.25, 1.25<, PlotPoints Ø 100, Mesh Ø False, ColorFunction Ø Hue In[44]:= x êê f êê g Out[44]= g@f@x ü Composition In[45]:= Nest@f, x, 4 Out[45]= f@f@f@f@x In[46]:= NestList@f, x, 4 Out[46]= 8x, f@x, f@f@x, f@f@f@x, f@f@f@f@x< In[47]:= Nest@1 + 1 ê Ò &, x, 3 1 Out[47]= x Listable functions In[48]:= data = TableARandomReal@, =E; Mandelbrotset2@c_, nmax_ := Length@FixedPointList@Ò^2 + c &, c, nmax, SameTest Ø HAbs@Ò2 > 2.0 &L

4 procvsfuncprog2.nb 7 8 procvsfuncprog2.nb ensityplot@mandelbrotset2@x + y I, 100, 8x, -1.75, 0.75<, 8y, -1.25, 1.25<, PlotPoints Ø 100, Mesh Ø False, ColorFunction Ø Hue Prime counting function pproc@n_ := Module@8cnt, i<, cnt = 0; If@PrimeQ@i, cnt++, 8i, 2, n<; cnt ; ListPlot@Table@8i, pproc@i<, 8i, 1, 100< ü Time comparison timingtable1 = Table@ Timing@Table@Mandelbrotset0@x, y, 8x, -1.75, 0.75, 1 ê n<, 8y, -1.25, 1.25, 1 ê n<@@1, 8n, 10, 100, 10<; timingtable2 = Table@Timing@Table@Mandelbrotset2@x + I y, 100, 8x, -1.75, 0.75, 1 ê n<, 8y, -1.25, 1.25, 1 ê n<@@1, 8n, 10, 100, 10<; ListPlot@8timingtable1, timingtable2<, Joined Ø True, Frame Ø True, PlotStyle Ø Thick List all the positive integers up to n 2. Extract the primes 3. Count what's extracted pfunc@n_ := Length@Select@Table@i, 8i, n<, PrimeQ ü Time comparison iscreteplot@8timing@pproc@n@@1, Timing@pfunc@n@@1<, 8n, 1000, , 1000<, Filling Ø None, Joined Ø True, Frame Ø True, PlotStyle Ø Thick

5 procvsfuncprog2.nb 9 10 procvsfuncprog2.nb Perfect numbers A number n is perfect if it is equal to the sum of its proper divisors (all divisors except n) or, equivalently, if the sum of all its divisors (including n) is equal to 2 n. For example, 6 is a perfect number since its proper factors, 1, 2, 3 sum to = 6. In[54]:= PerfectNumberQ@n_ := 2 n === Plus üü ivisors@n In[56]:= PerfectNumberQ@12 Out[56]= False With@8nmax = <, l = 8<; If@PerfectNumberQ@i, AppendTo@l, i, 8i, nmax<; l êê Timing , 86, 28, 496, 8128<< Time comparison With@8n = , dn = 1000<, iscreteplot@8timing@primesumproc@i@@1, Timing@primesumfunc@i@@1<, 8i, 1, n, dn<, Filling Ø None, Joined Ø True, Frame Ø True, PlotStyle Ø Thick With@8nmax = <, Select@Table@i, 8i, 1, nmax<, PerfectNumberQ êê Timing , 86, 28, 496, 8128<< Prime sums S n = 1 n n p i i=1 m Generate the list 8n, S n< n=1 primesumproc@n_ := Module@8i, ps, res<, ps = 0; res = 8<; ps = ps + Prime@i; AppendTo@res, 8i, ps ê i<, 8i, 1, n<; res primesumfunc@n_ := NestList@ 8Ò@@1 + 1, HÒ@@2 Ò@@1 + Prime@Ò@@1 + 1L ê HÒ@@1 + 1L< &, 81, 2<, n - 1

6 procvsfuncprog2.nb procvsfuncprog2.nb 1, 3, 5, 5 question Interview question: What is the combination of the numbers 1, 3, 5 and 5 with any of the arithmetic operations (+, -, *, /) that yields 16? ü Include In[1]:= Listelete@list_, elem_ := elete@list, Position@list, elem@@1, 1 In[2]:= ListOperation@l_ := Module@8res, i<, res = l@@1; res = res~l@@i~l@@i + 1, 8i, 2, Length@l, 2<; res In[3]:= numset = 81, 3, 5, 5<; In[4]:= opset = 8Plus, Subtract, Times, ivide<; In[57]:= Catch@ numset1 = Listelete@numset, n1; numset2 = Listelete@numset1, n2; opset1 = Listelete@opset, op1; res1 = n1~op1~n2; numset3 = Listelete@numset2, n3; opset2 = Listelete@opset1, op2; res2 = res1~op2~n3; n4 = numset3@@1; res3 = res2~op3~n4; If@res3 ã 16, Throw@8n1, op1, n2, op2, n3, op3, n4<, 8op3, opset2<, 8op2, opset1<, 8n3, numset2<, 8op1, opset<, 8n2, numset1<, 8n1, numset< Out[57]= 81, ivide, 5, Plus, 3, Times, 5< In[58]:= listperm = Permutations@numset Out[58]= 881, 3, 5, 5<, 81, 5, 3, 5<, 81, 5, 5, 3<, 83, 1, 5, 5<, 83, 5, 1, 5<, 83, 5, 5, 1<, 85, 1, 3, 5<, 85, 1, 5, 3<, 85, 3, 1, 5<, 85, 3, 5, 1<, 85, 5, 1, 3<, 85, 5, 3, 1<< In[59]:= listop = Flatten@Table@8i, j, k<, 8i, opset<, 8j, opset<, 8k, opset<, 2 Out[59]= 88Plus, Plus, Plus<, 8Plus, Plus, Subtract<, 8Plus, Plus, Times<, 8Plus, Plus, ivide<, 8Plus, Subtract, Plus<, 8Plus, Subtract, Subtract<, 8Plus, Subtract, Times<, 8Plus, Subtract, ivide<, 8Plus, Times, Plus<, 8Plus, Times, Subtract<, 8Plus, Times, Times<, 8Plus, Times, ivide<, 8Plus, ivide, Plus<, 8Plus, ivide, Subtract<, 8Plus, ivide, Times<, 8Plus, ivide, ivide<, 8Subtract, Plus, Plus<, 8Subtract, Plus, Subtract<, 8Subtract, Plus, Times<, 8Subtract, Plus, ivide<, 8Subtract, Subtract, Plus<, 8Subtract, Subtract, Subtract<, 8Subtract, Subtract, Times<, 8Subtract, Subtract, ivide<, 8Subtract, Times, Plus<, 8Subtract, Times, Subtract<, 8Subtract, Times, Times<, 8Subtract, Times, ivide<, 8Subtract, ivide, Plus<, 8Subtract, ivide, Subtract<, 8Subtract, ivide, Times<, 8Subtract, ivide, ivide<, 8Times, Plus, Plus<, 8Times, Plus, Subtract<, 8Times, Plus, Times<, 8Times, Plus, ivide<, 8Times, Subtract, Plus<, 8Times, Subtract, Subtract<, 8Times, Subtract, Times<, 8Times, Subtract, ivide<, 8Times, Times, Plus<, 8Times, Times, Subtract<, 8Times, Times, Times<, 8Times, Times, ivide<, 8Times, ivide, Plus<, 8Times, ivide, Subtract<, 8Times, ivide, Times<, 8Times, ivide, ivide<, 8ivide, Plus, Plus<, 8ivide, Plus, Subtract<, 8ivide, Plus, Times<, 8ivide, Plus, ivide<, 8ivide, Subtract, Plus<, 8ivide, Subtract, Subtract<, 8ivide, Subtract, Times<, 8ivide, Subtract, ivide<, 8ivide, Times, Plus<, 8ivide, Times, Subtract<, 8ivide, Times, Times<, 8ivide, Times, ivide<, 8ivide, ivide, Plus<, 8ivide, ivide, Subtract<, 8ivide, ivide, Times<, 8ivide, ivide, ivide<< In[60]:= Riffle@listperm@@1, listop@@1 Out[60]= 81, Plus, 3, Plus, 5, Plus, 5< In[61]:= allcaseslist = Flatten@ Table@ Riffle@listperm@@i, listop@@j, 8i, Length@listperm<, 8j, Length@listop<, 1 ; In[62]:= resultlist = ListOperation êü allcaseslist; In[66]:= ListPlot@resultlist, Joined Ø True, PlotRange Ø All Out[66]= In[64]:= winningpos = Position@ListOperation êü allcaseslist, 16 Out[64]= 88115<< In[65]:= Extract@allcaseslist, winningpos Out[65]= 881, ivide, 5, Plus, 3, Times, 5<< Conclusions Reform your sequential brain!

7 procvsfuncprog2.nb 13 Think lists Ë Think listable functions Ë Think global operations Ë Avoid loops Ë Avoid counters Ë Think functional programming

Lecture 07. (with solutions) Summer 2001 Victor Adamchik. 2 as an infinite product of nested square roots. M #'((((((((((((((((((((((((((((( 2 ] # N

Lecture 07. (with solutions) Summer 2001 Victor Adamchik. 2 as an infinite product of nested square roots. M #'((((((((((((((((((((((((((((( 2 ] # N Lecture 07 (with solutions) Summer 00 Victor Adamchik Off#General::spell'; Off#General::spell'; $Line 0; Exercise Vieta's formula, developed in 593, expresses cccc as an infinite product of nested square

More information

Automating the Tedious Stuff (Functional programming and other Mathematica magic)

Automating the Tedious Stuff (Functional programming and other Mathematica magic) /22 Automating the Tedious Stuff (Functional programming and other Mathematica magic) Connor Glosser Michigan State University Departments of Physics & Electrical/Computer Engineering π, 2014 /22 Table

More information

Class 4 : Programming in Mathematica Part #2

Class 4 : Programming in Mathematica Part #2 Class 4 : Programming in Mathematica Part #2 Note : Almost all of the examples below are taken from the Mathematica documentation center. In this class we shall see how to handle, expressions, lists, functions,

More information

Newton s Method. Example : Find the root of f (x) =

Newton s Method. Example : Find the root of f (x) = Newton s Method Example : Find the root of f (x) = If we draw the graph of f (x), we can see that the root of f (x) = 0 is the x - coordinate of the point where the curve intersects with the x - axis.

More information

REPRESENTATION OF CURVES IN PARAMETRIC FORM

REPRESENTATION OF CURVES IN PARAMETRIC FORM - Representation of curves in parametric form 1 REPRESENTATION OF CURVES IN PARAMETRIC FORM.1. Parametrization of curves in the plane Given a curve in parametric form, its graphical representation in a

More information

GRAPHICAL REPRESENTATION OF SURFACES

GRAPHICAL REPRESENTATION OF SURFACES 9- Graphical representation of surfaces 1 9 GRAPHICAL REPRESENTATION OF SURFACES 9.1. Figures defined in Mathematica ô Graphics3D[ ] ø Spheres Sphere of centre 1, 1, 1 and radius 2 Clear "Global` " Graphics3D

More information

Teaching Complex Analysis as a Lab- Type ( flipped ) Course with a Focus on Geometric Interpretations using Mathematica

Teaching Complex Analysis as a Lab- Type ( flipped ) Course with a Focus on Geometric Interpretations using Mathematica Teaching Complex Analysis as a Lab- Type ( flipped ) Course with a Focus on Geometric Interpretations using Mathematica Bill Kinney, Bethel University, St. Paul, MN 2 KinneyComplexAnalysisLabCourse.nb

More information

Boundary scanning and complex dynamics

Boundary scanning and complex dynamics BoundaryScanPP.nb 1 Boundary scanning and complex dynamics A preprint version of a Mathematical graphics column from Mathematica in Education and Research. Mark McClure mcmcclur@unca.edu Department of

More information

Mandelbrot in Mathematica

Mandelbrot in Mathematica ^01 Mandelbrot in Mathematica.nb 1 Mandelbrot in Mathematica Guide to plotting the most famous instance of the Mandelbrot Set in Mathematica Novak Petrovic npetrovic@gmail.com 0 Version history 20 August

More information

LECTURE 16. Functional Programming

LECTURE 16. Functional Programming LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

More information

Chapter 1. Fundamentals of Higher Order Programming

Chapter 1. Fundamentals of Higher Order Programming Chapter 1 Fundamentals of Higher Order Programming 1 The Elements of Programming Any powerful language features: so does Scheme primitive data procedures combinations abstraction We will see that Scheme

More information

Organization of Programming Languages CS3200/5200N. Lecture 11

Organization of Programming Languages CS3200/5200N. Lecture 11 Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages

More information

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is

More information

Introduction to Functional Programming

Introduction to Functional Programming Introduction to Functional Programming Xiao Jia xjia@cs.sjtu.edu.cn Summer 2013 Scheme Appeared in 1975 Designed by Guy L. Steele Gerald Jay Sussman Influenced by Lisp, ALGOL Influenced Common Lisp, Haskell,

More information

Monte Carlo Method for Definite Integration

Monte Carlo Method for Definite Integration Monte Carlo Method for Definite Integration http://www.mathcs.emory.edu/ccs/ccs215/monte/node13.htm Numerical integration techniques, such as the Trapezoidal rule and Simpson's rule, are commonly utilized

More information

Section 1.6. Inverse Functions

Section 1.6. Inverse Functions Section 1.6 Inverse Functions Important Vocabulary Inverse function: Let f and g be two functions. If f(g(x)) = x in the domain of g and g(f(x) = x for every x in the domain of f, then g is the inverse

More information

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

More information

Functions, Conditionals & Predicates

Functions, Conditionals & Predicates Functions, Conditionals & Predicates York University Department of Computer Science and Engineering 1 Overview Functions as lambda terms Defining functions Variables (bound vs. free, local vs. global)

More information

Assignment 1. Prolog to Problem 1. Two cylinders. ü Visualization. Problems by Branko Curgus

Assignment 1. Prolog to Problem 1. Two cylinders. ü Visualization. Problems by Branko Curgus Assignment In[]:= Problems by Branko Curgus SetOptions $FrontEndSession, Magnification Prolog to Problem. Two cylinders In[]:= This is a tribute to a problem that I was assigned as an undergraduate student

More information

AT Arithmetic. Integer addition

AT Arithmetic. Integer addition AT Arithmetic Most concern has gone into creating fast implementation of (especially) FP Arith. Under the AT (area-time) rule, area is (almost) as important. So it s important to know the latency, bandwidth

More information

Constants and Variables

Constants and Variables DATA STORAGE Constants and Variables In many introductory courses you will come across characteristics or elements such as rates, outputs, income, etc., measured by numerical values. Some of these will

More information

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable.

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable. Scope of Variables The variables used in function m-files are known as local variables. Any variable defined within the function exists only for the function to use. The only way a function can communicate

More information

Crash Course in Monads. Vlad Patryshev

Crash Course in Monads. Vlad Patryshev Crash Course in Monads Vlad Patryshev Introduction Monads in programming seem to be the most mysterious notion of the century. I find two reasons for this: lack of familiarity with category theory; many

More information

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement Outline Expression Evaluation and Control Flow In Text: Chapter 6 Notation Operator evaluation order Operand evaluation order Overloaded operators Type conversions Short-circuit evaluation of conditions

More information

An introduction to Scheme

An introduction to Scheme An introduction to Scheme Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize

More information

Chapter 4 Introduction to Control Statements

Chapter 4 Introduction to Control Statements Introduction to Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives 2 How do you use the increment and decrement operators? What are the standard math methods?

More information

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Programming Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

More information

Examples of Fourier series

Examples of Fourier series Examples of Fourier series Preliminaries In[]:= Below is the definition of a periodic extension of a function defined on L, L. This definition takes a function as a variable. The function has to be inputted

More information

Test # 1 Review. to the line x y 5. y 64x x 3. y ( x 5) 4 x 2. y x2 2 x. Á 3, 4 ˆ 2x 5y 9. x y 2 3 y x 1. Á 6,4ˆ and is perpendicular. x 9. g(t) t 10.

Test # 1 Review. to the line x y 5. y 64x x 3. y ( x 5) 4 x 2. y x2 2 x. Á 3, 4 ˆ 2x 5y 9. x y 2 3 y x 1. Á 6,4ˆ and is perpendicular. x 9. g(t) t 10. Name: Class: Date: ID: A Test # 1 Review Short Answer 1. Find all intercepts: y 64x x 3 2. Find all intercepts: y ( x 5) 4 x 2 3. Test for symmetry with respect to each axis and to the origin. y x2 2 x

More information

1 Basic Plotting. Radii Speeds Offsets 1, 1, 1 2, 5, 19 0, 0, 0 1, 0.8, 0.4, 0.2, 0.4, 0.2 1, 10, 17, 26, 28, 37 0, Π, Π, 0, 0, Π

1 Basic Plotting. Radii Speeds Offsets 1, 1, 1 2, 5, 19 0, 0, 0 1, 0.8, 0.4, 0.2, 0.4, 0.2 1, 10, 17, 26, 28, 37 0, Π, Π, 0, 0, Π 1 Basic Plotting Placing wheels on wheels on wheels and giving them different rates of spin leads to some interesting parametric plots. The images show four examples. They arise from the values below,

More information

n Group of statements that are executed repeatedly while some condition remains true

n Group of statements that are executed repeatedly while some condition remains true Looping 1 Loops n Group of statements that are executed repeatedly while some condition remains true n Each execution of the group of statements is called an iteration of the loop 2 Example counter 1,

More information

Fine Arts in Solow Model. Abstract

Fine Arts in Solow Model. Abstract Fine Arts in Solow Model Tetsuya Saito Department of Economics, SUNY at Buffalo Abstract Applying some built-in functions of Mathematica, this note provides some graphics derived from convergent paths

More information

CS 360 Programming Languages Interpreters

CS 360 Programming Languages Interpreters CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like

More information

Introduction to Functional Programming

Introduction to Functional Programming A Level Computer Science Introduction to Functional Programming William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London Aims and Claims Flavour of Functional

More information

Inverse Iteration Algorithms for Julia Sets

Inverse Iteration Algorithms for Julia Sets Inverse Iteration Algorithms for Julia Sets by Mark McClure Inverse iteration algorithms are extremely fast methods to generate images of Julia sets. While they are fairly simple to understand and implement,

More information

259 Lecture 25: Simple Programming

259 Lecture 25: Simple Programming 259 Lecture 25: Simple Programming In[1]:= In[2]:= Off General::spell Off General::spell1 Note: To type a command in a Mathematica notebook, use the mouse to move the cursor until it is horizontal, click

More information

Introduction to Mathematica

Introduction to Mathematica Introduction to Mathematica Thomas Hahn Max-Planck-Institut für Physik München T. Hahn, Introduction to Mathematica p.1 Expert Systems In technical terms, Mathematica is an Expert System. Knowledge is

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012 CMSC 33 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 2, FALL 22 TOPICS TODAY Bits of Memory Data formats for negative numbers Modulo arithmetic & two s complement Floating point formats

More information

functional programming in Python, part 2

functional programming in Python, part 2 Programming Languages Week 2 functional programming in Python, part 2 College of Information Science and Engineering Ritsumeikan University review of part 1 eliminating assignment makes programs easier

More information

Operators And Expressions

Operators And Expressions Operators And Expressions Operators Arithmetic Operators Relational and Logical Operators Special Operators Arithmetic Operators Operator Action Subtraction, also unary minus + Addition * Multiplication

More information

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017 Introduction to Concepts in Functional Programming CS16: Introduction to Data Structures & Algorithms Spring 2017 Outline Functions State Functions as building blocks Higher order functions Map Reduce

More information

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Algebra 1 Summer Review Packet DUE THE FIRST DAY OF SCHOOL

Algebra 1 Summer Review Packet DUE THE FIRST DAY OF SCHOOL Name: Algebra 1 Summer Review Packet DUE THE FIRST DAY OF SCHOOL About Algebra 1: Algebra 1 teaches students to think, reason and communicate mathematically. Students use variables to determine solutions

More information

Fine Arts and Solow Model: A Clarification

Fine Arts and Solow Model: A Clarification San Jose State University From the SelectedWorks of Yeung-Nan Shieh 2008 Fine Arts and Solow Model: A Clarification Yeung-Nan Shieh, San Jose State University Jason Kao Available at: https://works.bepress.com/yeung-nan_shieh/9/

More information

HIGHER ORDER FUNCTIONS 2

HIGHER ORDER FUNCTIONS 2 HIGHER ORDER FUNCTIONS 2 COMPUTER SCIENCE 61A June 21, 2012 1. You already saw in lecture a method to check if a number is prime: def is_prime(n): k = 2 while k < n: if n % k == 0: return False k += 1

More information

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes Multiple-byte data CMSC 313 Lecture 03 big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes UMBC, CMSC313, Richard Chang 4-5 Chapter

More information

1 An introduction to Mathematica

1 An introduction to Mathematica An introduction to Mathematica Mathematica is a very large and seemingly complex system. It contains hundreds of functions for performing various tasks in science, mathematics, and engineering, including

More information

Chapter 15 Functional Programming Languages

Chapter 15 Functional Programming Languages Chapter 15 Functional Programming Languages Fundamentals of Functional Programming Languages Introduction to Scheme A programming paradigm treats computation as the evaluation of mathematical functions.

More information

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

More information

Functional Programming Languages (FPL)

Functional Programming Languages (FPL) Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

More information

Timing for Ripple Carry Adder

Timing for Ripple Carry Adder Timing for Ripple Carry Adder 1 2 3 Look Ahead Method 5 6 7 8 9 Look-Ahead, bits wide 10 11 Multiplication Simple Gradeschool Algorithm for 32 Bits (6 Bit Result) Multiplier Multiplicand AND gates 32

More information

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value 1 Number System Introduction In this chapter, we will study about the number system and number line. We will also learn about the four fundamental operations on whole numbers and their properties. Natural

More information

Representations of Curves and Surfaces, and of their Tangent Lines, and Tangent Planes in R 2 and R 3 Robert L.Foote, Fall 2007

Representations of Curves and Surfaces, and of their Tangent Lines, and Tangent Planes in R 2 and R 3 Robert L.Foote, Fall 2007 CurvesAndSurfaces.nb Representations of Curves and Surfaces, and of their Tangent Lines, and Tangent Planes in R and R 3 Robert L.Foote, Fall 007 Curves and Surfaces Graphs ü The graph of f : Æ is a curve

More information

Making Holes and Windows in Surfaces

Making Holes and Windows in Surfaces The Mathematica Journal Making Holes and Windows in Surfaces Alan Horwitz In this article, we demonstrate makehole, a program which removes points from any Graphics or Graphics3D picture whose coordinates

More information

Functions. Def. Let A and B be sets. A function f from A to B is an assignment of exactly one element of B to each element of A.

Functions. Def. Let A and B be sets. A function f from A to B is an assignment of exactly one element of B to each element of A. Functions functions 1 Def. Let A and B be sets. A function f from A to B is an assignment of exactly one element of B to each element of A. a A! b B b is assigned to a a A! b B f ( a) = b Notation: If

More information

Functional programming with Common Lisp

Functional programming with Common Lisp Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

More information

Definition MATH Benjamin V.C. Collins, James A. Swenson MATH 2730

Definition MATH Benjamin V.C. Collins, James A. Swenson MATH 2730 MATH 2730 Benjamin V.C. Collins James A. Swenson s and undefined terms The importance of definition s matter! may be more important in Discrete Math than in any math course that you have had previously.

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

Prepared by: Shraddha Modi

Prepared by: Shraddha Modi Prepared by: Shraddha Modi Introduction Operator: An operator is a symbol that tells the Computer to perform certain mathematical or logical manipulations. Expression: An expression is a sequence of operands

More information

And Now to Something Completely Different: Finding Roots of Real Valued Functions

And Now to Something Completely Different: Finding Roots of Real Valued Functions And Now to Something Completely Different: Finding Roots of Real Valued Functions Four other Oysters followed them, And yet another four; And thick and fast they came at last, And more, and more, and more{

More information

autograd tutorial Paul Vicol, Slides Based on Ryan Adams January 30, 2017 CSC 321, University of Toronto

autograd tutorial Paul Vicol, Slides Based on Ryan Adams January 30, 2017 CSC 321, University of Toronto autograd tutorial Paul Vicol, Slides Based on Ryan Adams January 30, 2017 CSC 321, University of Toronto 1 tutorial outline 1. Automatic Differentiation 2. Introduction to Autograd 3. IPython Notebook

More information

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters. CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3

More information

Lists and Loops. defining lists lists as queues and stacks inserting and removing membership and ordering lists

Lists and Loops. defining lists lists as queues and stacks inserting and removing membership and ordering lists Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule MCS

More information

Expression Evaluation and Control Flow. Outline

Expression Evaluation and Control Flow. Outline Expression Evaluation and Control Flow In Text: Chapter 6 Outline Notation Operator Evaluation Order Operand Evaluation Order Overloaded operators Type conversions Short-circuit evaluation of conditions

More information

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website: Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

Functional Programming Languages (FPL)

Functional Programming Languages (FPL) Functional Programming Languages (FPL) 1. Definitions... 3 2. Applications... 3 3. Examples... 4 4. FPL Characteristics:... 5 5. Lambda calculus (LC)... 6 5.1. LC expressions forms... 6 5.2. Semantic of

More information

LECTURE 17. Expressions and Assignment

LECTURE 17. Expressions and Assignment LECTURE 17 Expressions and Assignment EXPRESSION SYNTAX An expression consists of An atomic object, e.g. number or variable. An operator (or function) applied to a collection of operands (or arguments)

More information

Number Theory Open, Round 1 Test #101

Number Theory Open, Round 1 Test #101 Number Theory Open, Round 1 Test #101 1. Write your 6-digit ID# in the I.D. NUMBER grid, left-justified, and bubble. Check that each column has only one number darkened. 2. In the EXAM NO. grid, write

More information

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017 SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Programming Languages

Programming Languages Programming Languages Lambda Calculus and Scheme CSCI-GA.2110-003 Fall 2011 λ-calculus invented by Alonzo Church in 1932 as a model of computation basis for functional languages (e.g., Lisp, Scheme, ML,

More information

Operators in java Operator operands.

Operators in java Operator operands. Operators in java Operator in java is a symbol that is used to perform operations and the objects of operation are referred as operands. There are many types of operators in java such as unary operator,

More information

Unit 1 Lesson 4. Introduction to Control Statements

Unit 1 Lesson 4. Introduction to Control Statements Unit 1 Lesson 4 Introduction to Control Statements Essential Question: How are control loops used to alter the execution flow of a program? Lesson 4: Introduction to Control Statements Objectives: Use

More information

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc. CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:

More information

Recitation 1. Scan. 1.1 Announcements. SkylineLab has been released, and is due Friday afternoon. It s worth 125 points.

Recitation 1. Scan. 1.1 Announcements. SkylineLab has been released, and is due Friday afternoon. It s worth 125 points. Recitation 1 Scan 1.1 Announcements SkylineLab has been released, and is due Friday afternoon. It s worth 125 points. BignumLab will be released on Friday. 1 2 RECITATION 1. SCAN 1.2 What is scan? In the

More information

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

OffPlot::"plnr"; OffGraphics::"gptn"; OffParametricPlot3D::"plld" Needs"Graphics`Arrow`" Needs"VisualLA`"

OffPlot::plnr; OffGraphics::gptn; OffParametricPlot3D::plld NeedsGraphics`Arrow` NeedsVisualLA` Printed from the Mathematica Help Browser of.: Transformation of Functions In this section, we will explore three types of transformations:.) Shifting.) Reflections (or flips).) Stretches and compressions

More information

This file contains an excerpt from the character code tables and list of character names for The Unicode Standard, Version 3.0.

This file contains an excerpt from the character code tables and list of character names for The Unicode Standard, Version 3.0. Range: This file contains an excerpt from the character code tables and list of character names for The Unicode Standard, Version.. isclaimer The shapes of the reference glyphs used in these code charts

More information

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis Chapter 14 Functional Programming Programming Languages 2nd edition Tucker and Noonan It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

More information

It Does Algebra. Note we have a list of lists, and the elements inside are expressions: we'll see more explicit functional constructs later

It Does Algebra. Note we have a list of lists, and the elements inside are expressions: we'll see more explicit functional constructs later Mathematica as Lisp Outline of this talk: 1) Show a few "Gee Whiz" features of Mathematica -show some algebraic calcs -show some nice typesetting -show some pretty graphics 2) Is it lisp? - front end /

More information

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control Chapter 2 Control, Quick Overview Control Selection Selection Selection is how programs make choices, and it is the process of making choices that provides a lot of the power of computing 1 Python if statement

More information

A model for an intramuscular injection of a medication.

A model for an intramuscular injection of a medication. injection_model.nb 1 A model for an intramuscular injection of a medication. Needs@"Graphics`Colors`"D; Intravenous injection of a medication: Injected drug in blood decays exponentially due to metabolic

More information

COP 4516: Math for Programming Contest Notes

COP 4516: Math for Programming Contest Notes COP 4516: Math for Programming Contest Notes Euclid's Algorithm Euclid's Algorithm is the efficient way to determine the greatest common divisor between two integers. Given two positive integers a and

More information

Imperative languages

Imperative languages Imperative languages Von Neumann model: store with addressable locations machine code: effect achieved by changing contents of store locations instructions executed in sequence, flow of control altered

More information

Algebra. Mathematica QuickStart for Calculus 101C. Solving Equations. Factoring. Exact Solutions to single equation:

Algebra. Mathematica QuickStart for Calculus 101C. Solving Equations. Factoring. Exact Solutions to single equation: Mathematica QuickStart for Calculus 101C Algebra Solving Equations Exact Solutions to single equation: In[88]:= Solve@x^3 + 5 x - 6 ã 0, xd Out[88]= :8x Ø 1, :x Ø 1 2 I-1 + Â 23

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Functions. How is this definition written in symbolic logic notation?

Functions. How is this definition written in symbolic logic notation? functions 1 Functions Def. Let A and B be sets. A function f from A to B is an assignment of exactly one element of B to each element of A. We write f(a) = b if b is the unique element of B assigned by

More information

A B C ((NOT A) AND B) OR C

A B C ((NOT A) AND B) OR C chapter5 pg 184 1,2,6 1 Assuming that x=1 and y=2, determine the value of each of the following Boolean expressing: (x=1) AND(y=3) (x1) Not[(x=1) AND (y=2)] Answer: False True False 2 Assume

More information

callback, iterators, and generators

callback, iterators, and generators callback, iterators, and generators 1 Adding a Callback Function a function for Newton s method a function of the user to process results 2 A Newton Iterator defining a counter class refactoring the Newton

More information

Flow Control: Branches and loops

Flow Control: Branches and loops Flow Control: Branches and loops In this context flow control refers to controlling the flow of the execution of your program that is, which instructions will get carried out and in what order. In the

More information

Homework 3. Assigned on 02/15 Due time: midnight on 02/21 (1 WEEK only!) B.2 B.11 B.14 (hint: use multiplexors) CSCI 402: Computer Architectures

Homework 3. Assigned on 02/15 Due time: midnight on 02/21 (1 WEEK only!) B.2 B.11 B.14 (hint: use multiplexors) CSCI 402: Computer Architectures Homework 3 Assigned on 02/15 Due time: midnight on 02/21 (1 WEEK only!) B.2 B.11 B.14 (hint: use multiplexors) 1 CSCI 402: Computer Architectures Arithmetic for Computers (2) Fengguang Song Department

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013 TOPICS TODAY Bits of Memory Data formats for negative numbers Modulo arithmetic & two s complement Floating point

More information

Operators in C. Staff Incharge: S.Sasirekha

Operators in C. Staff Incharge: S.Sasirekha Operators in C Staff Incharge: S.Sasirekha Operators An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

CSE : Python Programming

CSE : Python Programming CSE 399-004: Python Programming Lecture 10: Functional programming, Memoization March 26, 2007 http://www.seas.upenn.edu/~cse39904/ Announcements Should have received email about meeting times Length:

More information

Introduction to Programming II W4260. Lecture 2

Introduction to Programming II W4260. Lecture 2 Introduction to Programming II W4260 Lecture 2 Overview Storing Data Basic types Arrays Controlling the flow of execution Loops (for, while) Ifthenelse Operators Arithmetic, relational, logical Functions

More information

Loops / Repetition Statements

Loops / Repetition Statements Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

More information

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method. Reals 1 13 Reals Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method. 13.1 Floating-point numbers Real numbers, those declared to be

More information

You can drag the graphs using your mouse to rotate the surface.

You can drag the graphs using your mouse to rotate the surface. The following are some notes for relative maxima and mimima using surfaces plotted in Mathematica. The way to run the code is: Select Menu bar -- Evaluation -- Evaluate Notebook You can drag the graphs

More information

Programming for the Web with PHP

Programming for the Web with PHP Aptech Ltd Version 1.0 Page 1 of 11 Table of Contents Aptech Ltd Version 1.0 Page 2 of 11 Abstraction Anonymous Class Apache Arithmetic Operators Array Array Identifier arsort Function Assignment Operators

More information