Sample exam 2017 Q1 Flashcards

1
Q

Write a short sentence or two about the difference between:

(a) ghc and ghci

A

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.

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

Write a short sentence or two about the difference between:

(b) Int and Integer

A

(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.

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

Write a short sentence or two about the difference between:

(c) + and ++

A

(c) ++ is the concatenation operator for lists whereas + is the addition operator for numeric types

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

Write a short sentence or two about the difference between:

(d) -1 and (-)1

A

-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

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

Write a short sentence or two about the difference between:

(e) \ and /

A

(e) \ can be used to separate multi line strings, or to begin lambda functions (\x y -> x + y)
/ is the division operator.

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

Write a short sentence or two about the difference between:

(f) a function and a functor

A
(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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a short sentence or two about the difference between:

g) [1,2] and (1,2

A

(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.

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

Write a short sentence or two about the difference between:

(h) && and and

A

(h) && is boolean AND which ands together two boolean values.

and is the conjunction of a boolean list (or indeed, any foldable type)

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

Write a short sentence or two about the difference between:

(i) True and Just True

A

(i) True is True :: Bool, Just True is True wrapped in the Maybe monad.

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

Write a short sentence or two about the difference between:

(j) foldl and foldr

A

(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

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