fork download
  1. object Main{
  2. def doSomething(l : MyList, i : Int):MyList = {
  3. l match {
  4. case Nil() => Nil()
  5. case Cons(e,tail) if e > i => Cons(e,doSomething(tail,i))
  6. case Cons(e,tail) => doSomething(tail,i)
  7. }
  8. }
  9.  
  10. def doIt(l : MyList, i : Int) : MyList = {
  11. (l,i) match {
  12. case (Nil(),_) => Nil()
  13. case (_,0) => Nil()
  14. case (Cons(e,t),j) => Cons(e,doIt(t,j-1))
  15. }
  16. }
  17. def main(args: Array[String]) {
  18. val l = Cons(13,Cons(23,Cons(17,Nil())))
  19. for( i <- 1 to 5) println(doIt(l,i))
  20. }
  21. }
  22.  
  23. case class Nil extends MyList
  24.  
  25. case class Cons(cur : Int , follow : MyList) extends MyList
  26.  
  27. object MyList {
  28. def push(n : Int, l : MyList):MyList = {
  29. Cons(n,l)
  30. }
  31. def tail(l : MyList) : MyList = {
  32. l match{
  33. case Nil() => Nil()
  34. case Cons(c,f) => f
  35. }
  36. }
  37. def pop(l : MyList) : (Int,MyList) = {
  38. l match{
  39. case Nil() => throw new IllegalArgumentException("wrong parameter")
  40. case Cons(c,f) => (c,f)
  41. }
  42. }
  43. }
Success #stdin #stdout 0.19s 211776KB
stdin
Standard input is empty
stdout
Cons(13,Nil())
Cons(13,Cons(23,Nil()))
Cons(13,Cons(23,Cons(17,Nil())))
Cons(13,Cons(23,Cons(17,Nil())))
Cons(13,Cons(23,Cons(17,Nil())))