1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 19: Functions, Types and Data Structures in Haskell COMP 144 Programming Language.

Slides:



Advertisements
Similar presentations
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Advertisements

Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions.
Comp 205: Comparative Programming Languages Functional Programming Languages: Haskell Lecture notes, exercises, etc., can be found at:
Cse536 Functional Programming 1 6/12/2015 Lecture #11, Nov. 01, 2004 Special Guest lecture by Tom Harke Today’s Topics –The Haskell Class system –Instance.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 21: Functional Programming in Python COMP 144 Programming Language Concepts Spring.
A Lightning Tour of Haskell Lecture 1, Designing and Using Combinators John Hughes.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 20: Lists and Higher-Order Functions in Haskell COMP 144 Programming Language Concepts.
Comp 205: Comparative Programming Languages User-Defined Types Enumerated types Parameterised types Recursive types Lecture notes, exercises, etc., can.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Functional Programming Notes for CSCE 190 Based on Sebesta,
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
Functional Programming Lecture 4 - More Lists Muffy Calder.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
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.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
Chapter 9: Functional Programming in a Typed Language.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
1 Functional Programming Lecture 6 - Algebraic Data Types.
Overview of the Haskell 98 Programming Language
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
CS5205Haskell1 CS5205: Foundations in Programming Languages Overview Lecturer : Chin Wei Ngan Office : S “Language.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
0 INTRODUCTION TO FUNCTIONAL PROGRAMMING Graham Hutton University of Nottingham.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
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,
Functional Programming Lecture 3 - Lists Muffy Calder.
Haskell. 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.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Lecture 14: Advanced Topic: Functional Programming
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Functional Programming
Conditional Expressions
Principles of programming languages 12: Functional programming
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
Theory of Computation Lecture 4: Programs and Computable Functions II
Functions and patterns
A lightening tour in 45 minutes
Haskell.
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
An aggregation mechanism
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
Functions and patterns
Functions and patterns
PROGRAMMING IN HASKELL
Presentation transcript:

1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 19: Functions, Types and Data Structures in Haskell COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos Feb 25 The University of North Carolina at Chapel Hill

2 COMP 144 Programming Language Concepts Felix Hernandez-Campos Functions Functions are the most important kind of value in functional programmingFunctions are the most important kind of value in functional programming –Functions are values! Mathematically, a function f associates an element of a set X to a unique element of second set YMathematically, a function f associates an element of a set X to a unique element of second set Y –We write f::X->Y three :: Integer -> Integer infinity :: Integer square :: Integer -> Integer smaller :: (Integer, Integer) -> Integer

3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Currying Functions Functional can be applied to a partially resulting in new functions with fewer argumentsFunctional can be applied to a partially resulting in new functions with fewer arguments smaller :: (Integer, Integer) -> Integer smaller (x,y) = if x <= y then x else y smaller2 :: Integer -> Integer -> Integer smaller2 x y = if x <= y then x else y The value of the application smaller2 x is a function with type Integer -> IntegerThe value of the application smaller2 x is a function with type Integer -> Integer –This is known as currying a function

4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Curried Functions Curried functions help reduce the number of parenthesesCurried functions help reduce the number of parentheses –Parentheses make the syntax of some functional languages (e.g. Lisp, Scheme) ugly Curried functions are usefulCurried functions are useful twice :: (Integer -> Integer) -> (Integer -> Integer) twice f x = f (f x) square :: Integer -> Integer square x = x * x quad :: Integer -> Integer quad = twice square Function as argument Function as result

5 COMP 144 Programming Language Concepts Felix Hernandez-Campos Operators Operators are functions in infix notation rather than prefix notationOperators are functions in infix notation rather than prefix notation –E.g rather than plus 3 4 Functions can be used in infix notation using the ‘f ‘ notationFunctions can be used in infix notation using the ‘f ‘ notation –E.g. 3 `plus` 4 Operators can be used in prefix notation using the (op) notationOperators can be used in prefix notation using the (op) notation –E.g. (+) 3 4 As any other function, operators can be applied partially using the sections notationAs any other function, operators can be applied partially using the sections notation –E.g. The type of (+3) is Integer -> Integer

6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Operator Associativity Operators associate from left to right (left- associative) or from right to left (right-associative)Operators associate from left to right (left- associative) or from right to left (right-associative) –E.g. - is left-associative 3 – 4 – 5 means (3 – 4) – 5 Operator -> is right-associativeOperator -> is right-associative –X –> Y –> Z means X -> (Y –> Z) Exercise: deduce the type of h inExercise: deduce the type of h in h x y = f (g x y) f :: Integer -> Integer g :: Integer -> Integer -> Integer

