Functional Programming 1 Flashcards

1
Q

What is a higher order function?

A

A function that acts on other functions

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

What is a first order function?

A

A function that acts on lists, or other primitives

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

What is a function type? (Give an example)

A

The type A=>B is the type of a function that takes an argument of type A and returns a result of type B

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

What is the type of a function that maps an integer to an integer

A

int => int

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

What are function literals sometimes called

A

Anonymous functions - they do not have a name`

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

What does a theory consist of

A
  1. One or more data types 2. Operations on these data types 3. Laws that describe the relationships between these data types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If we want to implement high level concepts following their mathematical theories …

A

there is no place for mutation

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

A call is said to be in tail position if example ?

A

the caller does nothing other than return the value of the recursive call: else myrecursivefuntion() is in tail position but else 1 + myrecursivefuntion() is not because there is still work to do when the function returns

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

If all recursive calls made by a function are in tail position, Scala automatically

A

compiles the recursion to iterative loops that don’t consume call stack frames for each iteration.

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

By default, Scala doesn’t tell us if tail call elimination was successful, but if we’re expecting this to occur for a recursive function we write, we can

A

tell the compiler about this assumption using the tailrec annotation

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

what does a tailrec annotation look like

A

@annotation.tailrec or just @tailrec if you have imported the annotation package scala.annotation.tailrec

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

write a method called fib that calculates the fibonacci number of a given integer.

A

def fib(x: Int): BigInt = { @tailrec def fibHelper(x: Int, prev: BigInt = 0, next: BigInt = 1): BigInt = x match { case 0 => prev case 1 => next case _ => fibHelper(x - 1, next, (next + prev)) } fibHelper(x) }

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

create a method called formatResult that takes a name, an integer and a function that computes a result

A

def formatResult(name: String, n: Int, f: Int => Int) = { val msg = “The %s of %d is %d.” msg.format(name, n, f(n)) }

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

It’s a common convention to use names like ___________ for parameters to a higher order function

A

f, g, and h

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

It’s a common convention to use names like f, g, and h for parameters to a higher- order function. In functional programming, we tend to use very short variable names, even one-letter names.

A

This is usually because HOFs are so general that they have no opinion on what the argument should actually do. All they know about the argu- ment is its type

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

Often, and especially when writing HOFs, we want to write code that works for any type it’s given. These are called

A

polymorphic functions

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

def findFirstA:Int

A

A polymorphic function that takes type A as a parameter. And instead of hardcoding an equality check for a given key, take a function with which to test each element of the array.

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

What is a polymorphic function sometimes called

A

A generic function (Parametric polymorphism)

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

//What is this?

(x: Int) => x == 9

A

It is a function literal that takes an integer and returns a boolean value depending on whether or not the integer is equal to 9

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

write a function literal that takes two integers and returns a boolean as to whether or not they are equal

A

(x: Int, y: Int) => x == y

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

When we define a function literal, what is actually being defined in Scala is an object with a

A

method called apply.

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

What is being returned here?

def partial1A,B,C: B => C

A

a function that takes in a type B and returns a type C

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

convert a function f of two arguments into a curried function of one argument that partially applies f. def curryA,B,C: A => (B => C)

A

def curryA, B, C: A => B => C = (a: A) => (b: B) => f(a, b)

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

Implement uncurry, which reverses the transformation of curry. def uncurryA,B,C: (A, B) => C

A

def uncurryA, B, C: (A, B) => C = (a: A, b: B) => f(a)(b)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
f andThen g is the same as
g compose f
26
g compose f is the same as
f andthen g
27
A functional data structure is (not surprisingly) operated on using
pure functions
28
a pure function must not ____ or \_\_\_\_
change data in place or perform other side effects
29
what does "sealed" mean when applied as an access modifier to a trait
All implementations of this trait must be declared within this file
30
Cons[+A] what does the + mean
it means that the type parameter A is covariant
31
def apply[A](as: A\*): List[A] = if (as.isEmpty) Nil else Cons(as.head, apply(as.tail: \_\*)) //This is a variadic function - meaning?
It accepts zero of more arguments of type A:
32
Variadic functions are just providing a little syntactic sugar for
creating and passing a Seq of elements explicitly.
33
Implement a function tail for removing the first element of a List.
def tail[A](as: List[A]) = as match { case h :: t =\> t case Nil =\> ??? }
34
method(polymorphic) for replacing the item at the head of a list
def setHead[A](h: A, t: List[A]) = h :: tail(t)
35
def factorial(n: Int) Int = if(n-0) 1 else n \* factorial(n-1) //is this a tail recursive function?
No. In order to be a tail recursive function the function needs to call itself as its last action. In this case the result of the call factorial(n-1) needs to be multiplied by n, therefor it is not the last action.
36
What happens when a function call is not tail recursive?
There is a build up of intermediate results - that need to be kept until you can compute a final value.
37
a number x is called a fixed point of a function f if
f(x) = x
38
Generally functions associate to the ____ meaning that sum (cube) (1,10) is the same as
left (sum(cube))(1,10)
39
def sum(f:Int =\> Int)(a:Int,b:Int):Int = //... what is the function type
(Int =\> Int) =\> (Int, Int) =\> Int
40
function types associate to the ______ which means that Int =\> Int =\> Int is read as
Int =\> (Int =\> Int)
41
Mathematically, we call the function which takes an integer as argument and which returns a boolean indicating whether the given integer belongs to a set, the
The characteristic function of the set
42
A type that inherits from any other type
Nothing
43
Type parameters e.g. [T] do not affect evaluation in scala another term for this is
Type erasure
44
Languages that use type erasure include
Java, Scala, Haskell, ML and OCaml
45
Languages that keep type parameters around at runtime include
C++ C# F#
46
What are two principle types of polymorphism
- Subtyping - Generics (Parametric)
47
In Scala all lists are constructed from ...
The Empty List Nil and the construction operation Cons
48
what does x :: xs give you
A new list with the first element x followed by the list xs
49
show an example of xs :: xs using fruit
apples :: (oranges :: (pears :: Nil))
50
Convention - Operators ending in : associate ... Which means what for a :: b :: c
Right this is interpreted as a :: (b :: c)
51
What are the three fundamental operations on lists through which all other operations can be expressed
IsEmpty Head Tail
52
Write the following method as an insertion sort def isort(xs: List[Int]): List[Int]
def isort(xs: List[Int]): List[Int] = xs match { case List() =\> List() case y :: ys =\> insert(y, isort(ys)) }
53
All lists are constructed from
- The empty list Nil, together with - the construction operation :: cons
54
do an insertion sort def isort(xs : List[Int]): List [Int]
def isort(xs: List[Int]): List[Int] = xs match { case List() =\> List() case y :: ys = insert(y, isort(ys)) }
55
xs.length
the number of elements of xs
56
xs.last
the lists last element
57
xs.init
a list consisting of all elements except the last one
58
//implement remove the nth element of the list def removeAt[T](xs:List[T], n:Int):List[T]
def removeAt[T](xs:List[T], n:Int):List[T] = (xs take n) ::: (xs drop n + 1)
59
What does the splitAt function do ?
Returns two lists as a pair 1. The list up to the index n 2. The list after the index n
60
// implement a concatenation function using recursion def concat[T](xs:List[T],ys:List[T]):List[T]
def concat[T](xs:List[T],ys:List[T]):List[T] = xs match { case List() =\> ys case z :: zs =\> z :: concat(zs,ys) }
61
// implement the reverse function def reverse[T](xs:List[T]):List[T]
def reverse[T](xs:List[T]):List[T] = xs match { case List() =\> List() case List(x) =\> List(x) case x :: xs =\> reverse(xs) ++ List(x) }
62
def removeAt[T](xs:List[T], n:Int):List[T]
def removeAt[T](xs:List[T], n:Int):List[T] = xs take n ::: xs drop n+1
63
Create a tuple with the values one, 2 and 3.00 called mytuple How would you access 3
val mytuple = ("One",2,3.00) mytuple.\_3