© M. Winter COSC 4P41 – Functional Programming 2.12.1 Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.

Slides:



Advertisements
Similar presentations
Haskell Lets review some of the Haskell concepts you have been learning on your own. The answers are included, but try it yourself first.
Advertisements

Exercises – don’t use built in functions for these as we want the practice Write a recursive function to add up all the numbers in a list "flatten" a list.
© M. Winter COSC 4P41 – Functional Programming Patterns of computation over lists Applying to all – mapping map :: (a -> b) -> [a] -> [b] map f.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Advanced Programming Andrew Black and Tim Sheard Lecture 4 Intro to Haskell.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
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.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Overview of the Haskell 98 Programming Language
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.
© M. Winter COSC 4P41 – Functional Programming Modules in Haskell Using modules to structure a large program has a number of advantages: Parts of.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
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,
Recursion Higher Order Functions CSCE 314 Spring 2016.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
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}
Polymorphic Functions
Functional Programming
Conditional Expressions
Recursion.
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
Theory of Computation Lecture 4: Programs and Computable Functions II
Functional Programming Lecture 12 - more higher order functions
Functions and patterns
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Computer Science 312 Haskell Lists 1.
PROGRAMMING IN HASKELL
Higher Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Higher Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CS 457/557: Functional Languages Folds
Lazy Programming Lazy evaluation:
CSE 3302 Programming Languages
Functions and patterns
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b f $ x = f x (.) :: (b -> c) -> (a -> b) -> (a -> c) (f. g) x = f (g x) flip :: (a -> b -> c) -> b -> a -> c flip f x y = f y x

© M. Winter COSC 4P41 – Functional Programming Recursion fac :: Int -> Int fac n | n == 0= 1 | n > 0= fac (n-1) * n fac :: Int -> Int fac n | n == 0= 1 | n > 0= fac (n-1) * n | otherwise= error ”fac only defined on natural numbers”

© M. Winter COSC 4P41 – Functional Programming Primitive Recursion fun :: Int -> Int fun n | n == 0 = … | n > 0 = … fun (n-1) … data Nat = Zero | Succ Nat fun :: Nat -> Nat fun Zero= … fun (Succ n)= … (fun n) …

© M. Winter COSC 4P41 – Functional Programming Tuples The type (t 1,t 2,…,t n ) consists of tuples of values (v 1,v 2,…,v n ) in which v 1 ::t 1,…,v n ::t n. Examples: minAndMax :: Int -> Int -> (Int,Int) minAndMax x y | x >= y = (y,x) | otherwise = (x,y) type ShopItem = (String,Int) A type definition is treated as a shorthand in Haskell – wherever a name like ShopItem is used, it has exactly the same effect as if (String,Int) had been written.

© M. Winter COSC 4P41 – Functional Programming Pattern Matching addPair :: (Int,Int) -> Int addPair (x,y) = x+y shift :: ((Int,Int),Int) -> (Int,(Int,Int)) shift ((x,y),z) = (x,(y,z)) name :: ShopItem -> String price :: ShopItem -> Int name (n,p) = n price (n,p) = p fst :: (a,b) -> asnd :: (a,b) -> b fst (x,y) = xsnd (x,y) = y

© M. Winter COSC 4P41 – Functional Programming Currying and Uncurrying multiply :: Int -> Int -> Int multiply x y = x*y multiplyUC :: (Int,Int) -> Int multiplyUC (x,y) = x*y g (x,y) x y curry g f(x,y) x y uncurry f

© M. Winter COSC 4P41 – Functional Programming Lists For the type t there is a Haskell type [t] of lists from t. [1,2,3,4,1,4] :: [Int] [True] :: [Bool] [’a’,’a’,’b’] :: [Char] ”aab” :: String but type String = [Char] [fac, (+1)] :: [Int -> Int] [] :: [a]

