Facebook
From Corrupt Matamata, 6 Years ago, written in OCaml (Objective Caml).
This paste is a reply to Re: Untitled from Queen Camel - go back
Embed
Viewing differences between Re: Untitled and Re: Re: Untitled
def Fib(n: Int): Int = 
{
  
let rec fib  = function n ->
        if ( n < 0) then raise (Failure "Gunwo")
        else if n = 0 then 0
        else if n = 1 then 1
        else fib (n - 1) + fib (n - 2);;


let fib2 = fun n ->
        let rec fib_tr = fun (n, fib1, fib2) -> 
                
if (n < 0) throw new Exception("Gunwo")
  
then raise(Failure "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) 
then fib1 
    else if (n == 1) then fib2
    else Fibb(n fib_tr(n 1, 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 
fib2)
        in fib_tr(n,0,1);;



let 
x1 = (-2, -1, 0, 1, 2)
val 
2);;
let 
(_, _, x, _, _) = xl

val 
x1;; 

let 
x2 = ((1, 2), (0, 1))
val 
1));;
let 
((_, _), (x, _)) = x2

def id[A](x:A) 
x2;;



let rec initSegment 
x
def initSegment[A] (xs1: List[A], xs2: List[A]): Boolean = {
//  val h1::t1 = xs1
//  val h2::t2 = xs2
  
fun (xs1,xs2) ->
        
if (xs1 == Nil) true
  
= []) then true
        
else if (xs2 == Nil) false
  
= []) then false
        
else if (xs1.head == xs2.head) initSegment(xs1.tail, xs2.tail)
  
(List.hd xs1 = List.hd xs2) then initSegment(List.tl xs1,List.tl xs2)
        
else false
}

val 
false;;


let 
l1 = 1 :: 2 :: Nil
val 
[];;
let 
l2 = :: Nil
[];;
initSegment(l2, l1)

def id[A](x:A) 
l1);;

let rec replaceNth 
x
def replaceNth[A] (xs:List[A], n: Int, el: A): List[A] = 
{
  
fun (xs, n, el) ->
        
if (n < 0 || xs == Nil) throw new Exception("Dupa")
  
= []) then raise (Failure "Dupa")
  
else if (n == 0) then el :: xs.tail
  
List.tl xs
  
else xs.head List.hd xs :: replaceNth(xs.tail,n-1,el)
}

replaceNth(List.tl xs,n-1,el);;

replaceNth('a' :: 'b' :: 'c' ::Nil, 0, 'd')

::[], 5, 'd')