blob: c019cb6cc78c010c74ee8804495b0f20644742e4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
enum HLst {
case HCons[+Hd, +Tl <: HLst](hd: Hd, tl: Tl)
case HNil
}
object Test {
import HLst._
def length(hl: HLst): Int = hl match {
case HCons(_, tl) => 1 + length(tl)
case HNil => 0
}
def sumInts(hl: HLst): Int = hl match {
case HCons(x: Int, tl) => x + sumInts(tl)
case HCons(_, tl) => sumInts(tl)
case HNil => 0
}
def main(args: Array[String]) = {
val hl = HCons(1, HCons("A", HNil))
assert(length(hl) == 2, length(hl))
assert(sumInts(hl) == 1)
}
}
|