© M. Winter COSC 4P41 – Functional Programming Lists for enumerated types Lists of numbers, characters and other enumerated types [n.. m] is the list [n,n+1,…,m]; if n exceeds m, the list is empty. [2.. 7] = [2,3,4,5,6,7] [ ] = [3.1,4.1,5.1,6.1] [’a’.. ’m’] = ”abcdefghijklm” [n,p.. m] is the list of numbers whose first two elements are n and p with the numbers ascending in steps p-n up to m, [7,6.. 3] = [7,6,5,4,3] [0.0, ] = [0.0,0.3,0.6,0.9] [’a’,’c’.. ’n’] = ”acegikm”

© M. Winter COSC 4P41 – Functional Programming List comprehension Suppose that the list ex is [2,4,7], then the list comprehension [ 2*n | n <- ex ] will be [4,8,14]. Further examples: [ 2*n | n 3] = [8] [ m+n | (m,n) <- [(2,3),(2,1),(7,8)] ] = [5,3,15] [ m+n | n <- ex, even n, m <- ex, odd m] = [9,11] List comprehension is not a new feature of the language. Each expression using list comprehension can be translated into an expression of the core language, i.e., without list comprehension.

© M. Winter COSC 4P41 – Functional Programming Some list functions in Prelude.hs -- data [a] = [] | a : [a] head :: [a] -> a head (x:_) = x last :: [a] -> a last [x] = x last (_:xs) = last xs tail :: [a] -> [a] tail (_:xs) = xs init :: [a] -> [a] init [x] = [] init (x:xs) = x : init xs null :: [a] -> Bool null [] = True null (_:_) = False

© M. Winter COSC 4P41 – Functional Programming (++) :: [a] -> [a] -> [a] [] ++ ys = ys (x:xs) ++ ys = x : (xs ++ ys) map :: (a -> b) -> [a] -> [b] map f xs = [ f x | x <- xs ] filter :: (a -> Bool) -> [a] -> [a] filter p xs = [ x | x <- xs, p x ] concat :: [[a]] -> [a] concat = foldr (++) [] length :: [a] -> Int length = foldl' (\n _ -> n + 1) 0 (!!) :: [a] -> Int -> a (x:_) !! 0 = x (_:xs) !! n | n>0 = xs !! (n-1) (_:_) !! _ = error "Prelude.!!: negative index" [] !! _ = error "Prelude.!!: index too large"

© M. Winter COSC 4P41 – Functional Programming foldl :: (a -> b -> a) -> a -> [b] -> a foldl f z [] = z foldl f z (x:xs) = foldl f (f z x) xs foldl1 :: (a -> a -> a) -> [a] -> a foldl1 f (x:xs) = foldl f x xs foldr :: (a -> b -> b) -> b -> [a] -> b foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs) foldr1 :: (a -> a -> a) -> [a] -> a foldr1 f [x] = x foldr1 f (x:xs) = f x (foldr1 f xs) iterate :: (a -> a) -> a -> [a] iterate f x = x : iterate f (f x) reverse :: [a] -> [a] reverse = foldl (flip (:)) []

© M. Winter COSC 4P41 – Functional Programming take :: Int -> [a] -> [a] take n _ | n <= 0 = [] take _ [] = [] take n (x:xs) = x : take (n-1) xs drop :: Int -> [a] -> [a] drop n xs | n <= 0 = xs drop _ [] = [] drop n (_:xs) = drop (n-1) xs and, or :: [Bool] -> Bool and = foldr (&&) True or = foldr (||) False any, all :: (a -> Bool) -> [a] -> Bool any p = or. map p all p = and. map p elem, notElem :: Eq a => a -> [a] -> Bool elem = any. (==) notElem = all. (/=)

© M. Winter COSC 4P41 – Functional Programming sum, product :: Num a => [a] -> a sum = foldl' (+) 0 product = foldl' (*) 1 maximum, minimum :: Ord a => [a] -> a maximum = foldl1 max minimum = foldl1 min zip :: [a] -> [b] -> [(a,b)] zip = zipWith (\a b -> (a,b)) zipWith :: (a->b->c) -> [a]->[b]->[c] zipWith z (a:as) (b:bs) = z a b : zipWith z as bs zipWith _ _ _ = [] unzip :: [(a,b)] -> ([a],[b]) unzip = foldr (\(a,b) ~(as,bs) -> (a:as, b:bs)) ([], [])