dr Robert Kowalczyk WMiI UŁ

Slides:



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

Haskell Lets review some of the Haskell concepts you have been learning on your own. The answers are included, but try it yourself first.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Advanced Programming Andrew Black and Tim Sheard Lecture 4 Intro to Haskell.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
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.
Operators, Functions and Modules1 Pattern Matching & Recursion.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
1 Functional Programming Lecture 6 - Algebraic Data Types.
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.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
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.
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.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
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.
© 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.
Advanced Functional Programming 2010
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
Haskell: Syntax in Functions
Conditional Expressions
Recursion.
dr Robert Kowalczyk WMiI UŁ
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
Theory of Computation Lecture 4: Programs and Computable Functions II
PROGRAMMING IN HASKELL
Functional Programming Lecture 12 - more higher order functions
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
Functional Programming
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell Strings and Tuples
Computer Science 312 Haskell Lists 1.
PROGRAMMING IN HASKELL
Higher Order Functions
PROGRAMMING IN HASKELL
Type & Typeclass Syntax in function
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
Lazy Programming Lazy evaluation:
CSE 3302 Programming Languages
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
Programming Languages
PROGRAMMING IN HASKELL
Functional Programming
Higher-Order Functions in Haskell
Computer Science 312 Making choices Tail Recursion
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

dr Robert Kowalczyk WMiI UŁ Summer School Haskell 2 dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Syntax in Haskell pattern matching f : Z -> Z f(0) = 1 f(1) = 3 f(2) = 6 f(x) = 0 if only x<>0 and x<>1 and x<>2 f :: Int -> Int f(0) = 1 f(1) = 3 f(2) = 6 f(x) = 0 (or f(_)=0) dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Syntax in Haskell case of f : Z -> Z f(0) = 1 f(1) = 3 f(2) = 6 f(x) = 0 if only x<>0 and x<>1 and x<>2 f :: Int -> Int f x = case x of 0 -> 1 1 -> 3 2 -> 6 _ -> 0 dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Syntax in Haskell if ... else ... sgn : Z -> Z sgn(x) = -1 if only x<0 sgn(0) = 0 sgn(x) = 1 if only x>0 sgn:: Int -> Int sgn x = if x > 0 then 1 else if x == 0 then 0 else -1 sgn:: Int -> Int sgn x = if x > 0 then 1 else if x == 0 then 0 else -1 dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Syntax in Haskell guards ... sgn : Z -> Z sgn(x) = -1 if only x<0 sgn(0) = 0 sgn(x) = 1 if only x>0 sgn:: Int -> Int sgn x | x < 0 = -1 | x == 0 = 0 | otherwise = 1 sgn:: Int -> Int sgn x | x > 0 = 1 | x == 0 = 0 | x < 0 = -1 sgn:: Int -> Int sgn 0 = 0 sgn x | x > 0 = 1 | x < 0 = -1 dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Syntax in Haskell where bmiTell :: Float -> Float -> String   bmiTell weight height       | bmi <= skinny = ”You're underweight”       | bmi <= normal = ”You're normal”      | bmi <= fat    = ”You're overweight”       | otherwise     = ”You have a problem”       where bmi = weight / height ^ 2             skinny = 18.5             normal = 25.0             fat = 30.0   dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Syntax in Haskell let ... in ... cylinder :: (RealFloat a) => a -> a -> a cylinder r h = let sideArea = 2 * pi * r * h topArea = pi * r ^2 in sideArea + 2 * topArea dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Recursion Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to iteration). The approach can be applied to many types of problems, and recursion is one of the central ideas of computer science. dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Recursion factorial :: Integer-> Integer factorial 0 = 1 factorial n = n * factorial (n-1) mult:: Int -> Int ->Int mult 0 n = 0 mult m n = n + mult (m-1) n dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Recursion fib :: Int -> Int fib 0 = 0 fib 1 = 1 fib n = fib (n – 2) + fib (n - 1) length’ :: [a] -> Integer length’ [] = 0 length’ (_:xs) = 1 + length’ xs dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Recursion maximum' :: (Ord a) => [a] -> a  maximum' [] = error ”list is empty”   maximum' [x] = x   maximum' (x:xs) = max x (maximum' xs) reverse' :: [a] -> [a]   reverse' [] = []   reverse' (x:xs) = reverse' xs ++ [x] dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Recursion zip' :: [a] -> [b] -> [(a,b)]   zip' _ [] = []   zip' [] _ = []   zip' (x:xs) (y:ys) = (x,y):zip' xs ys quicksort :: (Ord a) => [a] -> [a]   quicksort [] = []   quicksort (x:xs) =  smallerSorted ++ [x] ++ biggerSorted where smallerSorted = quicksort [a | a <- xs, a <= x]           biggerSorted = quicksort [a | a <- xs, a > x]      dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Read from keyboard readFromKeyboard = do putStr "Podaj x: " x <- getLine putStr "Podaj y: " y <- getLine putStrLn (show((read x::Float)+(read y::Float)))      show :: Show a => a -> String read :: Read a => String -> a dr Robert Kowalczyk WMiI UŁ

