Skip to content

Instantly share code, notes, and snippets.

@scottoasis
Last active December 20, 2015 15:38
Show Gist options
  • Save scottoasis/6155644 to your computer and use it in GitHub Desktop.
Save scottoasis/6155644 to your computer and use it in GitHub Desktop.
object PlayWithString {
def isMatching(pat: String)(str: String): Boolean =
/*
-- termination --
*/
if (pat == "*" || (pat.isEmpty && str.isEmpty)) true
else if ((pat.isEmpty && !str.isEmpty) || (str.isEmpty)) false
/*
-- induction --
*/
else if (pat startsWith "*")
if (isMatching(pat.tail)(str)) true
else isMatching(pat)(str.tail)
else if (pat startsWith "?") isMatching(pat.tail)(str.tail)
else (str startsWith pat.head.toString) && isMatching(pat.tail)(str.tail)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment