Skip to content

Instantly share code, notes, and snippets.

@wkimeria
Last active December 4, 2016 03:49
Show Gist options
  • Save wkimeria/7e9bc1d7e66f578abe958e457c5d2038 to your computer and use it in GitHub Desktop.
Save wkimeria/7e9bc1d7e66f578abe958e457c5d2038 to your computer and use it in GitHub Desktop.
def hasSubsequence[A](sup: List[A], sub: List[A]): Boolean = {
def loop[A](supRev: List[A], subRev: List[A], isSubSequence: Boolean): Boolean = supRev match {
case Nil => {
subRev match {
case Nil => isSubSequence
case _ => false
}
}
case Cons(x,xs) => subRev match {
case Nil => isSubSequence
case Cons(x2, xs2) => {
if(x == x2) loop(xs, xs2, true)
else loop(xs, subRev, false)
}
}
}
loop(sup, sub, false)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment