Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@priort
Created July 11, 2019 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save priort/264225fe3d6859b6a046c0c02709c428 to your computer and use it in GitHub Desktop.
Save priort/264225fe3d6859b6a046c0c02709c428 to your computer and use it in GitHub Desktop.
sealed class List<out E>
object Empty: List<Nothing>() {
override fun toString()= "[]"
}
data class Cons<E>(val head: E, val tail: List<E>): List<E>() {
override fun toString(): String = "$head :: $tail"
}
fun main() {
val myList1 = Empty
val myList2 = Cons(1, myList1)
val myList3 = Cons(2, myList2)
println("myList1: $myList1")
println("myList2: $myList2")
println("myList3: $myList3")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment