Functional programming Flashcards
(27 cards)
Haskell data types
Int - whole numbers with limited range
Integer- whole numbers with larger range
bool - true false
char- characters
float- fractional numbers
Example haskell function
addone :: Int -> Int ( takes int value and returns int value)
addone n = n+1
h:: Float -> Float
h n = n/1.5
Prebuilt function
sum[1..5] = 15
max 2 7
min 2 7
gcd
Operatiors
&& and
|| or
not = not
Strings
goodbye :: String -> String
goodbye n = “hello”
“hello “++”world””
“hello world”
putStr function gives new line when \n is mentioned
Functions continued
sumtwo :: Int -> Int -> Int
sumtwo m n = m + n
Guards
Used to express various cases
biggest :: Int -> Int -> Int
biggest x y
|x>=y =x
|otherwise = y
nested conditional expression
f:: String -> Int -> Int -> String - > String
f sex height weight haircolor =
if sex == “male” then
if height < 160 then
“you are a bit short”
else if weight < 70 then
“you are thin”
i aint doin allat
Built in list function
Colon
!!
++
length
head
last
tail
init
take
Drop
3 :[3,2] adds element to front of list if its same type
[3,3,2]
!! returns nth element of a list starting at 0
concats two list together
returns length of list
Returns first element
returns last element
returns list without first element
returns list without last element
returns first n elements
removes first n elements
list comprehension
factors :: INT -> [Int]
factors n = [x | x<- [1..n], nmod
x == 0]
addOrderedPairs :: [(Int, Int)] -> [Int]
addOrdPairs xs = [m + n | (m,n) <- xs, m< n]
WildCards
f:: Int -> Int- > Int
f x y
f 0 y = y
f x y = x// f x _ = x this is wildcard…….
zip function
zip[a’,’b’] [1,2] => [(‘a’,1),(‘b’,2)]
sequential pattern matching
F::[Int] -> Int
f(x : (1: xs)) = x // x is head (1:xs) is tail
f(4:(y:xs)) = y
f(x: (y : xs)) = 0
pattern matching vs case construction
firstword :: [String] -> String
firstoword [] = “no first word”
firstword(x:xs) = x
firstword xs
= case xs of
[] -> “no first word”
(x:_) -> x
Recursion over list
length’ :: [Int] -> Int
length’ [] = 0
length’ (_:xs) = 1 + length’ xs
insertion sort using recursion
insert element in correct spot?
ins :: int -> [Int] -> [Int]
ins x [] = [x]
ins x(y:ys)
|x <=y = x:(y:ys)
|otherwise = y:(ins x ys)
Isort :: [int] -> [Int]
isort [] = []
isort(x:xs) = ins x(isort xs)
qsort ….
qsort :: [Int] -> [Int]
qsort [] = []
qsort(x:xs)
=qsort[y | y<- xs, y<=x] ++ [x] ++ qsort [y | y<-xs, y>x]
local definitions
f x y
|g x > h y = z
|otherwise = g y
where
z = 2*y
g,h :: Int -> Int
g y = 3 * y * x + 2
h y = 2 * y + 1
min prebuilt function
returns min of two INTEGERS
polymorphic functions
type definiton given using type variables instead of actual type
e.g prebuilt function length can work on list of any type
length :: [a] -> Int
polymorpihc with tuples
first :: (a, b, c) -> a
first (x, y, z) = x
if we have (a,a,a) -> a
then if we use this in another definition with a type definiton
all variables must be instantiated with same type
High order function- takes function as arguement or returns a function as result
map function
map :: (a -> b)-> [a] -> [b] – applies function to every element of list
map f [ ] = [ ]
map f (x:xs) = f x : map f xs
map (+1) [1,2] => [2,3]
filter function
filter :: (a -> Bool) ->[a] -> [a]
filter p xs = [x | x <- xs, p x]
filter even [1..10] => [2,4,6,8]
takewhile function
takeWhile :: (a -> Bool) -> [a] -> [a]
takeWhile p [] = []
takeWhile p (x:xs)
| p x = x : takeWhile p xs
| otherwise = []
takeWhile(<3) [1,2,3,4,] = > [1,2]