Sunday, 5 June 2016

Haskell: Get all permutations starting with given letter.


PermUtil.hs
import Data.List

permutationsWithLetter x = filter (\letter -> (head letter == x)) . permutations

*Main> :load PermUtil.hs
[1 of 1] Compiling Main             ( PermUtil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> permutationsWithLetter 'a' "abc"
["abc","acb"]
*Main> 
*Main> permutationsWithLetter 'e' "abcde"
["edcba","ecdba","ebcda","ecbda","ebdca","edbca","eabcd","ebacd","ebcad","eacbd","ecabd","ecbad","eadcb","edacb","edcab","eacdb","ecadb","ecdab","eadbc","edabc","edbac","eabdc","ebadc","ebdac"]
*Main>



Previous                                                 Next                                                 Home

No comments:

Post a Comment