Guards1. 2 Let’s test this function. Main> maxi 3 2 3 Here is a trace of the function: n m maxi 3 2 Guards or conditions are used to express various cases.

Slides:



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

11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Operators, Functions and Modules1 Operators and Functions.
Comp 205: Comparative Programming Languages Functional Programming Languages: Haskell Lecture notes, exercises, etc., can be found at:
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.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Loose endsCS-2301, B-Term “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
0 PROGRAMMING IN HASKELL Chapter 2 - First Steps.
Advanced Programming Andrew Black and Tim Sheard Lecture 2 Intro to Haskell.
"Loose ends"CS-2301 D-term “Loose Ends” CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming Language, 2 nd edition,
True/False. False True Subject May Go Here True / False ? Type correct answer here. Type incorrect answer here.
BB1756: Software Development Functional Programming in Haskell 2010/2011 Dan Russell WWW:
Fixing Broken Programs. How do you figure out what’s wrong? Look carefully at the error message. Locate the error – Most messages have line numbers –
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
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.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Introduction to Python
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Definitions. name :: Type answer :: Int name = expression answer = Definitions associate a name with a value of a certain type is of type greater.
Operators, Functions and Modules1 Pattern Matching & Recursion.
TGI Boat Race Game READ ME Do NOT delete or add ANY slides in this game. You will only need to edit the question slides to add your questions and answers.
Sept 2004 SDP-MSc Slide 1 Section 6 Introduction to Functional Programming.
0 PROGRAMMING IN HASKELL Some first steps Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Overview of the Haskell 98 Programming Language
Allegro CL Certification Program Lisp Programming Series Level I Session Basic Lisp Development in the IDE.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
5.2: Solving Systems of Equations using Substitution
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.
15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
Solving Systems of Linear Equations by Substitution; Applications Solve systems of linear equations using substitution. 2.Solve applications involving.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
JavaScript Errors and Debugging Web Design Sec 6-3 Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development.
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)
Upgrade on Windows 7. DownloadSoftware Download Software from link provided in Webliography: e/
Conditional Expressions
Programming Languages
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
CSE 341 Section 1 (9/28) With thanks to Dan Grossman, et. al. from previous versions of these slides.
Standard and Expanded Form
PROGRAMMING IN HASKELL
Get Typed with TypeScript!
A lightening tour in 45 minutes
Haskell Chapter 4.
Functional Programming Lecture 2 - Functions
CSE 3302 Programming Languages
Chapter 5: Loops and Files.
One-Step Equations with Subtraction
And the text with form..
Objective: Be able to add and subtract directed numbers.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Programming Languages
CS005 Introduction to Programming
Logical Operations In Matlab.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functional Programming Lecture 2 - Functions
Scratch Programming Lesson 7 Debugging.
Objective: Be able to add and subtract directed numbers.
One-Step Equations with Addition and Subtraction
PROGRAMMING IN HASKELL
Programming Languages Dan Grossman 2013
Presentation transcript:

Guards1

2 Let’s test this function. Main> maxi Here is a trace of the function: n m maxi 3 2 Guards or conditions are used to express various cases in the definition of a function. Example: maxi :: Int -> Int -> Int maxi n m | n >= m = n | otherwise = m ?? 3 > 2 = True = 3 maxi 2 5 ?? 2 >= 5 = False ?? Otherwise = True = 5

Guards3 function_name p1 p2 … pn | g1 = e1 | g2 = e2 … | otherwise = e The general form of function definitions using guards:

Guards4 Example: maxiOf3 :: Int -> Int -> Int -> Int maxiOf3 m n p | m >= n && m >=p = m | n >= p = n | otherwise = p If m is the maximum then the first guard will succeed and the result will be m. If the first guard fails then either n or p should be the maximum and this is what the second guard is checking. If the second guard succeeds then the result will be n. Otherwise, if both (first and second) guards fail then the last guard will cause p to be returned as maximum of the three arguments.

Guards5 Here is a trace of the function: m n p maxiOf ?? 9 >= 5 && 5 >= 4 = True && True = True = 9 maxiOf ?? 1 >= 2 && 2 >= 5 = False && False = False ?? 2 >= 5 = False ?? otherwise = True =5 Let’s test maxiOf3. Main> maxiOf maxiOf3 m n p | m >= n && n >=p = m | n >= p = n | otherwise = p

Guards6 allEqual :: Int -> Int -> Int -> Bool allEqual m n p = (n = = m) && (n = = p) m n p allEqual =(3 = = 2) && (3 = = 4) =False && False =False m n p allEqual (maxi 1 5) 5 (maxi 4 2) = (5 = = (maxi 1 5)) && (5 = = (maxi 4 2)) ?? 1 >= 5 = False ?? otherwise = True = (5 = = 5) && (5 = = (maxi 4 2)) ?? 4 >= 2 = True = (5 = = 5) && (5 = = 4) = True && False = False Another Example:

Guards7 Error Messages

Guards8 We will first use Notepad to create a file containing the script We will then use Hugs to interpret this script and find the errors We will use Notepad again to edit the file and correct the errors We will also test our functions by evaluating some expressions The script on the next slide contains errors

Guards9 add3 :: Int -> Int -> Int -> Int add3 n m p = n + m + p sig :: Int -> Int sig a_number | a_number < 0 = -1 | a_mumber == 0 = 0 | otherwise = 1 abs :: Int -> Int abs x | x < 0 = x * -1 | otherwise = x answer :: Int answer = 42

Guards10 __ __ __ __ ____ ___ _________________________________________ || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard ||___|| ||__|| ||__|| __|| Copyright (c) ||---|| ___|| World Wide Web: || || Report bugs to: || || Version: December 2001 _________________________________________ Hugs mode: Restart with command line option +98 for Haskell 98 mode Reading file "C:\Program Files\Hugs98\\lib\Prelude.hs": Hugs session for: C:\Program Files\Hugs98\\lib\Prelude.hs Type :? for help Prelude>

Guards11 Prelude> :l d:\haskell\small.hs Reading file "d:\haskell\small.hs": ERROR "d:\haskell\small.hs":12 - Definition of variable "abs" clashes with import Prelude> The problem here is that abs is a built-in function. One way of solving this problem is to use a different name for our function. We will now edit the file and fix the error. Prelude> :e This command ( :e ) tells Hugs to edit the last module.

Guards12 Reading file "d:\haskell\small.hs": Dependency analysis ERROR "d:\haskell\small.hs":7 - Undefined variable "a_mumber" There is an error here to be fixed and we do that using the :e command. Prelude> :e Reading file "d:\haskell\small.hs": Hugs session for: C:\Program Files\Hugs98\\lib\Prelude.hs d:\haskell\small.hs Main> We should now be able to use these functions in our expressions.