Functional Programming 1 Flashcards
What is a higher order function?
A function that acts on other functions
What is a first order function?
A function that acts on lists, or other primitives
What is a function type? (Give an example)
The type A=>B is the type of a function that takes an argument of type A and returns a result of type B
What is the type of a function that maps an integer to an integer
int => int
What are function literals sometimes called
Anonymous functions - they do not have a name`
What does a theory consist of
- One or more data types 2. Operations on these data types 3. Laws that describe the relationships between these data types
If we want to implement high level concepts following their mathematical theories …
there is no place for mutation
A call is said to be in tail position if example ?
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
If all recursive calls made by a function are in tail position, Scala automatically
compiles the recursion to iterative loops that don’t consume call stack frames for each iteration.
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
tell the compiler about this assumption using the tailrec annotation
what does a tailrec annotation look like
@annotation.tailrec or just @tailrec if you have imported the annotation package scala.annotation.tailrec
write a method called fib that calculates the fibonacci number of a given integer.
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) }
create a method called formatResult that takes a name, an integer and a function that computes a result
def formatResult(name: String, n: Int, f: Int => Int) = { val msg = “The %s of %d is %d.” msg.format(name, n, f(n)) }
It’s a common convention to use names like ___________ for parameters to a higher order function
f, g, and h
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.
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
Often, and especially when writing HOFs, we want to write code that works for any type it’s given. These are called
polymorphic functions
def findFirstA:Int
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.
What is a polymorphic function sometimes called
A generic function (Parametric polymorphism)
//What is this?
(x: Int) => x == 9
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
write a function literal that takes two integers and returns a boolean as to whether or not they are equal
(x: Int, y: Int) => x == y
When we define a function literal, what is actually being defined in Scala is an object with a
method called apply.
What is being returned here?
def partial1A,B,C: B => C
a function that takes in a type B and returns a type C
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)
def curryA, B, C: A => B => C = (a: A) => (b: B) => f(a, b)
Implement uncurry, which reverses the transformation of curry. def uncurryA,B,C: (A, B) => C
def uncurryA, B, C: (A, B) => C = (a: A, b: B) => f(a)(b)
f andThen g is the same as
g compose f