【问题标题】:Counting list size resulting from a findall not working in Prolog由于 findall 在 Prolog 中不起作用而导致的计数列表大小
【发布时间】:2017-08-19 21:08:03
【问题描述】:

我使用findall获得了一个特定的列表,然后想统计其中的元素个数。

huntingbreeds(List) :-
   findall(Breedname, breed(Breedname,_,hunting), List).

这将返回我的数据库 [beagle, basset] 中的猎犬需求列表。

但是现在如果我尝试使用我的新谓词来计算它:

list_length([] , 0 ).
list_length([_|Xs] , L ) :- 
        list_length(Xs,N) , 
        L is N+1 .

我的查询 ?- list_length(huntingbreeds(List), Count)。

这只是返回 false。

我想我可能需要使用 arg 即

?- list_length(arg(1,huntingbreeds(List),L), Count).

但它再次返回 false。 有人知道我如何获得列表大小吗?我知道函数 list_length 就像我给它一样工作:

?- list_length([1,2,3], Count).

我明白了 计数 = 3。

但只是不知道如何从查找结果中将其传递给我的列表。我试过用完整的 findall 语句代替 huntingbreeds(List),但这仍然没有任何作用。

【问题讨论】:

    标签: prolog prolog-findall


    【解决方案1】:

    你可以试试:

    huntingbreeds(List),list_length(List, Count).
    

    【讨论】:

    • 感谢您的建议,但我必须使用 2 个参数创建该 list_length 谓词:list_length(List, count),但您的评论在我创建它时有所帮助:List_And_Count(L,Count) :-huntingbreeds(L),list_length(L, Count)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    相关资源
    最近更新 更多