Sample exam 2017 Q1 Flashcards
Write a short sentence or two about the difference between:
(a) ghc and ghci
GHC is the Glasgow Haskell Compiler. GHCi is GHC’s interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.
Write a short sentence or two about the difference between:
(b) Int and Integer
(b) Integer: Value depends on the amount of memory on your system. Int is a machine integer, usually between -2^29 to (2^29) -1
Alternate from The Haskell Wikibook:
Haskell has two types for integer numbers: Int and Integer.
“Integer” is an arbitrary precision type: it will hold any number no matter how big, up to the limit of your machine’s memory. That is why “factorial 1000” gives you the right answer. This means you never have arithmetic overflows. On the other hand it also means your arithmetic is relatively slow.
“Int” is the more common 32 or 64 bit integer. Implementations vary, although it is guaranteed to be at least 30 bits.
Write a short sentence or two about the difference between:
(c) + and ++
(c) ++ is the concatenation operator for lists whereas + is the addition operator for numeric types
Write a short sentence or two about the difference between:
(d) -1 and (-)1
-1 is the integer negative one,
(-) produces a partially applied function that takes one argument.
Usecase:
> functionSubtractOne = (-)1
> functionSubtractOne 5
-4
Can also get a type of that expression:
> :t (-)1
(-)1 :: Num a => a -> a
Show less
Write a short sentence or two about the difference between:
(e) \ and /
(e) \ can be used to separate multi line strings, or to begin lambda functions (\x y -> x + y)
/ is the division operator.
Write a short sentence or two about the difference between:
(f) a function and a functor
(f) A function is an evaluation of some arguments to a new value. They can be given type signatures in order to fix the input and output types. A functor is a typeclass of items that can be mapped over. This means members of the type class must define the fmap function. fmap :: Functor f => (a -> b) -> f a -> f b Actually, the list type is part of the functor typeclass and defines it's fmap as map. map :: (a -> b) -> [a] -> [b]
Write a short sentence or two about the difference between:
g) [1,2] and (1,2
(g) [1,2] is a list. Can be appended to and can be arbitrary length.
(1,2) is a tuple. Can only have two elements.
Write a short sentence or two about the difference between:
(h) && and and
(h) && is boolean AND which ands together two boolean values.
and is the conjunction of a boolean list (or indeed, any foldable type)
Write a short sentence or two about the difference between:
(i) True and Just True
(i) True is True :: Bool, Just True is True wrapped in the Maybe monad.
Write a short sentence or two about the difference between:
(j) foldl and foldr
(j) foldr and foldl
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
foldr f acc [] = acc
foldr f acc (x:xs) = f x (foldr f acc xs)
foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b
foldl f acc [] = acc
foldl f acc (x:xs) = foldl f (f acc x) xs