Last active
December 22, 2015 02:28
-
-
Save DiveInto/6403139 to your computer and use it in GitHub Desktop.
Functional Programming Principles in Scala week-6 Anagram code snippet
This file contains hidden or 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
/** | |
计算全部子集 | |
Input: List( ('a', 2), ('b', 2)) | |
Output: | |
Nil | |
List(('a', 1)) | |
List(('a', 2)) | |
List(('b', 1)) | |
List(('b', 2)) | |
List(('a', 1), ('b', 2)) | |
List(('a', 2), ('b', 2)) | |
List(('a', 1), ('b', 1)) | |
List(('a', 2), ('b', 1)) | |
**/ | |
type Occurrences = List[(Char, Int)] | |
def combination(occ: Occurrences): List[Occurrences] = occ match { | |
case Nil => | |
List(List()) | |
case head :: tail => | |
for{i <- (0 to head._2).toList | |
rest <- combination(tail) | |
} yield ((head._1, i) :: rest).filter(_._2 != 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment