Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Solución primer punto
- */
- import scala.annotation.tailrec
- object Punto1 {
- def zipf(f:(Int,Int)=>Int, lstA:List[Int], lstB:List[Int]):List[Int] = {
- @tailrec
- def zipfAux(f:(Int,Int)=>Int, lstA:List[Int], lstB:List[Int], acc:List[Int]):List[Int] = {
- if (lstA.isEmpty || lstB.isEmpty) acc
- else zipfAux(f, lstA.tail, lstB.tail, acc :+ f(lstA.head, lstB.head))
- }
- zipfAux(f, lstA, lstB, List())
- }
- def main(args: Array[String]): Unit = {
- println(zipf((x,y)=>x+y, List(1,2,3), List(2,4,6)))
- println(zipf((x,y)=>x*y, List(1,2,3), List(2,4,6)))
- }
- }
- /**
- * Punto 2 examen opcional
- */
- object Punto2 {
- type Arbol = List[Any]
- def path(arb:Arbol, x:Int):List[String] = {
- arb match {
- case Nil => Nil
- case List(v,izq,der) => {
- if (x < v.asInstanceOf[Int]) "izq"::path(izq.asInstanceOf[List[Any]],x)
- else if (x > v.asInstanceOf[Int]) "der"::path(der.asInstanceOf[List[Any]],x)
- else Nil //Tambien puede ser case _
- }
- case _ => Nil //Warning
- }
- }
- def main(args: Array[String]): Unit = {
- val a = List(4,List(2, List(1, List(), List()), List(3, List(), List())),List(6, List(), List()))
- println(path(a, 4))
- println(path(a, 3))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement