Skip to content

Instantly share code, notes, and snippets.

@DiveInto
Last active December 22, 2015 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DiveInto/6403139 to your computer and use it in GitHub Desktop.
Save DiveInto/6403139 to your computer and use it in GitHub Desktop.
Functional Programming Principles in Scala week-6 Anagram code snippet
/**
计算全部子集
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