Slide 64
Slide 64 text
4.6 再帰的データ
とくに代数的データ型は再帰的データの定義によく使用されます。このデータはそれ自
身の項によって定義され、サイズを制約しないデータを作成します。
ただし、再帰が永遠に続くため、このようには定義できません。
final case class Broken(broken: Broken)
最後を表すベースケースを定義することで妥当な再帰的データを定義できます。
sealed trait IntList
final case object End extends IntList
final case class Pair(head: Int, tail: IntList) extends IntList
Pair(1, Pair(2, Pair(3, End)))
// res: Pair = Pair(1,Pair(2,Pair(3,End)))