[Haskell-cafe] how can I select all the 3-element-combination out
of a list efficiently
Marc A. Ziegert
coeus at gmx.de
Sun May 20 21:27:54 EDT 2007
with which model in Combinatorics in mind do you want that function? with or without repetition?
<http://en.wikipedia.org/wiki/Combinatorics#Permutation_with_repetition> the order matters and each object can be chosen more than once
<http://en.wikipedia.org/wiki/Combinatorics#Permutation_without_repetition> the order matters and each object can be chosen only once
<http://en.wikipedia.org/wiki/Combinatorics#Combination_without_repetition> the order does not matter and each object can be chosen only once
<http://en.wikipedia.org/wiki/Combinatorics#Combination_with_repetition> the order does not matter and each object can be chosen more than once
--------------------------------------------------
import Data.List
perm3_with_rep,perm3_without_rep,comb3_with_rep,comb3_without_rep :: [a] -> [(a, a, a)]
perm3_with_rep es = [(x,y,z)|x<-es,y<-es,z<-es]
perm3_without_rep es = [(x,y,z)|let it s=zip s $ zipWith (++) (inits s) (tail $ tails s),(x,xr)<-it es,(y,yr)<-it xr,z<-yr]
comb3_with_rep es = [(x,y,z)|let it=init.tails,xs@(x:_)<-it es,ys@(y:_)<-it xs,z<-ys]
comb3_without_rep es = [(x,y,z)|let it=init.tails,(x:xr)<-it es,(y:yr)<-it xr,z<-yr]
comb3_to_perm3 :: [(a, a, a)] -> [(a, a, a)]
comb3_to_perm3 xyz = concat[perm_without_rep [x,y,z]|(x,y,z)<-xyz]
--------------------------------------------------
- marc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070521/5acc3b0b/attachment-0001.bin
More information about the Haskell-Cafe
mailing list