Skip to content

Instantly share code, notes, and snippets.

@cgopalan
Created October 6, 2011 03:06
Show Gist options
  • Save cgopalan/1266404 to your computer and use it in GitHub Desktop.
Save cgopalan/1266404 to your computer and use it in GitHub Desktop.
Forall and Exists in term of Filter
object ListFilter {
def forall(xs: List[Int], p: Int => Boolean): Boolean =
xs.isEmpty || xs.filter(p) == xs
def exists(xs: List[Int], p: Int => Boolean): Boolean =
!xs.isEmpty && !xs.filter(p).isEmpty
def main(args: Array[String]) {
println("Are members of list (1,2,3,4,5) less than 10? : " +
forall(List(1,2,3,4,5), (x => x < 10)));
println("Is there at least one member of list (1,2,3,4,5) thats greater than 5? : " +
exists(List(1,2,3,4,5), (x => x > 5)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment