programming paradigms and languagesmath.uni.lodz.pl › ~kowalcr › plockparad › haskell0.pdf ·...
TRANSCRIPT
dr Robert Kowalczyk WMiI UŁ
Programming Paradigms and Languages
Introduction to Haskell
dr Robert Kowalczyk WMiI UŁ 2
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.
Functional programming
dr Robert Kowalczyk WMiI UŁ 3
Declarative vs. imperative programming
dr Robert Kowalczyk WMiI UŁ 4
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Ł 5
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.
Haskell
dr Robert Kowalczyk WMiI UŁ 6
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.
Haskell compiler
dr Robert Kowalczyk WMiI UŁ 7
Haskell platform
dr Robert Kowalczyk WMiI UŁ 8
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
First program/script in Haskell
dr Robert Kowalczyk WMiI UŁ 9
:? 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
Help in Haskell
dr Robert Kowalczyk WMiI UŁ 10
Prelude>8+4
12
Prelude>34-35
-1
Prelude>3.5*5676
19866
Prelude>23/6
3.833333333333335
Prelude>2^4
16
Haskell as a calculator
dr Robert Kowalczyk WMiI UŁ 11
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
Functions in Haskell (embedded)
dr Robert Kowalczyk WMiI UŁ 12
-- 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
Comments and a simple function
dr Robert Kowalczyk WMiI UŁ 13
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
Variables
dr Robert Kowalczyk WMiI UŁ 14
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.
Excercise 1
dr Robert Kowalczyk WMiI UŁ 15
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
Checking the type of objects
dr Robert Kowalczyk WMiI UŁ 16
Int - [-2^29 .. 2^29-1]
Integer – very big integer numbers
Float – real numbers
Double – real numbers
Char - (Unicode) character
Bool – logical type
Simple types in Haskell
dr Robert Kowalczyk WMiI UŁ 17
Prelude>True && False
False
Prelude>True && (1<4)
True
Prelude>False || False
False
Prelude>2 /= 3
True
Prelude>not True
False
Logical operators
dr Robert Kowalczyk WMiI UŁ 18
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]
Lists
dr Robert Kowalczyk WMiI UŁ 19
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]]
Lists and lazy evaluation
dr Robert Kowalczyk WMiI UŁ 20
(!!) :: [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
Functions on lists
dr Robert Kowalczyk WMiI UŁ 21
product :: (Num a) => [a] -> a
product [-2,-1,0,1,2]
0
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]
Functions on lists
dr Robert Kowalczyk WMiI UŁ 22
[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]
The comprehension list
dr Robert Kowalczyk WMiI UŁ 23
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
Excercise 2
dr Robert Kowalczyk WMiI UŁ 24
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
Homework
dr Robert Kowalczyk WMiI UŁ 25
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
Haskell - complex excercise
dr Robert Kowalczyk WMiI UŁ 26
Any Quetions?