Back to Fpinscala

04.Answer

answerkey/datastructures/04.answer.md

latest724 B
Original Source
scala
/*
Again, it's somewhat subjective whether to throw an exception when asked to drop more elements than the list
contains. The usual default for `drop` is not to throw an exception, since it's typically used in cases where this
is not indicative of a programming error. If you pay attention to how you use `drop`, it's often in cases where the
length of the input list is unknown, and the number of elements to be dropped is being computed from something else.
If `drop` threw an exception, we'd have to first compute or check the length and only drop up to that many elements.
*/
def drop[A](l: List[A], n: Int): List[A] =
  if n <= 0 then l
  else l match
    case Nil => Nil
    case Cons(_,t) => drop(t, n-1)