From 74a607d1a6bd925a79c09bf82557126a74e34fd5 Mon Sep 17 00:00:00 2001 From: kjappvaffel <45148718+kjappvaffel@users.noreply.github.com> Date: Fri, 14 Oct 2022 15:25:24 +0200 Subject: [PATCH] Task 1 done --- tasks/task1.scala | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tasks/task1.scala b/tasks/task1.scala index e69de29..9eff86d 100644 --- a/tasks/task1.scala +++ b/tasks/task1.scala @@ -0,0 +1,29 @@ + +object Main extends App { + //task a + val x = (1 to 50).toList + println(x) + + //task b + def find_sum_of_list(list: List[Int]): Int = { + var sum: Int = 0 + list.foreach (sum += _) + sum + } + println(find_sum_of_list(List(1, 2, 3, 4))) + + //task c + def find_sum_of_list_rec(list: List[Int]): Int = list match{ + case List() => 0 + case _ => list.head + find_sum_of_list_rec(list.tail) + } + println(find_sum_of_list_rec(List(1, 2, 3, 4))) + + //task d + def nth_fibonacci(n: BigInt): BigInt = n match + case 0 => 0 + case 1 => 1 + case _ => nth_fibonacci(n-1) + nth_fibonacci(n-2) + + println(nth_fibonacci(10)) +} \ No newline at end of file