Typing and Programming with Functions Flashcards
what is a type?
a collection of related values
what is ⊥?
“bottom” - non-terminating stuff
how is type defined in Haskell?
e :: t means e has type t
what is a tuple?
a sequence of values of different types, surrounded by brackets ().
what does an apostrophe mean in Haskell?
it signifies a curried function
what is a curried function? (give an example)
a function that takes arguments one at a time
add’ :: Int -> (Int -> Int)
add’ x y = x + y
what are polymorphic functions?
functions that’s types contain multiple variable types, e.g. length :: [a] -> Int
What does it mean for a function to be overloaded?
a polymorphic funtion is overloaded if its type contains at least 1 class constraint, e.g. sum :: Num a => [a] -> a
give an example of a conditional expression in Haskell
signum :: Int -> Int
signum n = if n < 0 then -1 else
if n==0 then 0 else 1
give an example of a guarded equation on Haskell
signum n | n < 0 = -1
| n == 0 = 0
| otherwise = 1
what does _ signify?
a wildcard pattern that matches any argument value
use _ to define True or False with ands
True && b = b
False && _ = False
how are patterns matched?
in order
how is disjunction symbolised in Haskell?
||
use || to define True or False
False || b = b
True || _ = True
what is special about list comprehension?
its unique to functional programming
give an example of multiple generator comprehension
[(x, y) | x
give an example of dependant generator comprehension
[(x, y) | x
write a dependent generator list concatenator for 3 lists.
concat :: [[a]] -> a
concat xss = [x | xs
give an example of using a guard in Haskell
[x | x
write a function to find factors of a numer
factors :: Int -> [Int]
factors n = [x | x
use factors to write a prime function to determine whether a number is a prime
prime :: Int -> Bool
prime n = factors n == [1, n]
what does zip do?
maps 2 lists to a list of pairs of their corresponding elements
what is the type of zip?
[a] -> [b] -> [(a, b)]