Haskell Flashcards

1
Q

What is a functor?

A

A functor is a container which provides a function fmap which allows us to transform its values using a function, while keeping the structure of the container the same.

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

Defining a functor instance for a data type

A

If the data type declaration is
“data TypeName a = Fizz a | Buzz (TypeName a) a”

The functor instance is

instance Functor TypeName where
fmap f (Fizz a) = Fizz (f a)
fmap f (Buzz b a) = TypeName (fmap f b) (f a)

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

Defining a functor instance for the data type “data Tree a = Leaf a | Node (Tree a) a (Tree a) (Tree a)”

A

instance Functor Tree where
fmap f (Leaf a) = Leaf (f a)
fmap f (Node left a mid right) = Node (fmap f left) (f a) (fmap f mid) (fmap f right)

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

What do tail and init do, and what is their syntax?

A

tail [1, 2, 3, 4, 5] = [2, 3, 4, 5]
init [1, 2, 3, 4, 5] = [1, 2, 3, 4]

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

What does zip do?

A

It turns a pair of lists into a list of pairs, up until the length of the shorter pair
Example:
zip [1, 2, 3, 4] [5, 6, 7] = [(1,5),(2,6),(3,7)]

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

What does zipWith do?

A

zipWith (op) a b

zips the lists a and b, and then performs the operation on each pair

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

Concatenating two lists

A

Use the ++ operator
Example: map (y*) x ++ mymult x ys

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

Example of patterns in function definition

A

mymult x [] = []
mymult x (y:ys) = map (y*) x ++ mymult x ys

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

Using zipWith to work out the differences between consecutive numbers in a list

A

zipWith (-) vge (tail vge))
Calculates A_i - A_i+1

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

Using length to calculate the number of items in myList which satisfy “condition”

A

length [x | x <- myList, condition]

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

Haskell if statement syntax

A

if <condition>
then <action>
else <action></action></action></condition>

if var rem 2 == 0
then putStrLn “Even”
else putStrLn “Odd”

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

Syntax for writing a function definition with multiple cases

A

functionName <pattern>
| <condition> = <value>
| <condition> = <value>
| otherwise = <value></value></value></condition></value></condition></pattern>

partitionHelper (x:xs) n (y:ys)
| sameSign n x == True = partitionHelper xs x ((x:y):ys)
| sameSign n x == False = partitionHelper xs x ([x]:y:ys)

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

AND, OR, NOT, not equal operators

A

AND = &&
OR = ||
NOT = not (e.g. (not True) = False)
not equal = =

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

Prepending an item to a list

A

Use the “:” operator
1 : [2, 3, 4] = [1, 2, 3, 4]

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

What is lambda calculus?

A

Rule system for describing computations solely via function abstraction and application. It’s the simplest known Turing-complete programming language.

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

Definition of a λ-expression

A

A variable v is a λ-expression

If M is a λ-expression, then (λv.M) is too. This is abstraction: defining a function with parameter v and body M.

If M and N are λ-expressions, then (MN) is a λ-expression (this is the application of M to N).

17
Q

λ-expression associativity rules

A

Application is left-associative, meaning MNP = ((MN)P)
Abstraction is right-associative
Application has precedence over abstraction, meaning that λx.λy.xy = λx.(λy.(xy))

18
Q

Bound and free variables

A

A variable is bound if it occurs in a function that takes a variable of the same name as input, so λx.x is bound. The opposite of bound is free, like the x in λy.x.

19
Q

Beta-reduction

A

Beta-reduction allows us to substitute the argument of an abstraction with the value of an application ((λx.M[x])N) => (M[x := N]).
Example: (λx.x)a = a

20
Q

Alpha-conversion

A

Allows us to resolve name conflicts by renaming bound variables like this: (λx.M[x]) => (λy.M[y])
This prevents us from capturing free variables when beta-reducing expressions.

21
Q

What is a functional?

A

A function that returns another function.

22
Q

What is currying?

A

Turning a function of n arguments into a function of n-1 arguments.
We create a function that takes 1 input and returns a function which takes n-1 arguments and takes 1 input, which in turn returns a function which…

Currying allows for functions with multiple arguments in languages like Haskell which only support unary functions.

23
Q

Constructing nameless function

A

“\x -> x + x” is a nameless function for computing f(x) = 2x

“\x -> (\y -> x + y)” is a nameless function for computing f(x, y) = x+y

24
Q

What do the backtick and brackets do?

A

Backtick turns a function name into an infix operator. mod 3 2 = 3 mod 2.

Brackets turn an infix operator into a function. 1 + 2 = (+) 1 2.

25
Q

Time complexity of list operations

A

O(n) for reversing and getting length
O(1) for “:” and getting the head or tail

26
Q

Using lists for pattern matching in function definitions

A

startsWithA :: [Char] -> Bool
startsWithA [‘a’, _, _] = True
startsWithA _ = False
# Matches 3-element lists starting with “a”

startsWithA :: [Char] -> Bool
startsWithA (‘a’:_) = True
startsWithA _ = False
# Matches any list of length at least 1 whose first entry is a.

Use [] when there’s a limited number of items and () when it can be any length. “_” represents anything.

27
Q

Syntax for typeclass restrictions

A

sumTwo :: Num a => [a] -> a

This means that the function only works on a list of values of type “a”, which is any member of the Num typeclass (includes Int and Integer).

28
Q

Syntax for generating a list with guards

A

[x | x <- [1..5], x mod 2 == 0]
= [2, 4]

29
Q

Syntax for generating a sequence of numbers

A

[a..b]
Example: [1..5]

30
Q

Syntax for generating a list of tuples using multiple generators

A

[(x, y) | x <- [1..3], y <- [x..3]]

Guards and generators can even be interspersed, but guards can only refer to variables on their left

31
Q

What is a side effect?

A

A hidden state change that results from a function modifying or relying upon objects external to its parameter list. A pure function is one without side effects

32
Q

What is a functional programming language?

A

A functional programming language supports and encourages a programming style with function application and composition as basic building blocks.

33
Q

Why do we need types?

A

They tell us how to interpret a variable, provide restrictions on valid operations, and are required if you want to know what a bit pattern means

34
Q

What is parametric polymorphism?

A

Where we write a single implementation of a function which applies generically and identically to values of any type

35
Q

What is a class?

A

A collection of types that support specified, overloaded operations called methods. Overloaded means “having at least one class constraint”

36
Q

Linear recursive function

A

One with only one self-reference, as opposed to a multiple recursive function

37
Q

Tail recursive

A

The last thing the function does is call itself

38
Q

What is a fold?

A

“foldr” takes a function of two variables, a value, and a list. It applies the function to the value and the last item of the list, and then it applies it to the second to last item and that result, and so on. “foldl” does the same thing but starting from the beginning.

39
Q

What is lazy evaluation?

A

Deferring computation until strictly necessary. Expressions are not evaluated when they are bound to variables, instead their evaluation is only done when their result is needed by other computations.