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.