Oszur

Untitled

Apr 8th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. abstract class IntList
  2. case class Nil() extends IntList
  3. case class Cons(h: Int, t: IntList) extends IntList
  4.  
  5. object MyList {
  6.  
  7.   // Applies a binary operator to all elements of list as and a start value,
  8.   // going right to left.
  9.   def foldRight[B](as: IntList, b: B, f: (Int, B) => B): B = as match {
  10.     case Nil() => b
  11.     case Cons(h, t) => f(h, foldRight(t, b, f))
  12.   }
  13.  
  14.   // Applies a binary operator to a start value and all elements of list as,
  15.   // going left to right.
  16.   def foldLeft[B](as: IntList, b: B, f: (B, Int) => B): B = as match {
  17.     case Nil() => b
  18.     case Cons(h, t) => foldLeft(t, f(b, h), f)
  19.   }
  20.  
  21.   // Generate list of integers by applying f to b until it returns None
  22.   def unfold(b: Int, f: Int => Option[(Int, Int)]): IntList = f(b) match {
  23.     case None => Nil()
  24.     case Some((a, b)) => Cons(a, unfold(b, f))
  25.   }
  26.  
  27. }
  28.  
  29. object ListFuns {
  30.   import MyList._
  31.  
  32.   // return the sum of the elements of is
  33.   def sum(is: IntList): Int = //error("todo")
  34.     foldLeft(is, 0, (_:Int) + (_:Int))
  35.  
  36.   // return the number of elements of as
  37.   def length(as: IntList): Int = //error("todo")
  38.     foldLeft(as, 0, (y:Int, _) => y + 1)
  39.  
  40.   // apply function f to the elements of as
  41.   // and return the list of the results
  42.   def map(as: IntList, f: Int => Int): IntList = //error("todo")
  43.     foldRight(as, Nil(), (x:Int, xs:IntList) => Cons(f(x), xs))
  44.  
  45.   // return the elements of as for which f returns true
  46.   def filter(as: IntList, f: Int => Boolean): IntList = //error("todo")
  47.     foldRight(as, Nil(),
  48.               (x: Int, xs:IntList) => if(f(x)) Cons(x, xs) else xs)
  49.  
  50.   // return the list consisting of the elements of xs
  51.   // followed by the elements of ys
  52.   def append(xs: IntList, ys: IntList): IntList = //error("todo")
  53.     foldRight(xs, ys, (a, as:IntList) => Cons(a, as))
  54.  
  55.   // append the lists that are returned by mapping f to the elements of as
  56.   def flatMap(as: IntList, f: Int => IntList): IntList = //error("todo")
  57.     foldRight(as, Nil(),
  58.       (b, bs:IntList) => append(f(b), bs)
  59.     )
  60.    
  61.   // return the maximum number in is
  62.   def maximum(is: IntList): Int = //error("todo")
  63.     foldLeft(is, Int.MinValue, (x:Int, y:Int) => x max y)
  64.  
  65.   // turn the order of the elements of as around
  66.   def reverse(as: IntList): IntList = //error("todo")
  67.     foldLeft(as, Nil(), (xs:IntList, x:Int) => Cons(x, xs))
  68.  
  69.   // generate the list of integers from i until j
  70.   def fromUntil(i: Int, j: Int): IntList = // error("todo")
  71.     unfold(i, x => if(x > j) None else Some((x,x + 1)))
  72. }
Advertisement
Add Comment
Please, Sign In to add comment