7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Function Definition Functions with no parameters are constantsFunctions with no parameters are constants –E.g. pi = 3.14 has type pi :: Float The definition of a function can depend on the value of the parametersThe definition of a function can depend on the value of the parameters –Definitions with conditional expressions –Definitions with guarded equation smaller :: Integer -> Integer -> Integer smaller x y = if x <= y then x else y smaller2 x y | x <= y = x | y > x = y

8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Recursive definitions Functional languages make extensive use of recursionFunctional languages make extensive use of recursion fact :: Integer -> Integer fact n = if n == 0 then 1 else n * fact (n – 1) What is the result of fact -1 ?What is the result of fact -1 ? The following definition is more aThe following definition is more a fact n | n < 0 = error “negative argument for fact” | n == 0 = 1 | otherwise = n * fact (n-1)

9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Local Definitions Another useful notation in function definition is a local definitionAnother useful notation in function definition is a local definition Local declarations are introduced using the keyword whereLocal declarations are introduced using the keyword where For instance,For instance, f :: Integer -> Integer -> Integer f x y | x <= 10 = x + a | x > 10 = x – a where a = square b b = y + 1 b = y + 1

10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Types The following primitive time are available in HaskellThe following primitive time are available in Haskell –Bool –Integer –Float –Double –Char Any expression in Haskell has a typeAny expression in Haskell has a type –Primitive types –Derived types –Polymorphic types

11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Polymorphic Types Some functions and operations work with many typesSome functions and operations work with many types Polymorphic types are specified using type variablesPolymorphic types are specified using type variables For instance, the curry function has a polymorphic typeFor instance, the curry function has a polymorphic type curry :: ((a, b) -> c) -> (a -> b -> c) curry f x y = f (x,y) Type variables can be qualified using type classesType variables can be qualified using type classes (*) :: Num a => a -> a -> a

12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lists Lists are the workhorse of functional programmingLists are the workhorse of functional programming Lists are denoted as sequences of elements separated by commas and enclosed within square bracketsLists are denoted as sequences of elements separated by commas and enclosed within square brackets –E.g. [1,2,3] The previous notation is a shorthand for the List data typeThe previous notation is a shorthand for the List data type –E.g. 1:2:3:[]

13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lists Functions Functions that operate on lists often have polymorphic typesFunctions that operate on lists often have polymorphic types length :: [a] -> Integer length [] = 0 length (x:xs) = 1 + length xs In the previous example, the appropriate definition of the function for the specific arguments was chosen using pattern matchingIn the previous example, the appropriate definition of the function for the specific arguments was chosen using pattern matching Polymorphic List Pattern Matching

14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lists Example Derivation length :: [a] -> Integer length [] = 0 (1) length (x:xs) = 1 + length xs (2) length [1,2,3] = { definition (2) } 1 + length [2,3] = { definition (2) } length [3] = { definition of + } 2 + length [3] = { definition (2) } length [] = { definition of + } 3 + length [] = { definition (1) } = { definition of + } 3

15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lists Other Functions head :: [a] -> a head (x:xs) = x tail :: [a] -> [a] tail (x:xs) = xs

16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Data Types New data types can be created using the keyword dataNew data types can be created using the keyword data ExamplesExamples data Bool = False | True data Color = Red | Green | Blue | Violet data Point a = Pt a a Type Constructor Polymorphic Type Type Constructor

17 COMP 144 Programming Language Concepts Felix Hernandez-Campos Polymorphic Types Type constructors have typesType constructors have types Pt :: a -> a -> Point data Point a = Pt a a ExamplesExamples Pt :: Point Float Pt 'a' 'b' :: Point Char Pt True False :: Point Bool

18 COMP 144 Programming Language Concepts Felix Hernandez-Campos Recursive Types Binary Trees data Tree a = Leaf a | Branch (Tree a) (Tree a) Branch :: Tree a -> Tree a -> Tree a Leaf :: a -> Tree a Example function that operates on treesExample function that operates on trees fringe :: Tree a -> [a] fringe (Leaf x) = [x] fringe (Branch left right) = fringe left ++ fringe right

19 COMP 144 Programming Language Concepts Felix Hernandez-Campos List Comprehensions Lists can be defined by enumeration using list comprehensionsLists can be defined by enumeration using list comprehensions –Syntax: [ f x | x <- xs ] [ (x,y) | x <- xs, y <- ys ] ExampleExample quicksort [] = [] quicksort (x:xs) = quicksort [y | y <- xs, y<x ] ++ [x] ++ quicksort [y | y =x]Generator

20 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment A Gentle Introduction to Haskell by Paul Hudak, John Peterson, and Joseph H. Fasel.A Gentle Introduction to Haskell by Paul Hudak, John Peterson, and Joseph H. Fasel. – –Read sections 1 and 2