[英]How to recurse a single list in prolog?
I am reading a tutorial and I find it hard to understand how to recurse a single list. 我正在阅读教程,但很难理解如何递归单个列表。 Could someone give me a quick explanation of what the base case must be and why, and also what to do in the recursion.
有人可以快速给我解释一下基本案例必须是什么,为什么要这么做,以及在递归中要做什么。 My code is:
我的代码是:
type(string).
type(int).
instance(X,Y):- X, Y.
variable(_).
statement([]).
statement(A|B):- A, statement(B).
Purpose of the code is to make a light type checker to check things like this: 该代码的目的是使灯光类型检查器检查以下内容:
String s; int i; i = s.length();
I am passing this as a test: 我通过了作为测试:
statement([instance(type(string), variable(s))]).
I decided to put it in a list and recurse it and then just put it after the if. 我决定将其放在列表中并递归,然后将其放在if之后。 If it matches one of the rules, it'll be true.
如果符合其中一项规则,则为true。 Currently I am just making sure I can get the type instantiation to work.
目前,我只是确保可以使类型实例化正常工作。 Any help would be welcome!
任何帮助都将受到欢迎! Thanks in advance.
提前致谢。
You are missing a pair of square brackets in 您缺少一对方括号
statement(A|B)
It should be 它应该是
statement([A|B])
The rest of your recursive rule looks fine. 您的其余递归规则看起来不错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.