Prolog - 如何从输入文件的给定列表中制作变量列表? [英] Prolog - How to make a variable list out of a given list from input file?
问题描述
我有一个输入谓词将文件作为列表读取:input(Filename,List).然后列表将采用
I have a input predicate will read the file as a list: input(Filename,List). The list then will be in the format of
["_","_","_",9,"_","_"]
"_"
字面上是字符下划线 "_"
这里,而不是通配符.问题是如何编写谓词 pred(List,List2) 然后将所有 "_"
转换为变量但保持 9
仍然在同一位置?所以如果我输入
"_"
is literally the character underscore "_"
here, not a wild card. The question is how can I write a predicate pred(List,List2) then transform all the "_"
into variables but keep 9
still at the same position? So If I type in
input(Filename,List),pred(List,X).
我会得到类似的东西
X = [_G1426, _G1429, _G1432, 9, _G1438, _G1441].
我知道我是否定义了一个谓词
I know if I define a predicate
pred(a,[_,_,_,9,_,_]).
然后通过调用 pred(a,X)
我可以得到类似的结果.但问题是如何让它适应各种输入列表,9
可能在另一个位置,或者列表可能有不同的大小.有人可以帮帮我吗?
Then by calling pred(a,X)
I can have the similar result. But the thing is how to make it adaptive to all kinds of input lists, 9
might be at a another position or the list might be of different size. Can somebody help me?
推荐答案
to_var_list([],[]).
to_var_list([A|R1],[B|R2]):-
(A = '_' ->
true;
A = B),
to_var_list(R1,R2).
交换 '_'
与 "_"
如果您实际上有一个字符串并且没有下划线原子.此外,还有一个 read
谓词可以执行您想要实现的操作,但它需要一个 .
在文件中的列表之后.
Exchange '_'
with "_"
if you actually have one character strings and not underscore atoms.
Also, there is a read
predicate that does something like you want to achieve, but it expects a .
after your lists in the file.
这篇关于Prolog - 如何从输入文件的给定列表中制作变量列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!