FP in Scala and Recursion in FP Flashcards

1
Q

write a sum function in Scala

A
def sum (xs : List[Int]) : Int = 
       xs match { 
              case Nil => 0
              case x :: ys => x + sum(ys)
       }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

define the type of length in Scala using a generic type

A

length :: [T] (xs : List [T]) Int

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

what type of list is also possible in Scala?

A

heterogenous lists

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

What is list comprehension called in Scala?

A

sequence comprehension

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

Write a quicksort function in Scala

A
def qsort ( xs : List[Int]) : List[Int] = 
        xs match { 
                 case Nil => Nil
                 case x :: ys => qsort(for(i  x) yield j)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is the basic looping mechanism in fp?

A

recursion

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

define zip with recursion

A

zip :: [a] -> [b] -> [(a, b)]
zip [] _ = []
zip _ [] = []
zip (x : xs) (y : ys) = (x, y) : zip xs ys

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

define qsort with recursion

A
qsort :: [Int] -> [Int]
qsort [] = []
qsort(x : xs) = qsort smaller ++ [x] ++ qsort larger
where
smaller = [a | a  x]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

5 steps for writing recursions

A
  1. define the type
  2. enumerate the cases
  3. define the base cases
  4. define the step cases
    5 generalise / simplify
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

write a recursive sum in Scala

A
def sum (xs : List[Int]) : Int =
      xs match {
            case Nil => 0
            case x :: ys => x + sum(ys)
      }

sum: (xs : List[Int]) Int

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

what are the 2 styles of typing?

A

church style and curry style

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

Describe Church-style typing

A

\x:A.b

with domain’s type label (A)

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

Describe Curry-style typing

A

\x.b

without somain’s type label

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

describe typing in Scala

A

we need the type label of the domain and range

\x:A.(b:B)

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

what is a μ-recursive function?

A

a general recursive function.

partial functions that take finite tuples of Nat numbers and return one Nat number

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

how can μ-recursive functions be generates?

A

> primitive functions (constant, successor, and projection functions)

> operators (composition, primitive recursion, and minimization operators)

17
Q

what is a primitive recursion operatot?

A

where only f(0) and f(n + 1) are defined

18
Q

what is a minimization operator?

A

μ(f) = z if f(z) = 0 and f(k) > 0 for k < z