简体   繁体   English

访问存储在变量中的列表的头部和尾部作为进一步谓词的输入(Prolog)

[英]Access Head and Tail of List stored in a variable as input to further predicate (Prolog)

So, I have a simple list.所以,我有一个简单的清单。 Let's say:让我们说:

L([a,b,c]).

I want to access the Head and Tail (H|T) so I can recursively use the elements as input for other predicates.我想访问 Head 和 Tail (H|T),这样我就可以递归地使用这些元素作为其他谓词的输入。 Basically基本上

somePredicate(a),
someOtherPredicate(T).

until the list is empty.直到列表为空。 How can I do that?我怎样才能做到这一点?

I generated the list using:我使用以下方法生成了列表:

range(0,L) :-    
    L = [].
range(1,L) :- 
    L = [].
range(M,L) :-
    NewM is M - 1,
    numlist(1, NewM, L).

Simple:简单的:

 pre([H|T]):-
        writeln(H),
        writeln(T),
        pre(T).

?-pre([a,b,c]).
OUTPUT:
a
[b, c]
b
[c]
c
[]

If you want to use the values with another predicate:如果要将值与另一个谓词一起使用:

pre([H|T]):-
            writeln(H),
            pre1(T),
            pre(T).

pre1([H|T]):-
 writeln([H|T]).

?-pre([a,b,c]).
OUTPUT:
a
[b, c]
b
[c]
c
[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM