获取序言中列表的分布
我想计算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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不使用复合术语来表示卡片?
考虑红心皇后 (
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.
Sample query for getting the respective counts:
一个可能的解决方案是:
示例:
备注在Prolog中,代表一对
[x,y]
作为表单xy 。
使用SWI-Prolog内置谓词,另一个可能的解决方案是:
示例:
A possible solution is:
Example:
Remark In Prolog, it is more idiomatic to represent a pair
[x,y]
as a term of the formx-y
.Using swi-prolog built-in predicates, another possible solution is:
Example:
我想到了[编辑,
从slago的答案中
]
更好 element-currences in-a-a-list-in-in-in-list in>逻辑上的纯组计数代码。
I came up with [edit, much better with
clumped
from slago's answer]:e.g.
Related logically pure group counting code.