workshop1 Flashcards

1
Q

What happens here?

> sentence = “Sentences can go “ ++ onAndOn
onAndOn = “on and “ ++ onAndOn

take 65 sentence

A

onAndOn recurses on itself, repeating “on and “ infintaly

sentence adds onAndOn to the end of a string

take 65 sentence takes the first 65 characters of the string

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

What happens here?

> sentences = “Sentences can go on” : map (++ “ and on”) sentences

take 10 sentences

A

this is creating an infinite list that is growing by concatinating itself to a recursion of itseld

take 10 sentences will have a list of 10 strings begining with “Sentences can go on” and adding another “and on.

this works because each call into the recursion is mapping a concatination of “and on” to the result of every one that was previously processed

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

Can you give your own definitions of map

A
map::(a -> b) -> [a] -> [b]
map _ [] = []
map f (x:xs) = (f x) : map f xs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can you give your own definitions of take

A

take:: Int -> [a] -> [a]
take _ [] = []
take n (x:xs) = x : take (n-1) xs

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

give some examples of the domain of natural numbers

A

being even, odd, prime, 3-fold, etc

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

make a threefold property in haskell

A

> threefold :: Integer -> Bool

> threefold n = rem n 3 == 0

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

Why does threefold express a property of integer numbers?

A

???

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

What is the most general specification of a property?

A

???

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

make a lazy list of natural numbers that are threefolds

A

> threefolds = filter threefold [0..]

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

Can you give your own definition of filter?

A

filter::(Eq a) => (a -> Bool) -> [a] -> [a]
filter _ [] = []
filter f (x:xs) = if f x then x : filter f xs else filter f xs

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

how to say there is no largest natural number in haskell

A

> nats = [0..]query1 = all (\ n -> any (\ m -> n < m) nats) nats
or
forall = flip all
query1’ = forall nats (\ n -> exist nats (\ m -> n < m))

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

how to say there is a smallest natural number

A

