变量及其在序言中的设置和使用方式 [英] Variables and how they are set and used in prolog

查看:36
本文介绍了变量及其在序言中的设置和使用方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_1.html

所以在它有的教程中:

conflict(Coloring) :- 
   adjacent(X,Y), 
   color(X,Color,Coloring), 
   color(Y,Color,Coloring). 

我的理解是否正确,Color 是一个变量,并在第一次调用 color 后设置为一个值,然后该值用于第二次调用 color?

Am I understanding this correctly, that Color is a variable and is set to a value after the first call to color and then that value is used in the second call to color?

推荐答案

Prolog 中的变量:

Variables in Prolog:

  1. 所有变量和参数在声明它们的谓词(又名首次使用)的范围内都是局部的.当然,变量可以作为参数(基本上是通过引用")传递给另一个谓词.

  1. All variables and arguments are local in scope to the predicate in which they are declared (aka first used). Excepting of course that variables may be passed as arguments (essentially "by reference") to another predicate.

Prolog 变量在与其他事物绑定(统一)之前只是可变的".在那一点上,它们不再是可变的,而是与它们所统一的对象合而为一.因此使用统一"一词:统一就是合一.

Prolog variables are only "variable" until bound (unified) with something else. At that point they cease to be variable and become one with that with which they were unified. Hence the use of the term "unification": to unify is to become one.

当然,回溯会取消任何可能发生的统一,使事情恢复到原状.

Backtracking, of course, undoes any unification that might have occurred, returning things to the status quo ante as it were.

特殊变量 _ 是匿名变量".每次使用,即使在谓词的同一子句中也是独立的.例如,鉴于事实

The special variable _ is the "anonymous variable". Each use, even within the same clause of a predicate is independent. For instance, given the facts

letter(a).
letter(b).
letter(c).

digit(1).
digit(2).
digit(3).

谓词:

foo :- letter(A),number(A).

失败,同时

foo :- letter(_),number(_).

将成功(9 次,回溯).

will succeed (9 times, with backtracking).

这篇关于变量及其在序言中的设置和使用方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