workshop1 Flashcards
What happens here?
> sentence = “Sentences can go “ ++ onAndOn
onAndOn = “on and “ ++ onAndOn
take 65 sentence
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
What happens here?
> sentences = “Sentences can go on” : map (++ “ and on”) sentences
take 10 sentences
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
Can you give your own definitions of map
map::(a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = (f x) : map f xs
Can you give your own definitions of take
take:: Int -> [a] -> [a]
take _ [] = []
take n (x:xs) = x : take (n-1) xs
give some examples of the domain of natural numbers
being even, odd, prime, 3-fold, etc
make a threefold property in haskell
> threefold :: Integer -> Bool
> threefold n = rem n 3 == 0
Why does threefold express a property of integer numbers?
???
What is the most general specification of a property?
???
make a lazy list of natural numbers that are threefolds
> threefolds = filter threefold [0..]
Can you give your own definition of filter?
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 to say there is no largest natural number in haskell
> 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 to say there is a smallest natural number
> nats = [0..]query2 = any (\ n -> all (\ m -> n <= m) nats) nats
or
exist = flip any
query2’ = exist nats (\ n -> forall nats (\ m -> n <= m)
Give your own definition of all
myall::(a -> Bool) -> [a] -> Bool
myall _ [] = True
myall f (x:xs) = f x && all f xs
make a quickCheck property for myall
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
what general recursion pattern does foldr solve?
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.
update myall to use foldr
myall’ p = foldr (\ x b -> p x && b) True
define foldr
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr f z [] = z
foldr f z (x:xs) = f x (foldr f z xs)
Definition of being a prime number with a formula of predicate logic
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.
implement primes
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]
Are you able to state the types and provide your own definitions of all
all::(a -> Bool) -> [a] -> Bool all f [] = True all f (x:xs) | f x = all f xs | otherwise = False
Are you able to state the types and provide your own definitions of not
not::Bool -> Bool
not True = False
not _ = True
Are you able to state the types and provide your own definitions of any
any::(a -> Bool) -> [a] -> Bool any f [] = False any f (x:xs) | f x = True | otherwise = any f xs
Are you able to state the types and provide your own definitions of &&
(&&)::Bool -> Bool -> Bool
True && True= True
_ && _ = False
Are you able to state the types and provide your own definitions of ||
(||)::Bool -> Bool -> Bool
False || False = False
_ || _ = True