Skip to content

Instantly share code, notes, and snippets.

@jagbolanos
Created March 22, 2013 15:00
Show Gist options
  • Save jagbolanos/5221924 to your computer and use it in GitHub Desktop.
Save jagbolanos/5221924 to your computer and use it in GitHub Desktop.
case class NB(e:Int, izq:NB, der:NB)
def busquedaBinaria(v:Int, a:NB) : NB = {
if (a == null)
a
else {
if (v == a.e)
NB(v,null,null)
else {
if (v < a.e)
busquedaBinaria(v, a.izq)
else
busquedaBinaria(v, a.der)
}
}
}
//[1,2,3,5,7] => [1,3,6,11,18]
def acumularHelper(l:List[Int], acumulado:Int) : List[Int] = l match {
case Nil => Nil
case x :: xs => (acumulado + x) :: acumularHelper(xs, acumulado + x)
}
def acumular(l:List[Int]) : List[Int] = acumularHelper(l, 0)
def sumatoria(l:List[Int]) : Int = {
var x = 0
var z = (m:Int) => m * m
for (y <- l) {
x = x + z(y)
}
x
}
def duplicar(x:List[Int]) : List[Int] = x match {
case Nil => Nil
case y :: ys => y * 2 :: duplicar(ys)
}
def potencia2(x:List[Int]) : List[Int] = x match {
case Nil => Nil
case y :: ys => y * y :: potencia2(ys)
}
def aplicarfuncion(x:List[Int], f:Int => Int) : List[Int] = x match {
case Nil => Nil
case y :: ys => f(y) :: aplicarfuncion(ys, f)
}
def returnf (n:Int) : Int => Int = n match {
case 0 => (x:Int) => x
case 1 => (x:Int) => x + 5
case 2 => (x:Int) => x * 2
case _ => (x:Int) => x * x
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment