获取序言中列表的分布

发布于 2025-01-20 09:40:48 字数 616 浏览 0 评论 0原文

我想计算prolog中卡手的分布。 It means get this result:

?- distribution([sq,s9,s8,ha,hk,h5,da,dk,dj,d4,ca,c7,c6],D).
D=[[spade,3],[heart,3],[diamond,4],[club,3]]

But I don't know how to procede. I am able to get each color one by one, using this script:

distribution([], []).
distribution([H|T], [E|D]):-
    atom_chars(H, X),
    nth0(0, X, E),
    ( 
        E == 's' -> distribution(T, D); 
        E == 'h' -> distribution(T, D); 
        E == 'd' -> distribution(T, D); 
        E == 'c' -> distribution(T, D)
    ).

Can anyone help me?谢谢

I would like to calcul the distribution of a card hand in prolog.
It means get this result:

?- distribution([sq,s9,s8,ha,hk,h5,da,dk,dj,d4,ca,c7,c6],D).
D=[[spade,3],[heart,3],[diamond,4],[club,3]]

But I don't know how to procede. I am able to get each color one by one, using this script:

distribution([], []).
distribution([H|T], [E|D]):-
    atom_chars(H, X),
    nth0(0, X, E),
    ( 
        E == 's' -> distribution(T, D); 
        E == 'h' -> distribution(T, D); 
        E == 'd' -> distribution(T, D); 
        E == 'c' -> distribution(T, D)
    ).

Can anyone help me? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏夜暖风 2025-01-27 09:40:48

为什么不使用复合术语来表示卡片?

