Informatics 1 Functional Programming Lecture 5. Function properties. Don Sannella University of Edinburgh

Similar documents
Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

Standard prelude. Appendix A. A.1 Classes

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

More fun with recursion

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

CS 320: Concepts of Programming Languages

A tour of the Haskell Prelude

Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh

Introduction to Programming and 4Algorithms Abstract Types. Uwe R. Zimmer - The Australian National University

CSCE 314 Programming Languages. Functional Parsers

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Functional Parsers

Module Title: Informatics 1 Functional Programming (first sitting) Exam Diet (Dec/April/Aug): December 2014 Brief notes on answers:

A general introduction to Functional Programming using Haskell

Abstract Types, Algebraic Types, and Type Classes

Informatics 1 Functional Programming Lectures 15 and 16. IO and Monads. Don Sannella University of Edinburgh

Basic types and definitions. Chapter 3 of Thompson

CSCE 314 Programming Languages. Monadic Parsing

CSc 520. Principles of Programming Languages 11: Haskell Basics

CSCE 314 Programming Languages. Monadic Parsing

The Caesar Cipher Informatics 1 Functional Programming: Tutorial 3

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

An introduction introduction to functional functional programming programming using usin Haskell

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

Lecture 2: List algorithms using recursion and list comprehensions

Introduction to Programming, Aug-Dec 2006

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

Shell CSCE 314 TAMU. Higher Order Functions

Higher Order Functions in Haskell

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

CS 320: Concepts of Programming Languages

Lecture 19: Functions, Types and Data Structures in Haskell

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

EECS 700 Functional Programming

7. String Methods. Methods. Methods. Data + Functions Together. Designing count as a Function. Three String Methods 1/22/2016

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

Programming Languages Fall 2013

Functional Programming TDA 452, DIT 142

CSc 372. Comparative Programming Languages. 18 : Haskell Type Classes. Department of Computer Science University of Arizona

Informatics 1 Functional Programming 19 Tuesday 23 November IO and Monads. Philip Wadler University of Edinburgh

CSCE 314 Programming Languages

Haskell Modules. Input and Output. Urtė Zubkaitė

Software Properties as Axioms and Theorems

Week 5 Tutorial Structural Induction

7. String Methods. Topics: Methods and Data More on Strings Functions and Methods The String Class

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.

Functional Programming TDA 452, DIT 142

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Lambda expressions, functions and binding

Introduction to Functional Programming using Haskell

2. (18 points) Suppose that the following definitions are made (same as in Question 1):

CIS 252 Introduction to Computer Science Section M013: Exam 1 (Pink) February 22, Points Possible

CS 440: Programming Languages and Translators, Spring 2019 Mon

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

CSc 372 Comparative Programming Languages

CIS 252 Introduction to Computer Science Section M013: Exam 1 (Blue) February 22, Points Possible

Haskell Overview II (2A) Young Won Lim 8/9/16

Combinational Logic & Circuits

CIS 252 Introduction to Computer Science Section M019: Exam 1 (Yellow) February 22, Points Possible

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

CS 320: Concepts of Programming Languages

Programming Language Concepts: Lecture 14

CS 360: Programming Languages Lecture 10: Introduction to Haskell

IA014: Advanced Functional Programming

Programming in Haskell Aug-Nov 2015

CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++

CSCE 110 PROGRAMMING FUNDAMENTALS

Infix to Postfix Conversion

Introduction to String Manipulation

Strings are actually 'objects' Strings

Intro to Haskell Notes: Part 5

Case by Case. Chapter 3

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

Introduction to Programming: Lecture 3

Chapter 8 - Characters and Strings

CIS 252 Introduction to Computer Science Section M013: Exam 2 (Green) March 29, Points Possible

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

Haskell Syntax in Functions

Introduction to Programming, Aug-Dec 2008

QuickCheck, SmallCheck & Reach: Automated Testing in Haskell. Tom Shackell

Haskell through HUGS THE BASICS

