Created
December 12, 2013 21:29
-
-
Save Hulzenga/7935792 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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