> nats = [0..]query2 = any (\ n -> all (\ m -> n <= m) nats) nats
or
exist = flip any
query2’ = exist nats (\ n -> forall nats (\ m -> n <= m)

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

Give your own definition of all

A

myall::(a -> Bool) -> [a] -> Bool
myall _ [] = True
myall f (x:xs) = f x && all f xs

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

make a quickCheck property for myall

A

this will not work:
\ p xs -> all p xs == myall p xs
QuickCheck is designed to display counterexamples, but there is no general way to display functions, and p is a function.

general conversion from lists to properties:
> list2p :: Eq a => [a] -> a -> Bool
> list2p = flip elem

prop_myall:: [Int] -> [Int] -> Bool
prop_myall = (\ ys xs -> let p = list2p ys) in all p xs == myall p xs

> quickCheck myallTest

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

what general recursion pattern does foldr solve?

A

you have to specify a value for the base case,

you have to give a definition for the recursive case (x:xs) in terms of the first element of the list x and a recursive call for the tail of the list xs.

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

update myall to use foldr

A

myall’ p = foldr (\ x b -> p x && b) True

17
Q

define foldr

A

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

18
Q

Definition of being a prime number with a formula of predicate logic

A

P(n):≡n∈N∧n>1∧∀d∈N(1 < d < n→¬D(d,n)).

n is prime :≡ n is a natural number and n>1 and for all natural numbers d with 1< d< n it holds that d does not divide n.

19
Q

implement primes

A

first first thing we need is the divide relation
> divide :: Integer -> Integer -> Bool
> divide n m = rem m n == 0

next the relations:
> isPrime :: Integer -> Bool
> isPrime n = n > 1 && all (\ d -> not (divide d n)) [2..n-1]

20
Q

Are you able to state the types and provide your own definitions of all

A
all::(a -> Bool) -> [a] -> Bool
all f [] = True
all f (x:xs) 
    | f x = all f xs
    | otherwise = False
21
Q

Are you able to state the types and provide your own definitions of not

A

not::Bool -> Bool
not True = False
not _ = True

22
Q

Are you able to state the types and provide your own definitions of any

A
any::(a -> Bool)  -> [a] -> Bool
any f [] = False
any f (x:xs) 
    | f x = True
    | otherwise = any f xs
23
Q

Are you able to state the types and provide your own definitions of &&

A

(&&)::Bool -> Bool -> Bool
True && True= True
_ && _ = False

24
Q

Are you able to state the types and provide your own definitions of ||

A

(||)::Bool -> Bool -> Bool
False || False = False
_ || _ = True

25
Q

slightly more efficient version of isPrime

A

> isPrime’ :: Integer -> Bool
isPrime’ n = all (\ x -> rem n x /= 0) xs
where xs = takeWhile (\ y -> y^2 <= n) [2..]

26
Q

tests only for divisibility by primes

A

> prime :: Integer -> Bool
prime n = n > 1 && all (\ x -> rem n x /= 0) xs
where xs = takeWhile (\ y -> y^2 <= n) primesprimes :: [Integer]
primes = 2 : filter prime [3..]

(has to start explicity at 2 because …?)

27
Q

sieve of Eratosthenes

A

> sieve :: [Integer] -> [Integer]
sieve (n:ns) = n : sieve (filter (\m -> rem m n /= 0) ns)eprimes = sieve [2..]

or

primes :: [Int]
primes = sieve [2..]

sieve :: [Int] -> [Int]
sieve (p:xs) = p : sieve [x | x

28
Q

find the smallest natural number that matches an arbitrary property

A

nats = [0..]

least :: (Integer -> Bool) -> Integer
least p = head (filter p nats)

least1 p = lst p 0
where lst p n = if p n then n else lst p (n+1)

29
Q

A prime pair is a pair (p,p+2)(p,p+2) with the property that both pp and p+2p+2 are primes. The first prime pair is (3,5). Implement a function for generating prime pairs, and use this to find the first 100 prime pairs.

A

> dif2 :: [Integer] -> [(Integer,Integer)]
dif2 (p:q:rs) = if p + 2 == q then (p,q) : dif2 (q:rs)
else dif2 (q:rs)primePairs = dif2 primes

30
Q

A prime triple is a triple (n,m,k)(n,m,k) such that n

A

> dif6 :: [Integer] -> [(Integer,Integer,Integer)]
dif6 (p:q:r:ss) = if p + 6 == r then (p,q,r) : dif6 (q:r:ss)
else dif6 (q:r:ss)primeTriples = dif6 primessol = take 100 primeTriples

31
Q

Implement a function nextPrime with the property that nextPrime n returns n when n is prime, and the next prime after n otherwise.

A

> nextPrime :: Integer -> Integer

> nextPrime n = if prime n then n else nextPrime (n+1)

32
Q

A Mersenne number is a natural number of the form 2^p−1 where pp is a prime number. A Mersenne prime is a Mersenne number that is itself prime. Write a function for generating Mersenne primes. How far do you get? Note: only 48 Mersenne primes are known

A

> mersenne :: [Integer]

> mersenne = [ p | p

33
Q

A Pythagorean triple is a triple of natural numbers (x,y,z) with the property that x^2+y^2=z^2. The smallest example is (3,4,5). Implement a Haskell function that generates Pythagorean triples. Are there Pythagorean triples (x,y,z) with x=y? If your answer is “Yes”, give the smallest one. If your answer is “No”, explain why this is impossible.

A

> pythTriples :: [(Integer,Integer,Integer)]
pythTriples = filter (\ (x,y,z) -> x^2 + y^2 == z^2)
[ (x,y,z) | z

34
Q

Prove by induction that it holds for all natural numbers nn that
1+2+⋯+n=(n(n+1))/2.

A

Base case: for n=0 the property holds.

Induction step.

Induction hypothesis: 1+2+⋯+n=(n(n+1))/2.

To be proved: 1+2+⋯+n+(n+1)=((n+1)(n+2))/2.

Note: ((n+1)(n+2))/2 is the result of substituting n+1 for n in (n(n+1))/2.

Proof (of the induction step):
1+2+⋯+n+(n+1)=ih
=(n(n+1))/2+(n+1)
=(n(n+1))/2+2(n+1)/2
=(n+2)(n+1)/2
=(n+1)(n+2)/2
Note: step =ih uses the induction hypothesis.

35
Q

Prove by induction that it holds for all natural numbers n that
1^2+2^2+⋯+n^2=n(n+1)(2n+1)/6.

A

To be proved:
for all n∈N:1^2+2^2+⋯+n^2=n(n+1)(2n+1)/6

Proof by induction.
Base case: for n=0 the property holds.

Induction step.

Induction hypothesis:
1^2+2^2+⋯+n^2=n(n+1)(2n+1)/6

To be proved:
1+2^2+⋯+n^2+(n+1)^2=(n+1)(n+2)(2n+3)/6

Proof:

1^2+2^2+⋯+n^2+(n+1)^2=ih
=n(n+1)(2n+1)/6+(n+1)^2
=n(n+1)(2n+1)/6+(6(n+1)^2)/6
=(2n^2+n)(n+1)/6+((6n+6)(n+1))/6
=(2n^2+7n+6)(n+1)/6
=(n+2)(2n+3)(n+1)/6
=(n+1)(n+2)(2n+3)/6

36
Q

Prove by induction that it holds for all natural numbers n that
1^3+2^3+⋯+n^3=((n(n+1))/2)^2.

A

Proof by induction.
Base case: for n=0 the property holds.

Induction step.
Induction hypothesis:
1^3+2^3+⋯+n^3=((n(n+1))/2)^2

To be proved:
1^3+2^3+⋯+n^3+(n+1)^3=((n+1)(n+2)/2)^2

Proof:
1^3+2^3+⋯+n^3+(n+1)^3=ih
=((n(n+1))/2)^2(n+1)^3
=(n^2(n+1)^2)/4+(4(n+1)^3)/4
=(n^2(n+1)^2)/4+((4n+4)(n+1)^2)/4
=((n+1)^2(n^2+4n+4))/4
=((n+1)^2(n+2)^2)/4
=((n+1)(n+2)/2)^2

37
Q

Prove by induction that if A is a finite set with |A|=n, then |P(A)|=2^n

  • P(A) = powerset of A
A

The empty set has 0 members and P(∅)={∅} has 1(=2^0) member, so in the base case the assertion holds.

Suppose that
if |A|=n, then |P(A)|=2^n.
Now let A′ be such that A⊆A′ and |A′|=n+1.

Then there is exactly one object x with x∉A and A′=A∪{x}.
Now consider the subsets B′ of A′.
How many of those are there? The subsets B′ of A′ are of two kinds:
those with x∈B′ and those with x∉B′.
In fact, for any subset B of A, B⊆A′ and B∪{x}⊆A′, and B≠B∪{x}.
Thus A′ has twice as many subsets as A and we have, by induction hypothesis, that |P(A′)|=2⋅2^n=2^(n+1)

38
Q

A permutation of a list is a reordering of the members of a list. Here is a Haskell implementation:

> perms :: [a] ->[[a]]
perms [] = [[]]
perms (x:xs) = concat (map (insrt x) (perms xs)) where
insrt x [] = [[x]]
insrt x (y:ys) = (x:y:ys) : map (y:) (insrt x ys)

Find a formula (closed form) for the number of permutations of a list of n distinct objects, and prove your guess by induction.

A

There are n! permutations for a list of n distinct objects.

The empty list has a single permutation, and indeed, 0!=1 (by the convention for an empty product).

Suppose a list of n distinct objects has n! permutations.

Then there are n+1 ways to insert a new object into one of these.
Together this gives (n+1)×n! = (n+1)! permutations of a list of n+1 distinct elements.