Shell CSCE 314 TAMU. Functions continued

CS3157: Advanced Programming. Outline

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

What we will learn about this week:

02157 Functional Programming Tagged values and Higher-order list functions

Type Processing by Constraint Reasoning

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Haskell Overview II (2A) Young Won Lim 9/26/16

today cs3157-fall2002-sklar-lect05 1

Math 171 Proficiency Packet on Integers

Transcription:

Informatics 1 Functional Programming Lecture 5 Function properties Don Sannella University of Edinburgh

Part I Booleans and characters

Boolean operators not :: Bool -> Bool (&&), ( ) :: Bool -> Bool -> Bool not False True not True False False && False False False && True False True && False False True && True True False False False False True True True False True True True True

Defining operations on characters islower :: Char -> Bool islower x a < x && x < z isupper :: Char -> Bool isupper x A < x && x < Z isdigit :: Char -> Bool isdigit x 0 < x && x < 9 isalpha :: Char -> Bool isalpha x islower x isupper x

Defining operations on characters digittoint :: Char -> Int digittoint c isdigit c ord c - ord 0 inttodigit :: Int -> Char inttodigit d 0 < d && d < 9 chr (ord 0 + d) tolower :: Char -> Char tolower c isupper c chr (ord c - ord A + ord a ) otherwise c toupper :: Char -> Char toupper c islower c chr (ord c - ord a + ord A ) otherwise c These rely on the conversion functions: ord :: Char -> Int chr :: Int -> Char -- same as: fromenum :: Char -> Int -- same as: toenum :: Int -> Char

Part II Conditionals and Associativity

Conditional equations max :: Int -> Int -> Int max x y x > y x y > x y max3 :: Int -> Int -> Int -> Int max3 x y z x > y && x > z x y > x && y > z y z > x && z > y z

Conditional equations with otherwise max :: Int -> Int -> Int max x y x > y x otherwise y max3 :: Int -> Int -> Int -> Int max3 x y z x > y && x > z x y > x && y > z y otherwise z otherwise :: Bool otherwise True

Conditional expressions max :: Int -> Int -> Int max x y if x > y then x else y max3 :: Int -> Int -> Int -> Int max3 x y z if x > y && x > z then x else if y > x && y > z then y else z

Another way to define max3 max3 :: Int -> Int -> Int -> Int max3 x y z if x > y then if x > z then x else z else if y > z then y else z

Key points about conditionals As always: write your program in a form that is easy to read. Don t worry (yet) about efficiency: premature optimization is the root of much evil. Conditionals are your friend: without them, programs could do very little that is interesting. Conditionals are your enemy: each conditional doubles the number of test cases you must consider. A program with five two-way conditionals requires 2 5 32 test cases to try every path through the program. A program with ten two-way conditionals requires 2 10 1024 test cases.

A better way to define max3 max3 :: Int -> Int -> Int -> Int max3 x y z max (max x y) z

An even better way to define max3 max3 :: Int -> Int -> Int -> Int max3 x y z x max y max z max :: Int -> Int -> Int max x y x > y x otherwise y

An even better way to define max3 max3 :: Int -> Int -> Int -> Int max3 x y z x max y max z max :: Int -> Int -> Int x max y x > y x otherwise y x + y stands for (+) x y x > y stands for (>) x y x max y stands for max x y

Associativity prop_max_assoc :: Int -> Int -> Int -> Bool prop_max_assoc x y z (x max y) max z x max (y max z) It doesn t matter where the parentheses go with an associative operator, so we often omit them. Why we use infix notation prop_max_assoc :: Int -> Int -> Int -> Bool prop_max_assoc x y z max (max x y) z max x (max y z) This is much harder to read than infix notation!

Key points about associativity There are a few key properties about operators: associativity, identity, commutativity, distributivity, zero, idempotence. You should know and understand these properties. When you meet a new operator, the first question you should ask is Is it associative? The second is Does it have an identity? Associativity is our friend, because it means we don t need to worry about parentheses. The program is easier to read. Associativity is our friend, because it is key to writing programs that run twice as fast on dual-core machines, and a thousand times as fast on machines with a thousand cores.

