Skip to content

Instantly share code, notes, and snippets.

@Hulzenga
Created December 12, 2013 21:29
Show Gist options
  • Save Hulzenga/7935792 to your computer and use it in GitHub Desktop.
Save Hulzenga/7935792 to your computer and use it in GitHub Desktop.
/**
* isSubset(a,b) determines whether Set a is a subset of Set b
*
* @param a
* - the first set
* @param b
* - the second set
* @return - true or false
*/
public static boolean isSubset(Set a, Set b) {
/*
* a smart optimisation would be to check if b >= a first. If not you
* can return false immediately
*/
if (a.isEmpty()) {
return true;
}
if (b.isEmpty()) { // we already know that a is not empty
return false;
}
if (a.getHead() == b.getHead()) {
return isSubset((Set) a.getTail(), (Set) b.getTail());
} else {
// this is convoluted and ugly
Set aHead = new Set();
aHead = Set.cons(a.getHead(), aHead);
return isSubset((Set) a.getTail(), b) && isSubset(aHead, (Set) b.getTail());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment