Functional Programming Lecture 5 - Tuples. Packaging several values together Sometimes you need to package up several values into a single object –A function.

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

CSC 270 Nov. 22, 2005 Last Day of Scheme Dr. Stephen Bloch
Haskell Lets review some of the Haskell concepts you have been learning on your own. The answers are included, but try it yourself first.
Modern Programming Languages, 2nd ed.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Muller ‘ s mothod Hun Hee Lee. Muller ’ s method Muller ’ s method for solving an equation of one variable f(x)=0. Muller ’ s method is an iterative method.
Logic & program control part 2: Simple selection structures.
7-5 solving quadratic equations
Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
Chapter 1 pp 1-14 Properties of Algorithms Pseudocode.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Data Types. Every program must deal with data The data is usually described as a certain type This type determines what you can do with the data and how.
5.3 Discriminant and 5.4Find ABC Discriminant: b 2 – 4ac A, B, C ax 2 + bx + c = 0.
5.5 Solving Quadratic Equations by Factoring
6.5 – Solving Equations with Quadratic Techniques.
Haskell Chapter 1, Part II. List Comprehension  List comprehensions are a way to filter, transform and combine lists  Similar to mathematical set comprehensions.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Solving Quadratic Equations by Factoring. Solution by factoring Example 1 Find the roots of each quadratic by factoring. factoring a) x² − 3x + 2 b) x².
Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
1 Functional Programming Lecture 6 - Algebraic Data Types.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
Copyright © 2011 Pearson, Inc. P.5 Solving Equations Graphically, Numerically and Algebraically.
Tuesday December 10, 2013 Bell Ringer: Solve the following equation: 4(a - 6) + 4 = 2a - 6.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
9.5 - The Quadratic Formula
Ch : Solving Systems of Equations Algebraically.
Chapter 6 Section 5. Objectives 1 Copyright © 2012, 2008, 2004 Pearson Education, Inc. Solving Quadratic Equations by Factoring Solve quadratic equations.
Solving Quadratic Equations by Factoring. Martin-Gay, Developmental Mathematics 2 Zero Factor Theorem Quadratic Equations Can be written in the form ax.
Solving Quadratic Equations. Factor: x² - 4x - 21 x² -21 a*c = -21 b = -4 x + = -21 = x 3x3x x 3 (GCF) x-7 (x – 7)(x + 3)
Chapter SevenModern Programming Languages1 A Second Look At ML.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
9.5 Solving by Factoring Algebra 14.0, Predicting with Evidence What are the zeros for y = x 2 – x – 6? Now factor x 2 – x – 6 = 0 and solve. What.
3.1 Solving Systems By Graphing Or Substitution. * A system of equations is a collection of equations in the same variable. *A solution to a system is.
Functional Programming Lecture 3 - Lists Muffy Calder.
1 PROGRAMMING IN HASKELL Lecture 2 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Warm Up  1.) Write 15x 2 + 6x = 14x in standard form. (ax 2 + bx + c = 0)  2.) Evaluate b 2 – 4ac when a = 3, b = -6, and c = 5.
Discrete Mathematics Lecture # 13 Applications of Venn Diagram.
2.2 Solving Quadratic Equations Algebraically Quadratic Equation: Equation written in the form ax 2 + bx + c = 0 ( where a ≠ 0). Zero Product Property:
The Quadratic Formula..
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
Solving Equations Graphically, Numerically, and Algebraically
Theory of Computation Lecture 4: Programs and Computable Functions II
Functional Programming Lecture 12 - more higher order functions
Copyright 2013, 2010, 2007, 2005, Pearson, Education, Inc.
A lightening tour in 45 minutes
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Quadratic Equations, Functions, Zeros, and Models
PROGRAMMING IN HASKELL
THEORY OF COMPUTATION Lecture One: Automata Theory Automata Theory.
5.9 The Quadratic Formula 12/11/2013.
Solving Quadratics by Factoring
PROGRAMMING IN HASKELL
Type & Typeclass Syntax in function
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Quadratic Equations.
Chapter 6 Section 5.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Solving Quadratic Equations by Factoring
Terminology and Symbols
Presentation transcript:

Functional Programming Lecture 5 - Tuples

Packaging several values together Sometimes you need to package up several values into a single object –A function returns one value, but what if you have several values to return? –Several data values are used together; it is logical to treat the entire collection as one object.

Tuples (2,3) :: (Int,Int) –a pair or a 2-tuple (2, “dog”) :: (Int, String) –a pair or a 2-tuple (“Calder”,”Monday”,[9,11,14]) :: (String, String,[Int]) –a triple or a 3-tuple (pi, times2) :: (Double, Int->Int) –where times2 x = x * 2 ( ) :: ( ) –a 0-tuple (sometimes used as a dummy value) You can have n-tuples for any n.

x1 :: T1 … xn :: Tn (x1,…,xn) :: (T1,…Tn) What is type of (2,3)? What is type of (2,”dog”)? Convention, (5,6) is a pair, (Int,Int) is a Pair.

Tuples vs. Lists Similarities: Both package up several values into one. Both are type constructors. Differences: Tuple: fixed number of components, the components may have different types. (3,4) :: (Int,Int) (3,”hello”) :: (Int,[Char]) List: any number of elements, the elements must have the same type. [3,4] ::[Int] [3,”hello”] :: type error

Example: Cross Product Represent sets A and B as lists. mathematics A  B = {(x,y) | x  A, y  B} Haskell crossProd :: [a] -> [b] -> [(a,b)] crossProd xs ys = [(x,y) | x <- xs, y <- ys] crossProd “abc” [1..3] => [(‘a’,1), (‘a’,2), (‘a’,3), (‘b’,1), (‘b’,2), (‘b’,3), (‘c’,1), (‘c’,2), (‘c’,3)]

Example: A Database A database holds information about employees. type Item = (String, String, String, Int, Int) -- (name, address, postcode, age, salary) type Database = [Item] ex1 = [(“Smith”, “Byres”, “G12 8QQ”, 30, 30000), …..

Database queries Make a mailing label for every employee under 30 who is making at least 30k per year, and who lives within the G1 postcode area. labels :: Database -> [String] labels db = [n ++ “\n” ++ a ++ “\n” | (n,a,p,o,s) =30000, take 3 p == “G1_”] (n,a,p,o,s) is called a pattern. Note difference between `\n’ and “\n”

Returning several results Example: Quadratic Equation Quadratic equation ax 2 + bx + c = 0 has two solutions (- b   (b 2 - 4ac)) / 2a. We would like a function that solves the equation properly, and returns both solutions Note - in some cases there are complex solutions - we will ignore these.

Example: Quadratic Equation quadratic :: Double -> Double -> Double -> (Double,Double) 3 arguments and one result which is a pair quadratic a b c = (x1, x2) where x1 = (-b + d) / (2*a) x2 = (-b - d) / (2*a) d = sqrt (b^2 - 4*a*c) or quadratic a b c = let d = sqrt (b^2 - 4*a*c) x1 = (-b + d) / (2*a) x2 = (-b - d) / (2*a) in (x1, x2)

A note about floating point It is not always exact => 0.01 :: Double but => :: Double This is not an error. It is a property of floating point, not of Haskell. You must be careful with floating point, as in any programming language.

Rounding It is possible to round the numbers => 0.01 But rounded output DOES NOT affect the number actually represented, it gives only the illusion that the number is exactly correct.

Comparison of floating point It may be unsafe to use == to compare two floating point numbers –if == then should get this result else but you’ll get this result instead It does not make any difference if the output is printed in rounded form - the internal computation will be wrong. This is true for all programming languages

When should floating point be used? The input data already has some imprecision data measured in an experiment. You expect an approximate result, and you know what the “error tolerance” is. The result may be big (astronomical) or small (microscopic), and it would be written in scientific notation. Do not use floating point for money!