Back to Fpinscala

15.Answer

answerkey/laziness/15.answer.md

latest289 B
Original Source
scala
/*
The last element of `tails` is always the empty `LazyList`, so we handle this as a special case, by appending it to the output.
*/
def tails: LazyList[LazyList[A]] =
  unfold(this):
    case Empty => None
    case l @ Cons(_, t) => Some((l, t()))
  .append(LazyList(empty))