考虑红心皇后 (

Why not represent cards using compound terms?

Consider the queen of hearts (????): hq as an atom, h(q) as a compound.

This makes thing a lot easier: no need to sort and everything stays purely relational.

cards_([],[],[],[],[]).
cards_([Card|Cards],Cs,Ds,Hs,Ss) :-
   card_(Card,Cards,Cs,Ds,Hs,Ss).

card_(c(X),Cards,[X|Cs],Ds,Hs,Ss) :- cards_(Cards,Cs,Ds,Hs,Ss).
card_(d(X),Cards,Cs,[X|Ds],Hs,Ss) :- cards_(Cards,Cs,Ds,Hs,Ss).
card_(h(X),Cards,Cs,Ds,[X|Hs],Ss) :- cards_(Cards,Cs,Ds,Hs,Ss).
card_(s(X),Cards,Cs,Ds,Hs,[X|Ss]) :- cards_(Cards,Cs,Ds,Hs,Ss).

Sample query for getting the respective counts:

?- Cards = [s(q),s(9),s(8),h(a),h(k),h(5),d(a),d(k),d(j),d(4),c(a),c(7),c(6)],
   cards_(Cards,Clubs,Diamonds,Hearts,Spades),
   length(Clubs,N_Clubs),
   length(Diamonds,N_Diamonds),
   length(Hearts,N_Hearts),
   length(Spades,N_Spades).
Cards      = [s(q),s(9),s(8),h(a),h(k),h(5),d(a),d(k),d(j),d(4),c(a),c(7),c(6)],
Clubs      = [a,7,6],
Diamonds   = [a,k,j,4],
Hearts     = [a,k,5],
Spades     = [q,9,8],
N_Clubs    = 3, 
N_Diamonds = 4,
N_Hearts   = 3, 
N_Spades   = 3.
深空失忆 2025-01-27 09:40:48

一个可能的解决方案是:

distribution(Cards, Distribution) :-
    distribution(Cards, [], Distribution).

distribution([], Distribution, Distribution).
distribution([Card|Cards], Accumulator, Distribution) :-
    suit(Card, Suit),
    update(Accumulator, Suit, NewAccumulator),
    distribution(Cards, NewAccumulator, Distribution).

suit(Card, Suit) :-
    atom_chars(Card, [First|_]),
    text(First, Suit).

text(c, club).
text(s, spade).
text(h, heart).
text(d, diamond).

update([], Suit, [[Suit,1]]).
update([[S,N]|Rest], Suit, Distribution) :-
    (   S = Suit
    ->  succ(N, N1),
        Distribution = [[S,N1]|Rest]
    ;   Distribution = [[S,N]|Rest1],
        update(Rest, Suit, Rest1) ).

示例:

?- distribution([sq,s9,s8,ha,hk,h5,da,dk,dj,d4,ca,c7,c6],D).
D = [[spade, 3], [heart, 3], [diamond, 4], [club, 3]].

备注在Prolog中,代表一对[x,y]作为表单xy 。

使用SWI-Prolog内置谓词,另一个可能的解决方案是:

distribution2(Cards, Distribution) :-
    maplist(suit, Cards, Suits),
    msort(Suits, SortedSuits),
    clumped(SortedSuits, Distribution).

示例:

?- distribution2([sq,s9,s8,ha,hk,h5,da,dk,dj,d4,ca,c7,c6],D).
D = [club-3, diamond-4, heart-3, spade-3].

A possible solution is:

distribution(Cards, Distribution) :-
    distribution(Cards, [], Distribution).

distribution([], Distribution, Distribution).
distribution([Card|Cards], Accumulator, Distribution) :-
    suit(Card, Suit),
    update(Accumulator, Suit, NewAccumulator),
    distribution(Cards, NewAccumulator, Distribution).

suit(Card, Suit) :-
    atom_chars(Card, [First|_]),
    text(First, Suit).

text(c, club).
text(s, spade).
text(h, heart).
text(d, diamond).

update([], Suit, [[Suit,1]]).
update([[S,N]|Rest], Suit, Distribution) :-
    (   S = Suit
    ->  succ(N, N1),
        Distribution = [[S,N1]|Rest]
    ;   Distribution = [[S,N]|Rest1],
        update(Rest, Suit, Rest1) ).

Example:

?- distribution([sq,s9,s8,ha,hk,h5,da,dk,dj,d4,ca,c7,c6],D).
D = [[spade, 3], [heart, 3], [diamond, 4], [club, 3]].

Remark In Prolog, it is more idiomatic to represent a pair [x,y] as a term of the form x-y.

Using swi-prolog built-in predicates, another possible solution is:

distribution2(Cards, Distribution) :-
    maplist(suit, Cards, Suits),
    msort(Suits, SortedSuits),
    clumped(SortedSuits, Distribution).

Example:

?- distribution2([sq,s9,s8,ha,hk,h5,da,dk,dj,d4,ca,c7,c6],D).
D = [club-3, diamond-4, heart-3, spade-3].
慈悲佛祖 2025-01-27 09:40:48

我想到了[编辑,从slago的答案中

card_suite(Card, Suite) :-                                           % from s5
    atom_concat(Char, _, Card),                                      % to s + 5 = s5
    select(Char-Suite, [s-spade, h-heart, d-diamond, c-club], _).    % to spade

cards_distribution(Cards, Distribution) :-      % from [s5, c7, s2]
    sort(0, @=<, Cards, SortedCards),           % to [c7, s2, s5]
    maplist(card_suite, SortedCards, Suites),   % to [club, spade, spade]
    clumped(Suites, Distribution).              % to [club-1, spade-2]

]

?- cards_distribution([sq,s9,s8,ha,hk,h5,da,dk,dj,d4,ca,c7,c6,s8],D).
D = [club-3, diamond-4, heart-3, spade-4]

更好 element-currences in-a-a-list-in-in-in-list in>逻辑上的纯组计数代码。

I came up with [edit, much better with clumped from slago's answer]:

card_suite(Card, Suite) :-                                           % from s5
    atom_concat(Char, _, Card),                                      % to s + 5 = s5
    select(Char-Suite, [s-spade, h-heart, d-diamond, c-club], _).    % to spade

cards_distribution(Cards, Distribution) :-      % from [s5, c7, s2]
    sort(0, @=<, Cards, SortedCards),           % to [c7, s2, s5]
    maplist(card_suite, SortedCards, Suites),   % to [club, spade, spade]
    clumped(Suites, Distribution).              % to [club-1, spade-2]

e.g.

?- cards_distribution([sq,s9,s8,ha,hk,h5,da,dk,dj,d4,ca,c7,c6,s8],D).
D = [club-3, diamond-4, heart-3, spade-4]

Related logically pure group counting code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文