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

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

问题描述

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

所以在它有的那个教程上:

So on that tutorial where it has:

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).

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

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