Functional programming Flashcards

(27 cards)

1
Q

Haskell data types

A

Int - whole numbers with limited range
Integer- whole numbers with larger range
bool - true false
char- characters
float- fractional numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Example haskell function

A

addone :: Int -> Int ( takes int value and returns int value)
addone n = n+1

h:: Float -> Float
h n = n/1.5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Prebuilt function

A

sum[1..5] = 15
max 2 7
min 2 7
gcd

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Operatiors

A

&& and
|| or
not = not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Strings

A

goodbye :: String -> String
goodbye n = “hello”

“hello “++”world””
“hello world”

putStr function gives new line when \n is mentioned

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Functions continued

A

sumtwo :: Int -> Int -> Int
sumtwo m n = m + n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Guards

A

Used to express various cases

biggest :: Int -> Int -> Int
biggest x y
|x>=y =x
|otherwise = y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

nested conditional expression

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Built in list function
Colon

!!

++

length

head

last

tail

init

take

Drop

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

list comprehension

A

factors :: INT -> [Int]
factors n = [x | x<- [1..n], nmodx == 0]

addOrderedPairs :: [(Int, Int)] -> [Int]
addOrdPairs xs = [m + n | (m,n) <- xs, m< n]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

WildCards

A

f:: Int -> Int- > Int
f x y
f 0 y = y
f x y = x// f x _ = x this is wildcard…….

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

zip function

A

zip[a’,’b’] [1,2] => [(‘a’,1),(‘b’,2)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

sequential pattern matching

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

pattern matching vs case construction

A

firstword :: [String] -> String
firstoword [] = “no first word”
firstword(x:xs) = x

firstword xs
= case xs of
[] -> “no first word”
(x:_) -> x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Recursion over list

A

length’ :: [Int] -> Int
length’ [] = 0
length’ (_:xs) = 1 + length’ xs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

insertion sort using recursion

A

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)

17
Q

qsort ….

A

qsort :: [Int] -> [Int]
qsort [] = []
qsort(x:xs)
=qsort[y | y<- xs, y<=x] ++ [x] ++ qsort [y | y<-xs, y>x]

18
Q

local definitions

A

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

19
Q

min prebuilt function

A

returns min of two INTEGERS

20
Q

polymorphic functions

A

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

21
Q

polymorpihc with tuples

A

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

22
Q

High order function- takes function as arguement or returns a function as result
map function

A

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]

23
Q

filter function

A

filter :: (a -> Bool) ->[a] -> [a]
filter p xs = [x | x <- xs, p x]
filter even [1..10] => [2,4,6,8]

24
Q

takewhile function

A

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]

25
dropwhile
removes elements while predicate holds dropwhile (<3)][,2,3,4] => [3,4]
26
ZipWidth
takes function and 2 lists as params and joins the two list by applying function between corresponding elements zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] zipWith _ [ ] _ = [ ] zipWith _ _ [ ] = [ ] zipWith f (x:xs) (y:ys) = f x y : zipWith f xs ys zipwith (+)[1,2][4,5]=> [5,7]
27