def Fib(n: Int): Int =
{
if (n < 0) throw new Exception("Gunwo")
else if (n == 0) 0
else if (n == 1) 1
else Fib(n - 1) + Fib(n - 2)
}
import scala.annotation.tailrec
def Fib_tr(n: Int): Int =
{
@tailrec
def Fibb(n: Int, fib1: Int, fib2: Int): Int =
if (n < 0) throw new Exception("Gunwo")
else if (n == 0) fib1
else if (n == 1) fib2
else Fibb(n - 1, fib2, fib1 + fib2)
Fibb(n,0, 1)
}
import scala.annotation.tailrec
def me_Kill_Myself (a: Double): Double = {
@tailrec
def root3_tl(x: Double): Double = {
if (math.abs(math.pow(x, 3) - a) <= 1.0E-15 * Math.abs(a)) x
else root3_tl(x + (a / math.pow(x, 2) - x) / 3)
}
root3_tl(if (a > 1) a/3 else a)
}
me_Kill_Myself (27)
val x1 = (-2, -1, 0, 1, 2)
val (_, _, x, _, _) = xl
val x2 = ((1, 2), (0, 1))
val ((_, _), (x, _)) = x2
def id[A](x:A) = x
def initSegment[A] (xs1: List[A], xs2: List[A]): Boolean = {
// val h1::t1 = xs1
// val h2::t2 = xs2
if (xs1 == Nil) true
else if (xs2 == Nil) false
else if (xs1.head == xs2.head) initSegment(xs1.tail, xs2.tail)
else false
}
val l1 = 1 :: 2 :: Nil
val l2 = 2 :: Nil
initSegment(l2, l1)
def id[A](x:A) = x
def replaceNth[A] (xs:List[A], n: Int, el: A): List[A] =
{
if (n < 0 || xs == Nil) throw new Exception("Dupa")
else if (n == 0) el :: xs.tail
else xs.head :: replaceNth(xs.tail,n-1,el)
}
replaceNth('a' :: 'b' :: 'c' ::Nil, 0, 'd')