Facebook
From Sweltering Sheep, 5 Years ago, written in Scala.
Embed
Download Paste or View Raw
Hits: 243
  1. def Fib(n: Int): Int =
  2. {
  3.   if (n < 0) throw new Exception("Gunwo")
  4.   else if (n == 0) 0
  5.   else if (n == 1) 1
  6.   else Fib(n - 1) + Fib(n - 2)
  7. }
  8.  
  9.  
  10.  
  11. import scala.annotation.tailrec
  12. def Fib_tr(n: Int): Int =
  13. {
  14.   @tailrec
  15.   def Fibb(n: Int, fib1: Int, fib2: Int): Int =  
  16.    
  17.     if (n < 0) throw new Exception("Gunwo")  
  18.     else if (n == 0) fib1
  19.     else if (n == 1) fib2
  20.     else Fibb(n - 1, fib2, fib1 + fib2)
  21.   Fibb(n,0, 1)
  22. }
  23.  
  24.  
  25.  
  26. import scala.annotation.tailrec
  27. def me_Kill_Myself (a: Double): Double = {
  28.   @tailrec
  29.   def root3_tl(x: Double): Double = {
  30.     if (math.abs(math.pow(x, 3) - a) <= 1.0E-15 * Math.abs(a)) x
  31.     else root3_tl(x + (a / math.pow(x, 2) - x) / 3)
  32.    
  33.   }
  34.   root3_tl(if (a > 1) a/3 else a)
  35. }
  36.  
  37. me_Kill_Myself (27)
  38.  
  39. val x1 = (-2, -1, 0, 1, 2)
  40. val (_, _, x, _, _) = xl
  41.  
  42. val x2 = ((1, 2), (0, 1))
  43. val ((_, _), (x, _)) = x2
  44.  
  45. def id[A](x:A) = x
  46. def initSegment[A] (xs1: List[A], xs2: List[A]): Boolean = {
  47. //  val h1::t1 = xs1
  48. //  val h2::t2 = xs2
  49.   if (xs1 == Nil) true
  50.   else if (xs2 == Nil) false
  51.   else if (xs1.head == xs2.head) initSegment(xs1.tail, xs2.tail)
  52. }
  53.  
  54. val l1 = 1 :: 2 :: Nil
  55. val l2 = 2 :: Nil
  56. initSegment(l2, l1)
  57.  
  58. def id[A](x:A) = x
  59. def replaceNth[A] (xs:List[A], n: Int, el: A): List[A] =
  60. {
  61.   if (n < 0 || xs == Nil) throw new Exception("Dupa")
  62.   else if (n == 0) el :: xs.tail
  63.   else xs.head :: replaceNth(xs.tail,n-1,el)
  64. }
  65.  
  66. replaceNth('a' :: 'b' :: 'c' ::Nil, 0, 'd')
  67.  
  68.  

Replies to Untitled rss

Title Name Language When
Re: Untitled Queen Camel scala 5 Years ago.