Programming Paradigms and Languages Introduction to Haskell dr Robert Kowalczyk WMiI UŁ
Functional programming In functional programming (special type of declarative programming), programs are executed by evaluating expressions, in contrast with imperative programming where programs are composed of many statements which change global state when executed. dr Robert Kowalczyk WMiI UŁ 2
Declarative vs. imperative programming dr Robert Kowalczyk WMiI UŁ 3
Functional programming languages Lisp - a family of programming languages: Common Lisp, Scheme i Clojure Ocaml ML Haskell Erlang Scala Python dr Robert Kowalczyk WMiI UŁ 4
Haskell Haskell is a modern general-purpose functional language created in order to combine all the advantages of functional programming in one elegant, powerful and freely available programming language. Haskell is unique in two ways. First, it is a pure functional programming language. If you have a function and you call it twice in two different places with the same arguments then it will return exactly the same value both times. Second, Haskell provides a very modern type system which incorporates features like typeclasses and generalized algebraic data types. Advanced type system helps catch mistakes, both silly and profound. dr Robert Kowalczyk WMiI UŁ 5
Haskell compiler To write Haskell programs, you need a program called a Haskell compiler. A compiler is a program that takes code written in Haskell and translates it into machine code, a more primitive language that the computer understands. Using the above analogy, the compiler is the oven that bakes your batter (code) into a cookie (executable file), and it's difficult to get the recipe from an executable once it's compiled. dr Robert Kowalczyk WMiI UŁ 6
Haskell platform dr Robert Kowalczyk WMiI UŁ 7
First program/script in Haskell After you have installed the Haskell Platform, it's now time to write your first Haskell code. You can: write script haskel.hs with code: main = do putstrln "Hello World" and compile it ghc -o first haskel.hs run ghci (interactive console) and write putstrln "Hello World" or load script :l haskel.hs dr Robert Kowalczyk WMiI UŁ 8
Help in Haskell :? or :help display list of commands :cd change directory :! run the shell command :! cd chamge local directory :! md create local directory :l or :load load module dr Robert Kowalczyk WMiI UŁ 9
Haskell as a calculator Prelude>8+4 12 Prelude>34-35 -1 Prelude>3.5*5676 19866 Prelude>23/6 3.833333333333335 Prelude>2^4 16 dr Robert Kowalczyk WMiI UŁ 10
Functions in Haskell (embedded) abs x => x sin x => sin(x) cos x => cos(x) tan x => tan(x) floor x => [x] div => / mod => % exp x => exp(x) log x => ln(x) max a b => maximum(a,b) min a b => minimum(a,b) sqrt x => x^(1/2) pi => 3.141592653589793 dr Robert Kowalczyk WMiI UŁ 11
Comments and a simple function -- one line comment {- -} block comment block comment Prelude>let square x = x * x Prelude>square 3 9 Prelude>square (-3) 9 Write script first.hs square x = x * x then load script :l first.hs and run function square 4 dr Robert Kowalczyk WMiI UŁ 12
Variables Prelude>let r=4 Prelude>r 4 Prelude>let area = pi*r*r error why? Prelude>let r=4.0 Prelude>area 50.26548245743669 Prelude>let r=5.0 Prelude>area 50.26548245743669 why? Prelude>let area = pi*r*r Prelude>area 78.53981633974483 dr Robert Kowalczyk WMiI UŁ 13
Excercise 1 Define the function writes x which write string x on the screen. For example, if you run writes "Robert" you should see on the screen: "Hello Robert!" Hint: If you want to add two strings you can use ++ operator. dr Robert Kowalczyk WMiI UŁ 14
Checking the type of objects To check the type of object we use the command :t or :type Prelude>:t "dog" "dog"::[char] Prelude>:t 'e' 'e'::char Prelude>let i=5 Prelude>:t i i::integer Prelude>lez z=5.6 Prelude>:t z z::double dr Robert Kowalczyk WMiI UŁ 15
Simple types in Haskell Int - [-2^29.. 2^29-1] Integer very big integer numbers Float real numbers Double real numbers Char - (Unicode) character Bool logical type dr Robert Kowalczyk WMiI UŁ 16
Logical operators Prelude>True && False False Prelude>True && (1<4) True Prelude>False False False Prelude>2 /= 3 True Prelude>not True False dr Robert Kowalczyk WMiI UŁ 17
Lists Prelude> let names = ["Jane", "George", "Kate"] Prelude> let numbers = [-2,-1,0,1,2] Prelude> -3 : numbers [-3,-2,-1,0,1,2] Prelude> numbers [-2,-1,0,1,2] Prelude> head numbers -2 Prelude> tail numbers [-1,0,1,2] dr Robert Kowalczyk WMiI UŁ 18
Lists and lazy evaluation Prelude>"Haskell" "Haskell" Prelude>'H':'a':'s':'k':'e':'l':'l':[] "Haskell" Prelude>[1,2..10] [1,2,3,4,5,6,7,8,9,10] Prelude>[5,3..(-1)] [5,3,1,-1] Prelude>[1,2..] 1,2,3,4,. let pitagoras = [(a, b, c) c <- [1..], b <- [1.. c], a <- [1.. b], a^2 + b^2 == c^2] Prelude> [[1,2,3],[2,3,4],[3,4,5]] [[1,2,3],[2,3,4],[3,4,5]] dr Robert Kowalczyk WMiI UŁ 19
Functions on lists (!!) :: [a] -> Int -> a (!!) [-2,-1,0,1,2] 1-1 length :: [a] -> Int length [-2,-1,0,1,2] 5 (++) :: [a] -> [a] -> [a] (++) [1,2,3] [4,5,6] [1,2,3,4,5,6] drop: Int -> [a] -> [a] drop 3 [-2,-1,0,1,2] [1,2] sum :: (Num a) => [a] -> a sum [-2,-1,0,1,2] 0 dr Robert Kowalczyk WMiI UŁ 20
product :: (Num a) => [a] -> a product [-2,-1,0,1,2] 0 Functions on lists map :: (a->b) -> [a] -> [b] map (+2) [-2,-1,0,1,2] [0,1,2,3,4] filter :: (a -> Bool) -> [a] -> [a] filter (>0) [-2,-1,0,1,2] [1,2] null :: [a] -> Bool null [] true take :: Int -> [a] -> [a] take 2 [-2,-1,0,1,2] [-2,-1] dr Robert Kowalczyk WMiI UŁ 21
The comprehension list [expression(x) x <- list, condition(x)] Prelude> [x^2 x <- [1..10], even x] [4,16,36,64,100] Prelude> l=[a+b a<-[1..5], b<-[-5..(-1)]] [-4,-3,-2,-1,0,-3,-2,-1,0,1,-2,-1,0,1,2,-1,0,1,2,3,0,1,2,3,4] dr Robert Kowalczyk WMiI UŁ 22
Excercise 2 Using the list comprehension write the function unitaryn x which will build nxn unitary matrix. For example, if you run unitaryn 5 you should see on the screen: [[1,0,0,0,0],[0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,0],[0,0,0,0,1]] Hint: use if command: if condition then instruction1 else instruction2 dr Robert Kowalczyk WMiI UŁ 23
Homework Create a function listi x that returns list of integers numbers [1..x]. Then, create a function sumsqrtlist (listi x) that returns the sum of square roots of the list listi x. For example, if you run sumsqrtlist (listi 5) you should see on the screen: 8.382332347441762 dr Robert Kowalczyk WMiI UŁ 24
Haskell - complex excercise Type annotation (optional) factorial :: Integer -> Integer Using recursion factorial 0 = 1 factorial n n > 0 = n * factorial (n - 1) Using recursion but written without pattern matching factorial n = if n > 0 then n * factorial (n-1) else 1 Using a list factorial n = product [1..n] Using fold (implements product) factorial n = foldl1 (*) [1..n] Point-free style factorial = foldr (*) 1. enumfromto 1 dr Robert Kowalczyk WMiI UŁ 25
Any Quetions? dr Robert Kowalczyk WMiI UŁ 26