Trouble with finding length of list in Prolog

偶尔善良 提交于 2021-02-11 17:51:30

问题


I'm having trouble finding the length of a list. I know how to deal with lists such as say [a,b,c,d] or [a,b,[],d] or even [a,[[[]]],c,[]] each of which have a length of 4. The problem I'm having is trying to figure out the length of the list in a case like this [a,[b,c],d]. There are 4 elements, but when I run my code, it'll print 3. It considers the inner list [b,c] as a single element and I'm not sure how to count those separately.

Here's what I have:

% this is the base case
mylen([],0).

% case where element is a single atom like a or []
mylen([_|T],Len) :- atom(_),Len is Len1+1.

% case that *should* deal with sublists [b,c]
mylen([[_|TH]|T],Len) :- mylen(TH,Len1), Len is Len1+1.

% general case
mylen([_|T],Len):- mylen(T,Len1),Len is Len1+1.

I hope my question is clear. Thank you!

Ps. I looked at other posts concerning this, but couldn't find any solutions to this specific problem.


回答1:


Your problem comes from the fact that you need to treat the head of the list in a special way when it is a non-empty list. So for example:

strangelen([], 0).
strangelen([H|T], Len) :-
    (   H = [_|_] % head is a non-empty list
    ->  strangelen(H, LenH)
    ;   LenH = 1
    ),
    strangelen(T, LenT),
    Len is LenH + LenT.

And then:

?- strangelen([a,b,c,d], Len).
Len = 4.

?- strangelen([a,b,[],d], Len).
Len = 4.

?- strangelen([a,[[[]]],c,[]], Len).
Len = 4.

?- strangelen([a,[b,c],d], Len).
Len = 4.

?- strangelen([[]], Len).
Len = 1.

?- strangelen([[[b,c]]], Len).
Len = 2.

This solution does not work for a first argument that is not a proper list (try ?- strangelen(List, Len).)




回答2:


?- atom(_).
false.

Then the second clause it's useless, will always fail. You should not ignore the 'shape' of the list' head, since it could be a list. This problem also appears in the third clause.



来源:https://stackoverflow.com/questions/29002906/trouble-with-finding-length-of-list-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!