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 pathlista3.sc
More file actions
98 lines (72 loc) · 2.5 KB
/
lista3.sc
File metadata and controls
98 lines (72 loc) · 2.5 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//Arkadiusz Ziobrowski - 229728
//Zadanie 1
//a
def existsRec[A](xs: List[A])(p: A => Boolean): Boolean =
xs match {
case h::t => p(h) || existsRec(t)(p)
case Nil => false
}
existsRec(List(5,1,2,3)) (_ == 2) == true
existsRec(List()) (_ == 2) == false
existsRec(List(1,1,1,1)) (_ > 2) == false
existsRec(List("a","a","a","a")) (_ == "a") == true
//b
def existsFL[A](xs: List[A])(p: A => Boolean): Boolean =
(xs foldLeft false) ((acc, x) => p(x) || acc)
existsFL(List(5,1,2,3)) (_ == 2) == true
existsFL(List()) (_ == 2) == false
existsFL(List(1,1,1,1)) (_ > 2) == false
existsFL(List("a","a","a","a")) (_ == "a") == true
//c
def existsFR[A](xs: List[A])(p: A => Boolean): Boolean =
(xs foldRight false) ((x, acc) => acc || p(x))
existsFR(List(5,1,2,3)) (_ == 2) == true
existsFR(List()) (_ == 2) == false
existsFR(List(1,1,1,1)) (_ > 2) == false
existsFR(List("a","a","a","a")) (_ == "a") == true
//Zadanie 2
def filter[A](xs: List[A])(p: A => Boolean): List[A] =
(xs foldRight List[A]()) ((x, acc) => if(p(x)) x::acc else acc)
filter(List(2,7,1,3,7,8,4,1,6,9)) (_ > 3) == List(7,7,8,4,6,9)
filter(List("A", "B")) (_ == "X") == List()
filter(List()) (_ == 1) == List()
filter(List(1,1,1,1)) (_ == 1) == List(1,1,1,1)
//Zadanie 3
//a
def remove1Rec[A](xs: List[A])(p: A => Boolean): List[A] =
xs match {
case h::t => if(p(h)) t else h::remove1Rec(t)(p)
case Nil => Nil
}
remove1Rec(List(1,2,3,2,5)) (_ == 2) == List(1, 3, 2, 5)
remove1Rec(List()) (_ != 3) == List()
remove1Rec(List("a","b","c")) (_ == "d") == List("a","b","c")
//b
def remove1TailRec[A](xs: List[A])(p: A => Boolean): List[A] = {
def inner(xs: List[A], acc: List[A]): List[A] =
xs match {
case h::t => if(p(h)) acc reverse_::: t else inner(t, h::acc)
case Nil => acc.reverse
}
inner(xs, List())
}
remove1TailRec(List(1,2,3,2,5)) (_ == 2) == List(1, 3, 2, 5)
remove1TailRec(List()) (_ != 3) == List()
remove1TailRec(List("a","b","c")) (_ == "d") == List("a","b","c")
//Zadanie 4
def splitAt[A](xs: List[A])(n: Int): (List[A], List[A]) =
xs match {
case h::t =>
if(n > 0) {
val tuple = splitAt(t)(n - 1)
(h :: tuple._1, tuple._2)
}
else
(Nil, xs)
case Nil => (Nil, Nil)
}
splitAt (List('a','b','c','d','e')) (2) == (List('a','b'), List('c','d','e'))
splitAt (List('a','b')) (3) == (List('a','b'), List())
splitAt (List('a','b','c','d','e')) (0) == (List(), List('a','b','c','d','e'))
splitAt (List()) (0) == (List(), List())
splitAt (List()) (10) == (List(), List())