Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Prolog对列表中的数字进行计数_Prolog - Fatal编程技术网

Prolog对列表中的数字进行计数

Prolog对列表中的数字进行计数,prolog,Prolog,我想知道你是否能帮我解决这个问题。我需要制作一个脚本来计算列表中的位数。以下是一个例子: amount([234,12,4356],L) 应导致: L=[c(234,3),c(12,2),c(4356,4)] 您需要获取atom的长度,并使其与该atom一起成为结果变量的头部: count(List,X):- counter(List,X). counter([],[]). counter([H|T],[(H,Z)|X]):- atom_length(H,Z),

我想知道你是否能帮我解决这个问题。我需要制作一个脚本来计算列表中的位数。以下是一个例子:

amount([234,12,4356],L) 
应导致:

 L=[c(234,3),c(12,2),c(4356,4)]

您需要获取atom的长度,并使其与该atom一起成为结果变量的头部:

 count(List,X):-
    counter(List,X).

counter([],[]).
counter([H|T],[(H,Z)|X]):-
    atom_length(H,Z),
    counter(T,X).
因此,有一个问题:

   ?- count([234,12,4356],L).
    L = [(234, 3),  (12, 2),  (4356, 4)].

到目前为止,您尝试了什么来解决这个问题?