FP2 Flashcards

1
Q

When would you use a enum and when woudl you use class instead ?

A

You should use an enum when you know how much case class you have in advance.

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

What is a sentinelle

A

La sentinelle est une valeur spéciale dans une structure de données, utilisée comme repère pour signaler la fin ou un état spécifique.

val notes = Array(75, 88, 92, 85, 0, 70, 68,-1) // -1 est la sentinelle

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

C’est quoi une variable de capture

A

Une variable de capture est une variable introduite dans un pattern matching (correspondance de motifs) pour capturer et stocker temporairement une valeur extraite d’une structure de données.

enum Context:
case Vide
case Cons(name: String, value: Int, tail: Context)

def printContext(context: Context): Unit = context match
case Context.Vide =>
println(“End of context.”)
case Context.Cons(name, value, tail) => // Variables de capture
println(s”Name: $name, Value: $value”)
printContext(tail)

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

Qunad on fait du pattenr mathching et que l’on veut dire que tous les autres cas retourne qqch comment peut on faire ?

A

Avec la syntaxe case _

Exemple:
def zipWith(l1: IntList, l2: IntList, op: (Int, Int) => Int): IntList =
(l1, l2) match
case (IntCons(x1, xs1), IntCons(x2, xs2)) =>
IntCons(op(x1, x2), zipWith(xs1, xs2, op))
case _ =>
IntNil

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
A
13
Q
A
14
Q
A
14
Q
A
15
Q
A
16
Q
A
17
Q
A
18
Q
A
18
Q
A
19
Q
A
19
Q
A
20
Q
A
21
Q
A
21
Q
A
22
Q
A
23
Q
A
24
Q
A
24
Q
A
25
Q
A
26
Q
A
26
Q
A
27
Q
A
28
Q
A