MI-PSL-8 Flashcards

1
Q

What are main features of collection List?

A

List je základní datovou strukturou funkcionálního programování. Jejich hlavní výhody:

jsou immutable
mají rekurzivní strukturu
je předdefinováno velké množství funkcí pro práci s listy

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

Do write the definition of the class which defines the node of the list.

A

final case class ::B extends List[B] {def isEmpty = false}

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

Do write the definition of Nil.

A

case object Nil extends List[Nothing] {
override def isEmpty = true
override def head: Nothing =
throw new NoSuchElementException(“empty list”)
override def tail: List[Nothing] =
throw new UnsupportedOperationException
(“empty list”)

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

Do write the concatenation function of the class List.

A
def ::[B >: A] (x: B): List[B] =
   new scala.collection.immutable.::(x, this)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the elementary operations of list by which the other can be defined.

A

funkce:

head - vrací první prvek listu
tail - vrací zbytek listu
isEmpty - indikuje zda je seznam prázdný

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

Write the definition of method List.length

A
def length: Int = this match {
     case Nil => 0
     case x :: xs => 1 + xs.length
   }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write the definition of method List.init

A
def init: A = this match {
   case Nil => error("Nil.last")
   case x :: Nil => Nil
   case x :: xs => x :: xs.init
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write the definition of method List.last

A
def last: A = this match {
   case Nil => error("Nil.last")
   case x :: Nil => x
   case x :: xs => xs.last
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write the definition of method List.take

A

def take(n: Int): List[A] =
if (n == 0 || isEmpty) Nil
else head :: tail.take(n-1)

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

Write the definition of method List.drop

A

def drop(n: Int): List[A] =
if (n == 0 || isEmpty) this
else tail.drop(n-1)

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

Write the method which enables list subscripting.

A

def apply(n: Int): A = drop(n).head

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

Write the method which enables list concatenation.

A
def :::[B >: A](prefix: List[B]): List[B] =
    prefix match {
      case Nil => this
      case x :: xs => this.:::(xs).::(x)
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write the definition of method List.map

A
def map[B](f: A => B): List[B] =
      this match {
        case Nil => this
        case x :: xs => f(x) :: xs.map(f)
      }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write the definition of method List.foreach

A
def foreach(f: A => Unit) {
    this match {
      case Nil => ()
      case x :: xs => f(x); xs.foreach(f)
    }   
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write the definition of method List.filter

A
def filter(p: A => Boolean): List[A] = 
    this match {
      case Nil => this
      case x :: xs =>
                if (p(x)) x :: xs.filter(p)
                else xs.filter(p)
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Describe reduceLeft.

A

Kombinuje prvky listu nějakým operátorem. Např pro list 1 2 3 4 5 a operaci + udělá následující: ((((1 + 2) + 3) + 4) + 5). Př.

def sum(xs: List[Int]) = // xs != Nil
    xs reduceLeft {(x, y) => x + y}
17
Q

Describe foldLeft

A

Metoda reduceLeft selže na prázdném listu. Toto řeší metoda foldLeft, které se jako argument předává neutrální prvek dané operace. Pro + je to 0, pro * je to 1. Př.

def sum(xs: List[Int]) =
    (xs foldLeft 0) { (x, y) => x + y }
18
Q

Describe flatMap

A

Metoda flatMap aplikuje nějakou funkci f na všechny prvky seznamu a výsledné podlisty zřetězí