hi:
want to replace all elements with the number of its depth in the difference list in prolog. such as:

?- deepList([a, ], L).
L = [0, [1]].

?- deepList([a, [b, c, [d, e]], f], L).
L = [0, [1, 1, [2, 2]], 0].

Thank!

Recommended Answers

Write your own code, how do you expect to learn without training?

Jump to Post

All 2 Replies

Write your own code, how do you expect to learn without training?

I did write my own code, Just not sure whether it is using difference list correctly, so , I want to see how will other people write codes on this problem. My code is following:

deepList(L1,L2):- deepdif(L1,[],L2,0).
deepdif([],L,L,N).
deepdif([X|T1],Hole,L2,N):- \+ is_list(X),append(Hole,[N],Hole1),deepdif(T1,Hole1,L2,N).
deepdif([X|T1],Hole,L2,N):- X == [],!,append(Hole,[N],Hole1),deepdif(T1,Hole1,L2,N).
deepdif([X|T1],Hole,L2,N):- is_list(X),N1 is N + 1,deepdif(X,[],X1,N1),append(Hole,[X1],X2),deepdif(T1,X2,L2,N).

in fact, it can give the correct result, but I am not sure whether I just use accumulator or difference list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.