Part III Append

Append (++) :: [a] -> [a] -> [a] [] ++ ys ys (x:xs) ++ ys x : (xs ++ ys) "abc" ++ "de" ( a : ( b : ( c : []))) ++ ( d : ( e : [])) a : (( b : ( c : [])) ++ ( d : ( e : []))) a : ( b : (( c : []) ++ ( d : ( e : [])))) a : ( b : ( c : ([] ++ ( d : ( e : []))))) a : ( b : ( c : ( d : ( e : [])))) "abcde"

Append (++) :: [a] -> [a] -> [a] [] ++ ys ys (x:xs) ++ ys x : (xs ++ ys) "abc" ++ "de" a : ("bc" ++ "de") a : ( b : ("c" ++ "de")) a : ( b : ( c : ("" ++ "de"))) a : ( b : ( c : "de")) "abcde"

Properties of append prop_append_assoc :: [Int] -> [Int] -> [Int] -> Bool prop_append_assoc xs ys zs (xs ++ ys) ++ zs xs ++ (ys ++ zs) prop_append_ident :: [Int] -> Bool prop_append_ident xs xs ++ [] xs && xs [] ++ xs prop_append_cons :: Int -> [Int] -> Bool prop_append_cons x xs [x] ++ xs x : xs

Efficiency (++) :: [a] -> [a] -> [a] [] ++ ys ys (x:xs) ++ ys x : (xs ++ ys) "abc" ++ "de" a : ("bc" ++ "de") a : ( b : ("c" ++ "de")) a : ( b : ( c : ("" ++ "de"))) a : ( b : ( c : "de")) "abcde" Computing xs ++ ys takes about n steps, where n is the length of xs.

A useful fact -- prop_sum.hs import Test.QuickCheck prop_sum :: Int -> Property prop_sum n n > 0 > sum [1..n] n * (n+1) div 2 [melchior]dts: ghci prop_sum.hs GHCi, version 6.8.3: http://www.haskell.org/ghc/ :? for help *Main> quickcheck prop_sum +++ OK, passed 100 tests. *Main>

Associativity and Efficiency: Left vs. Right Compare computing (associated to the left) with computing (associated to the right) ((xs 1 ++ xs 2 ) ++ xs 3 ) ++ xs 4 xs 1 ++ (xs 2 ++ (xs 3 ++ xs 4 )) where n 1, n 2, n 3, n 4 are the lengths of xs 1, xs 2, xs 3, xs 4. Associating to the left takes n 1 + (n 1 + n 2 ) + (n 1 + n 2 + n 3 ) steps. If we have m lists of length n, it takes about m 2 n steps. Associating to the right takes n 1 + n 2 + n 3 steps. If we have m lists of length n, it takes about mn steps. When m 1000, the first is a thousand times slower than the second!

Associativity and Efficiency: Sequential vs. Parallel Compare computing (sequential) with computing (parallel) x 1 + (x 2 + (x 3 + (x 4 + (x 5 + (x 6 + (x 7 + x 8 )))))) ((x 1 + x 2 ) + (x 3 + x 4 )) + ((x 5 + x 6 ) + (x 7 + x 8 )) In sequence, summing 8 numbers takes 7 steps. If we have m numbers it takes m 1 steps. In parallel, summing 8 numbers takes 3 steps. x 1 + x 2 and x 3 + x 4 and x 5 + x 6 and x 7 + x 8 (x 1 + x 2 ) + (x 3 + x 4 ) and (x 5 + x 6 ) + (x 7 + x 8 ), ((x 1 + x 2 ) + (x 3 + x 4 )) + ((x 5 + x 6 ) + (x 7 + x 8 )) If we have m numbers it takes log 2 m steps. When m 1000, the first is a hundred times slower than the second!