Skip to content

Instantly share code, notes, and snippets.

@lion137
Created July 6, 2019 11:37
Show Gist options
  • Save lion137/0be5dab6a83e37155da4096e5b6ebde3 to your computer and use it in GitHub Desktop.
Save lion137/0be5dab6a83e37155da4096e5b6ebde3 to your computer and use it in GitHub Desktop.
functional list
sealed trait MyList[+A]
case object Nil extends MyList[Nothing]
case class Cons[+A] (head: A, tail: MyList[A]) extends MyList[A]
object HelloWorld {
def sum_int_list(xs: MyList[Int]): Int = xs match {
case Nil => 0
case Cons(x, ys) => x + sum_int_list(ys)
}
def main(args: Array[String]) {
val list: MyList[Int] = Cons(1, Cons(2, Cons(3, Nil)))
println(sum_int_list(list))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment