This repository was archived by the owner on May 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlista6.sc
More file actions
70 lines (52 loc) · 1.64 KB
/
lista6.sc
File metadata and controls
70 lines (52 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//Arkadiusz Ziobrowski 229728
//Zadanie 1
def whileLoop(condition: =>Boolean)(expr: =>Any): Any =
if(condition) {
expr
whileLoop(condition)(expr)
}
var count = 0
whileLoop (count < 5) {
println(count)
count += 1
}
//Zadanie 2
def lrepeat[A](k: Int)(stream: Stream[A]): Stream[A] = {
def lrepeatInner(elem: A): Stream[A] = {
lazy val rep: Stream[A] = elem #:: rep
rep.take(k)
}
stream match {
case p #:: t => lrepeatInner(p) #::: lrepeat(k)(t)
case Stream.Empty => Stream.Empty
}
}
lrepeat(1)(Stream.Empty).toList == List()
lrepeat (1) (1 #:: 2 #:: 3 #:: Stream.Empty).toList == List(1, 2, 3)
(lrepeat (3) (Stream.from(1)) take 12).toList == List(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4)
//Zadanie 3
sealed trait lBT[+A]
case object LEmpty extends lBT[Nothing]
case class LNode[+A](elem: A, left: () => lBT[A], right: () => lBT[A]) extends lBT[A]
//a
def lBreath[A](ltree: lBT[A]): Stream[A] = {
def lBreadthInner(queue: List[lBT[A]]): Stream[A] = {
queue match {
case LNode(elem, left, right) :: t =>
elem #:: lBreadthInner(t ::: List(left(), right()))
case LEmpty :: t => lBreadthInner(t)
case Nil => Stream.Empty
}
}
lBreadthInner(List(ltree))
}
//b
def lTree(n: Int): lBT[Int] =
LNode(n, () => lTree(2 * n), () => lTree(2 * n + 1))
val empty = LEmpty
val tree = LNode[Int](1, () => LNode(2, () => LEmpty, () => LEmpty), () => LNode(3, () => LEmpty, () => LEmpty))
lBreath(empty).toList == List()
lBreath(tree).toList == List(1, 2, 3)
lBreath(LEmpty).toList == List()
lBreath(lTree(1)).take(1).toList == List(1)
lBreath(lTree(1)).take(10).toList == List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)