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
Q

f andThen g is the same as

A

g compose f

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

g compose f is the same as

A

f andthen g

27
Q

A functional data structure is (not surprisingly) operated on using

A

pure functions

28
Q

a pure function must not ____ or ____

A

change data in place or perform other side effects

29
Q

what does “sealed” mean when applied as an access modifier to a trait

A

All implementations of this trait must be declared within this file

30
Q

Cons[+A] what does the + mean

A

it means that the type parameter A is covariant

31
Q

def applyA: List[A] = if (as.isEmpty) Nil else Cons(as.head, apply(as.tail: _*))

//This is a variadic function - meaning?

A

It accepts zero of more arguments of type A:

32
Q

Variadic functions are just providing a little syntactic sugar for

A

creating and passing a Seq of elements explicitly.

33
Q

Implement a function tail for removing the first element of a List.

A

def tailA = as match { case h :: t => t case Nil => ??? }

34
Q

method(polymorphic) for replacing the item at the head of a list

A

def setHeadA = h :: tail(t)

35
Q

def factorial(n: Int) Int = if(n-0) 1 else n * factorial(n-1)

//is this a tail recursive function?

A

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
Q

What happens when a function call is not tail recursive?

A

There is a build up of intermediate results - that need to be kept until you can compute a final value.

37
Q

a number x is called a fixed point of a function f if

A

f(x) = x

38
Q

Generally functions associate to the ____ meaning that sum (cube) (1,10) is the same as

A

left (sum(cube))(1,10)

39
Q

def sum(f:Int => Int)(a:Int,b:Int):Int = //…

what is the function type

A

(Int => Int) => (Int, Int) => Int

40
Q

function types associate to the ______ which means that Int => Int => Int is read as

A

Int => (Int => Int)

41
Q

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

A

The characteristic function of the set

42
Q

A type that inherits from any other type

A

Nothing

43
Q

Type parameters e.g. [T] do not affect evaluation in scala another term for this is

A

Type erasure

44
Q

Languages that use type erasure include

A

Java, Scala, Haskell, ML and OCaml

45
Q

Languages that keep type parameters around at runtime include

A

C++ C# F#

46
Q

What are two principle types of polymorphism

A
  • Subtyping - Generics (Parametric)
47
Q

In Scala all lists are constructed from …

A

The Empty List Nil and the construction operation Cons

48
Q

what does x :: xs give you

A

A new list with the first element x followed by the list xs

49
Q

show an example of xs :: xs using fruit

A

apples :: (oranges :: (pears :: Nil))

50
Q

Convention - Operators ending in : associate … Which means what for a :: b :: c

A

Right this is interpreted as a :: (b :: c)

51
Q

What are the three fundamental operations on lists through which all other operations can be expressed

A

IsEmpty Head Tail

52
Q

Write the following method as an insertion sort

def isort(xs: List[Int]): List[Int]

A

def isort(xs: List[Int]): List[Int] = xs match { case List() => List() case y :: ys => insert(y, isort(ys)) }

53
Q

All lists are constructed from

A
  • The empty list Nil, together with - the construction operation :: cons
54
Q

do an insertion sort

def isort(xs : List[Int]): List [Int]

A

def isort(xs: List[Int]): List[Int] = xs match { case List() => List() case y :: ys = insert(y, isort(ys)) }

55
Q

xs.length

A

the number of elements of xs

56
Q

xs.last

A

the lists last element

57
Q

xs.init

A

a list consisting of all elements except the last one

58
Q

//implement remove the nth element of the list

def removeAtT:List[T]

A

def removeAtT:List[T] = (xs take n) ::: (xs drop n + 1)

59
Q

What does the splitAt function do ?

A

Returns two lists as a pair

  1. The list up to the index n
  2. The list after the index n
60
Q

// implement a concatenation function using recursion

def concatT:List[T]

A

def concatT:List[T] = xs match {

case List() => ys

case z :: zs => z :: concat(zs,ys)

}

61
Q

// implement the reverse function

def reverseT:List[T]

A

def reverseT:List[T] = xs match {

case List() => List()

case List(x) => List(x)

case x :: xs => reverse(xs) ++ List(x)

}

62
Q

def removeAtT:List[T]

A

def removeAtT:List[T] = xs take n ::: xs drop n+1

63
Q

Create a tuple with the values one, 2 and 3.00 called mytuple

How would you access 3

A

val mytuple = (“One”,2,3.00)

mytuple._3