[erlang-questions] omaha poker hand permutations

Raimo Niskanen <>
Tue Aug 16 17:07:09 CEST 2011


On Tue, Aug 16, 2011 at 03:27:43PM +0100, Joel Reymont wrote:
> The problem can be reduced to the following:
> 
> 5> L3 = ["AD", "AH", "AS", "QS"].
> ["AD","AH","AS","QS"]
> 
> 6> L4 = [ {A, B} || A <- L3, B <- L3, A /= B].                            
> [{"AD","AH"},
>  {"AD","AS"},
>  {"AD","QS"},
>  {"AH","AD"},
>  {"AH","AS"},
>  {"AH","QS"},
>  {"AS","AD"},
>  {"AS","AH"},
>  {"AS","QS"},
>  {"QS","AD"},
>  {"QS","AH"},
>  {"QS","AS"}]
> 
> Note the duplicate pairs of AD, AH and AH, AD.
> 
> Can these be eliminated inside the list comprehension itself?

I guess no, since list comprehension permutation assumes order is
significant so the pairs {"AD","AH"} and {"AH","AD"} are both generated.
Comparisions can only be done between generated term components.

In this case you better write it with functions, the outer chewing off one
combining with the rest. Remember that list comprehensions are just
high level list function notation.

For example:
  perm([A|[_|_]=As]) -> perm(A, lists:reverse(As), perm(As));
  perm([_|_]) -> [];
  perm([]) -> [].

  perm(A, [B|Bs], Acc) -> perm(A, Bs, [{A,B}|Acc]);
  perm(_, [], Acc) -> Acc.

X> mod:perm(["AD", "AH", "AS", "QS"]).
[{"AD","QS"},
 {"AD","AS"},
 {"AD","AH"},
 {"AH","QS"},
 {"AH","AS"},
 {"AS","QS"}]

> 
> 	Thanks, Joel
> 
> --------------------------------------------------------------------------
> - for hire: mac osx device driver ninja, kernel extensions and usb drivers
> ---------------------+------------+---------------------------------------
> http://wagerlabs.com | @wagerlabs | http://www.linkedin.com/in/joelreymont
> ---------------------+------------+---------------------------------------
> 
> 
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list