How to define this class in scala
data NestedList a = Elem a | List [NestedList a]
This in Haskell means that a NestedList is a type that can contain either an Elem or another NestedList. Is it possible to make such recursive definitions in scala?
This is actually what I'm trying to achieve
Note Issue 7 here on this page.
Update ....
Follwing answers below, I created the NestedList Trait and case classes for Elem and NList . Trying to implement flatten , I'm stuck here.
def flatten[T](xs: NestedList[T]): List[T] = xs match{ case Elem(xs) => List(xs) //case NList //have to fill this case }
source share