infinite lists Flashcards

1
Q

Fibonacci of n

fib::Int->Int

A

fib::Int->Int
fib 0 = 0
fib 1 = 1
fib n = (fib n-2) + (fib n-1)

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

quicksort

qsort::Ord a => [a] -> [a]

A
qsort::Ord a => [a] -> [a]
qsort [] = []
qsort (x:xs) = qsort smaller ++ [x] ++ qsort larger
where 
smaller = [a | a ← xs, a <= x]
larger = [b | b ← xs, b > x]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

reverse

reverse::[a] -> [a]

A

reverse::[a] -> [a]
reverse [] = []
reverse (x:xs) = reverse xs ++ [x]

or

reverse = foldr (\x xs -> xs ++ [x]) []

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

find (dictonary)

find::Eq a => a -> [(a,b)] -> [b]

A

find::Eq a => a -> [(a,b)] -> [b]

find k t = [v | (k’,v) ← t, k’ == k]

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

factors (all factors of a number from 1 to n)

factors:: Int -> [Int]

A

factors:: Int -> [Int]

factors n = [x | x ← [1..n], n mod x == 0]

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

rotate (move n letters from front to back)

rotate:: Int -> String -> String

A

rotate:: Int -> String -> String

rotate n xs = drop n xs ++ take n xs

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

factoral

fac::Int -> Int

A
fac::Int -> Int
fac n = product [1..n]
or
fac 0 = 1
fac n  = n * fac (n-1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

(*) (defined recursively with +)

(*):: Int -> Int -> Int

A

(*):: Int -> Int -> Int
m * 0 = 0
m * n = m + (m * (n-1))

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

product (multiply all items in list)

product::Num a => [a] -> a

A

product::Num a => [a] -> a
product [] = []
product (n:ns) = n * product ns

or

product = foldr (*) 1

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

length (of list)

length::[a] -> Int

A

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

or

length = foldr (_ n -> 1 + n) 0

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

(++) append lists

(++):: [a] -> [a] -> [a]

A

(++):: [a] -> [a] -> [a]
[] ++ ys = ys
(x:xs) ++ ys = x : (xs ++ ys)

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

insert (into ordered list)

insert:: Ord a=> a -> [a] -> [a]

A
insert:: Ord a=> a -> [a] -> [a]
insert x [] = [x]
insert x (y:ys) 
| x <= y        = x : y : ys
| otherwise = y : insert x ys
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
insert sort (using insert::Ord=> a -> [a] -> [a])
isort:: Ord a => [a] -> [a]
A

isort:: Ord a => [a] -> [a]
isort [] = []
isort (x:xs) = insert x (isort xs)

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

zip

zip::[a] -> [b] -> [(a,b)]

A

zip::[a] -> [b] -> [(a,b)]
zip [] _ = []
zip _ [] = []
zip (x:xs) (y:ys) = (x,y) : zip xs ys

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

drop (first n of a list)

drop:: Int -> [a] -> [a]

A

drop:: Int -> [a] -> [a]
drop 0 _ = []
drop _ [] = []
drop n (_:xs) = drop (n-1) xs

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

map (functions over a list)

map::(a -> b) -> [a] -> [b]

A
map::(a -> b) -> [a] -> [b]
map _ [] = []
map f (x:xs) = (f x) : map f xs

or

map f xs = [f x | x ← xs]

17
Q

filter (from a list based on predicate)

filter::(a -> Bool) -> [a] -> [a]

A
filter::(a -> Bool) -> [a] -> [a]
filter [] = []
filter p (x:xs) 
| p x = p : filter p xs
| otherwise = filter p xs

or

filter p xs = [x | x ← xs, p x]

18
Q

foldr - fold a function to right across list

foldr::(a -> b -> b) -> b -> [a] -> b

A

foldr::(a -> b -> b) -> b -> [a] -> b
foldr f v [] = v
foldr f v (x:xs) = f x (foldr f v xs)

19
Q

sum (of list)

sum::Num a => [a] -> a

A

sum::Num a => [a] -> a
sum [] []
sum (n:ns) = n + sum ns

or

sum = foldr (+) 0

20
Q

or (on list of Bool)

or::[Bool] -> Bool

A

or::[Bool] -> Bool
or [] = False
or (x:xs) = x || or xs

or

or = foldr (||) False

21
Q

and (on list of bool)

and::[Bool] -> Bool

A

and::[Bool] -> Bool
and [] = True
and (x:xs) = x && and xs

or

and = foldr (&&) True

22
Q

(.) composition

.)::(b->c) -> (a->b) -> (a -> c

A

(.)::(b->c) -> (a->b) -> (a -> c)

f . g = \x -> f (g x)

23
Q

id (the identity function)

id::a -> a

A

id::a -> a

id = \x -> x

24
Q

iterate (apply function to result of previous function)

iterate::(a -> a) -> a -> [a]

A

iterate::(a -> a) -> a -> [a]

iterate f x = x : iterate f (f x)

25
Q

primes (inefficient)

primes :: Int -> [Int]

A
primes :: Int -> [Int] 
primes n = [x | x ← [2..n], prime x]
where
prime  n = factors n == [1,n]
factors n = [x | x ← [1..n], n ‘mod‘ x == 0]