Playing cards: Difference between revisions

Added Erlang
(Updated second D entry)
(Added Erlang)
Line 1,032:
=={{header|E}}==
See [[Playing Cards/E]]
 
=={{header|Erlang}}==
The -spec() is a type declaration. It is optional, but helps humans to understand complex data types and the Erlang type verifier (dialyzer) to check the code.
<lang Erlang>
-module( playing_cards ).
 
-export( [deal/2, deal/3, deck/0, print/1, shuffle/1, task/0] ).
 
-record( card, {pip, suite} ).
 
-spec( deal( N_cards::integer(), Deck::[#card{}]) -> {Hand::[#card{}], Deck::[#card{}]} ).
deal( N_cards, Deck ) -> lists:split( N_cards, Deck ).
-spec( deal( N_hands::integer(), N_cards::integer(), Deck::[#card{}]) -> {List_of_hands::[[#card{}]], Deck::[#card{}]} ).
deal( N_hands, N_cards, Deck ) -> lists:foldl( fun deal_hands/2, {lists:duplicate(N_hands, []), Deck}, lists:seq(1, N_cards * N_hands) ).
 
deck() -> [#card{suite=X, pip=Y} || X <- suites(), Y <- pips()].
 
print( Cards ) -> [io:fwrite( " ~p", [X]) || X <- Cards], io:nl().
 
shuffle( Deck ) -> knuth_shuffle:list( Deck ).
 
task() ->
Deck = deck(),
Shuffled = shuffle( Deck ),
{Hand, New_deck} = deal( 3, Shuffled ),
{Hands, _Deck} = deal( 2, 3, New_deck ),
io:fwrite( "Hand:" ),
print( Hand ),
io:fwrite( "Hands:~n" ),
[print(X) || X <- Hands].
 
 
 
deal_hands( _N, {[Hand | T], [Card | Deck]} ) -> {T ++ [[Card | Hand]], Deck}.
 
pips() -> ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"].
 
suites() -> ["Clubs", "Hearts", "Spades", "Diamonds"].
</lang>
{{out}}
<pre>
22> playing_cards:task().
Hand: {card,"9","Diamonds"} {card,"7","Clubs"} {card,"King","Clubs"}
Hands:
{card,"6","Hearts"} {card,"3","Diamonds"} {card,"Queen","Spades"}
{card,"10","Hearts"} {card,"2","Hearts"} {card,"6","Diamonds"}
</pre>
 
=={{header|Forth}}==
Anonymous user