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Ł