Skip to content

Commit

Permalink
Add exercise 2.1: suffixes
Browse files Browse the repository at this point in the history
Both a built-in List and custom LIST implementation
  • Loading branch information
jordanlewis committed Jan 30, 2012
1 parent b6d13e8 commit be04b71
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/scala/ChapterTwo.scala
@@ -0,0 +1,15 @@
package org.jordanlewis.pfds

object Suffixes {
def apply[A](xs: List[A]): List[List[A]] = xs match {
case Nil => Nil::Nil
case x::xs => (x::xs)::Suffixes(xs)
}
}

object CustomSuffixes {
def apply[A](xs: LIST[A]): LIST[LIST[A]] = xs match {
case NIL => new CONS(NIL, NIL)
case CONS(x, xs) => new CONS(new CONS(x, xs), CustomSuffixes(xs))
}
}

0 comments on commit be04b71

Please sign in to comment.