Higher order functions map :: (a -> b) -> [a] -> [b]   map _ [] = []   map f (x:xs) = f x : map f xs       map (+2) [1,2,3,4] -> [3,4,5,6] map (length) [”I”,”love”,”Haskell”] -> [1,4,8]      filter :: (a -> Bool) -> [a] -> [a]   filter _ [] = []   filter p (x:xs)        | p x       = x : filter p xs       | otherwise = filter p xs   filter (>0) [-10..10] -> [1,2,3,4,5,6,7,8,9,10] filter (not.null) [[1,2,3],[],[3,4,5],[2,2],[],[]] -> [[1,2,3],[3,4,5],[2,2]]   dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Lambdas \ variables -> result        calculateOnList x = map (\a -> 2*a + 1) x      calculateOnLists x y = zipWith (\a b -> 2*a + 3*b + 1) x y      dr Robert Kowalczyk WMiI UŁ

Excercises with Teacher Define the function max3 :: Float-> Float -> Float -> Float which returns maximum from any three real numbers. Ex. 2 Define two sequences {a_n} and {b_n} by formula a_{n}=a_{n-1}+b_{n-1}, a_{0}=1 and b_{n}=a_{n-1}*b_{n-1}, b_{0}=1. Then create two lists: - the list of k elements of sequence {a_n}, - the list of k elements of sequence {b_n}. dr Robert Kowalczyk WMiI UŁ

Excercises with Teacher and without  Write the functions: firstFromList :: [a] -> a secondFromList :: [a] -> a lastFromList :: [a] -> a beforelastFromList :: [a] -> a which reads: first, second, last and before last element in any list. Ex. 4 Define the function sumPositiveElementsOnList :: [Int] -> Int which returns sum of all positive integer numbers from this list. dr Robert Kowalczyk WMiI UŁ

Excercises with Teacher’s help Write the function howManyTimes :: a -> [a] -> Int which returns the number of how many times the element is on the list. Ex. 6 isPrimeNumber :: Int -> Bool which returns True or False depending on whether the number given as the argument is the prime number or not the prime number. Then write the function nPrimeNumbers :: Int -> [Int] which returns the first n primes numbers. dr Robert Kowalczyk WMiI UŁ

Excercises without Teacher’s help Define functions toUpperString and toLowerString which will convert each letter in a string to a large (small) one. For example: toUpperString ”Helooo Haskell” -> ”HELOOO HASKELL” toLowerString ”Hello Haskell” -> ”hello haskell” Hint. Import functions toUpper and toLower from the module Data.Char. Ex. 8 Write the program palindrom which reads any string from the keyboard and check (True or False) if this string is a palindrom or not. dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Sites about Haskell      dr Robert Kowalczyk WMiI UŁ

Thank you for your attention  dr Robert Kowalczyk WMiI UŁ