Haskell Chapter 3, Part II. Topics  Guards  Where  Let  Case (not emphasized)

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements Animated Version.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements.
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 14: Stepwise.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Models of Concurrency Manna, Pnueli.
Statement-Level Control Structures
1.1The Principle of Mathematical Induction 1.2Divisibility Chapter Summary Case Study Mathematical Induction 1.
Formal Semantics of Programming Languages 虞慧群 Topic 5: Axiomatic Semantics.
P ROGRAMMING L ANGUAGES : C ONTROL 1. S LIDES R EFERENCES Kenneth C. Louden, Control I: Expressions and Statements: Principles and Practice. 2.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Chapter 8 Statement-Level Control Structures. 1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded.
This Week Finish relational semantics Hoare logic Interlude on one-point rule Building formulas from programs.
ISBN Chapter 8 Statement-Level Control Structures.
The Flow of Control Among Statements.  Selection Statements  Iterative Statements  Unconditional Branching  Guarded Commands.
PZ12A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ12A - Guarded commands Programming Language Design.
ISBN Chapter 8 Statement-Level Control Structures.
Controlling Program Flows
ISBN Chapter 8 Statement-Level Control Structures.
Statement-Level Control Structures
CHOOSING A TOPIC  Consider your own interest(s)  Consider the audience  Consider the available resources  Structure of the speech – Time limit – Current.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
Exam Delay Exam II will be held during the first fifty (50) minutes of class on 13 November 2003.
C HAPTER 8 Statement-Level Control Structures. CCSB314 Programming Language C HAPTER 8 T OPICS Introduction Selection Statements Iterative Statements.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
Statement Level Flow of Control GOTOs and Other Weirdness Copyright © by Curt Hill.
XP 1 XSLT II Robin Burke ECT 360. XP 2 Outline Conditionals Numbering Functions and operators Variables and parameters Named and recursive templates.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
The Confident Writer Chapter 2: Using Prewriting Strategies.

CS 36 – February 2 Control structures –Sequence √ –Loops √ –Selection If-statement Case-statement Finding loops.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Chapter 8 Statement-Level Control Structures. 2 Chapter 8 Topics  Introduction  Selection Statements  Iterative Statements  Unconditional Branching.
HCS 455 Week 4 Individual The Policy Process Part II Write a 1,400- to 1,750-word paper on how a topic becomes a policy. In your paper, include the following.
Evaluating Sources: Definitions and Information for Research
Haskell: Syntax in Functions
Selection—Making Decisions
Dr. Vamsi Paruchuri University of Central Arkansas
Statement-Level Control Structures
Haskell.
Chapter 5 Structures.
Statement-Level Control Structures
Statement-Level Control Structures
The Linux Command Line Chapter 27
Given that, {image} {image} Evaluate the limit: {image} Choose the correct answer from the following: {image}
MSIS 655 Advanced Business Applications Programming
Chapter 4: Decision Structures and Boolean Logic
Iteration Implemented through loop constructs (e.g., in C++)
Program Flow Control Selection & repetition
Repetition Control Structure
Selection Statements.
Find the reference angle for the angle measuring {image}
Evaluate the limit: {image} Choose the correct answer from the following:
Topics discussed in this section:
Chapter8: Statement-Level Control Structures April 9, 2019
Relational Operators.
Selection—Making Decisions
Selection Statements Chapter 3.
More JavaScript B. Ramamurthy 5/7/2019.
Chapter 3: Selection Structures: Making Decisions
CIS 720 Lecture 3.
CIS 720 Lecture 3.
Chapter 4: Decision Structures and Boolean Logic
Given that {image} {image} Evaluate the limit: {image} Choose the correct answer from the following:
The Linux Command Line Chapter 27
Presentation transcript:

Haskell Chapter 3, Part II

Topics  Guards  Where  Let  Case (not emphasized)

Guards

Guarded Command Language  Proposed by Dijkstra  Statement of the form G -> S  The guard is a proposition, must be true for statement to execute  Purpose: to support a new programming methodology that supported verification (correctness) during development  Basic Idea: if the order of evaluation is not important, the program should not specify one. All guards are evaluated, if more than one are true, choose one non-deterministically.  But in Haskell, they are evaluated top-down… more like an if-else from:

Haskell Guards bmiTell :: Double -> String bmiTell bmi | bmi <= 18.5 = "You're underweight, you emo, you!" | bmi <= 25.0 = "You're supposedy normal. Pffft. " | bmi <= 30.0 = "You're fat! Lose some weight." | otherwise = "You're a whale, congratulations!“  At least one must evaluate to true, else error.

More examples  Guards can take two parameters max' :: (Ord a) => a -> a -> a max' a b | a <= b = b | otherwise = a myCompare :: (Ord a) => a-> a-> Ordering a `myCompare` b | a == b = EQ | a <= b = LT | otherwise = GT

where

 purpose: avoid calculating same value multiple times  scope: only within function  defined at end of function, visible to all guards bmiTell :: Double -> Double -> String bmiTell weight height | bmi <= 18.5 = "You're underweight, you emo, you!" | bmi <= 25.0 = "You're supposedy normal. Pffft. " | bmi <= 30.0 = "You're fat! Lose some weight." | otherwise = "You're a whale, congratulations!" where bmi = (weight / height ^2) * 703

or even more “where” clauses  Names must line up! bmiTell' :: Double -> Double -> String bmiTell' weight height | bmi <= skinny = "You're underweight, you emo, you!" | bmi <= normal = "You're supposedy normal. Pffft. " | bmi <= fat = "You're fat! Lose some weight." | otherwise = "You're a whale, congratulations!" where bmi = (weight / height ^2) * 703 skinny = 18.5 normal = 25.0 fat = 30.0

limits to where  where bindings are not shared across different function bodies. So this does not work! greet :: String -> String greet "Juan" = niceGreeting ++ "Juan" greet "Fernando" = niceGreeting ++ "Fernando" greet name = badGreeting ++ name where niceGreeting = "Hello, so nice to see you, " badGreeting = "Oh, it you..."

Pattern matching with where initials :: String -> String -> String initials firstname lastname = [f] ++ ". " ++ [l] ++ "." where (f:_) = firstname (l:_) = lastname

functions in where clause calcBmis :: [(Double, Double)] -> [Double] calcBmis xs = [bmi w h | (w, h) <- xs] where bmi weight height = (weight / height ^2) * try calcBmis [(120, 62), (150, 79)]

let

 lets are expressions  allow you to bind variables  very local (don’t span guards)  can be used in pattern matching cylinder :: Double -> Double -> Double cylinder r h = let sideArea = 2 * pi * r * h topArea = pi * r ^ 2 in sideArea + 2 * topArea

more let  Introduce function in a local scope threeSquares = let square x = x * x in (square 5, square 3, square 2)  Bind to value aFormula = 4 * (let a = 9 in a + 1) + 2  Pattern match to access list/tuple components threeTimesHundred = (let (a,b,c) = (1,2,3) in a+b+c) *100  Use in list comprehensions calcBmis' :: [(Double, Double)] -> [Double] calcBmis' xs = [bmi | (w, h) <- xs, let bmi = w / h ^ 2]

let in GHCi  let binds names that can be used throughout GHCi session

let vs. where  where  is part of a syntactic construct (used in guards)  can be used to share bindings across parts of a function that are not syntactically expressions  let is an expression  expressions have values  let can be used almost anywhere in code … anywhere an expression can occur  but the scope of a let is only the expression which it encloses.  let y = a*b f x = (x+y)/y in f c + f d

case

case expression describeList :: [a] -> String describeList ls = "The list is " ++ case ls of [] -> "empty" [a] -> "singleton" xs -> " longer"

play and share 1. cookTemp takes a string and returns a cook temperature (“Slow” = 350, “Medium” = 400, “Get ‘Er Done” = 450, otherwise 250) 2. cookTemp2 takes an integer and returns a string. Do the reverse of the above, use a where clause. Use inequalities. 3. greet takes a list in the form [“Cyndi”,”Ann”,”Rader”] and displays “Hello Cyndi Rader”. Use a where clause. Do it again with a let clause. 4. myInfo. Takes a nested list such as: [["Cyndi", "Rader"], ["Golden", "CO"]] or [["Cyndi", "Rader"]]. If the list contains both name and location (i.e., length 2), print “firstname lives in city” otherwise print “firstname lives somewhere”. Use a where clause for firstname and city 5. sumUsage takes a list of numbers that represent KB (e.g., from a directory) and creates the sum in MB (use a let function to do the conversion). 6. boo takes a list. If the list has four elements, add the first and third and multiply by 100. If the list has three elements, add the first and second and multiply by 100. (why? just for pattern matching practice)