声明性语言 [英] Declarative Language
问题描述
我正在阅读关于声明性编程语言的文章。
如果我不明白这种类型/范式的编程语言的特性,并且它与命令式语言形成对比,那么我是否应该阅读这类编程语言的编程,比如Haskell,然后阅读那么这篇文章后面呢?
声明式范例的要点是懒惰。我们这些声明式程序员喜欢让编译器完成所有的工作。尽可能多的,我们没有指定我们想要使用的算法,而只是定义了我们想要的结果。
例如,如果在一个命令式的设置中你想要要计算直到 n
的整数总和,你可以写成(以C计): int f(int n){
int result = 0,i = 1;
for(; i <= n; i ++)
result + = i;
返回结果;
}
在声明式设置中,您只需声明这个总和(在Haskell中):
f 0 = 0
fn = n + f(n - 1)
这不是一个算法,它只是一个定义。但是,Haskell编译器足够聪明,无论如何我们都能得到结果。
当您切换到Prolog时,更具可见性,下面是标准示例:
-
我们在不同的人之间声明一些关系:
<$ c父亲(卢克,阿纳金)。
父亲(anakin,a_slave_on_tattouin)。
父亲(a_slave_on_tattouin,某人)。
父亲(某人,亚当)。
-
然后我们说明某人的祖先是他的父亲或他父亲的祖先:
祖先(年轻,老): -
父亲(年轻,老)。
祖先(年轻,非常老): -
父亲(年轻,老),
祖先(老,非常老)。
在那里,Prolog可以释放魔术和答案查询,如:
? - 祖先(luke,X)。
X = anakin;
X = a_slave_on_tattouin;
X =某人;
X =亚当;
false。
有助于阅读以上内容: Head: - Body
表示头
if Body
。因此,上面,
祖先(年轻,非常老): -
父亲(年轻,老),
祖先(Old,VeryOld)。
表示 和 正如你所看到的,我们没有指定算法的任何内容,Prolog仍然可以做很棒的事情,例如为我们提供 I was reading an article on Declarative Programming Languages. If I don't understand the qualities of this type/paradigm of programming languages and it's contrast to Imperative languages, should I just read up on programming in this type of programming language, like Haskell, and then read up that article later? The point of the declarative paradigm is to be lazy. We, declarative programmers, like to let compilers do all the work. As often as we can, we do not specify the algorithm we want to use but only the definition of our wanted results. For example, if in an imperative setup you want to calculate the sum of the integers up until in a declarative setup, you would just declare what this sum is (in Haskell): This is not an algorithm, it's merely a definition. But the Haskell compiler is clever enough to get us the result anyway. It's even more seeable when you switch to Prolog, with the canonical example that follows: We declare some relationships between various people: Then we state that someone's ancestor is his father or his father's ancestor: And there it is, Prolog can unleash the magic and answer queries such as: Some help to read those things above: means And As you can see, we didn't specify anything about the algorithm and still Prolog can do awesome things such as providing us all the details of 这篇关于声明性语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! VeryOld
是年轻的
的祖先,如果旧的是
年轻的
的父亲和 VeryOld
是旧的
的祖先。
;
表示或。所以在这里,Prolog告诉我们卢克有4个祖先, anakin
, a_slave_on_tattouin
等。
luke
的家谱。这是声明式编程的力量:指定数据,这就是你关心的内容,剩下的就是聪明人处理编译器的工作。n
, you could write (in C):int f(int n) {
int result = 0, i = 1;
for(;i <= n; i ++)
result += i;
return result;
}
f 0 = 0
f n = n + f (n - 1)
father(luke, anakin).
father(anakin, a_slave_on_tattouin).
father(a_slave_on_tattouin, someone).
father(someone, adam).
ancestor(Young, Old) :-
father(Young, Old).
ancestor(Young, VeryOld) :-
father(Young, Old),
ancestor(Old, VeryOld).
?- ancestor(luke, X).
X = anakin ;
X = a_slave_on_tattouin ;
X = someone ;
X = adam ;
false.
Head :- Body
means Head
if Body
. So above,ancestor(Young, VeryOld) :-
father(Young, Old),
ancestor(Old, VeryOld).
VeryOld
is Young
's ancestor if Old
is Young
's father and VeryOld
is Old
's ancestor.;
means or. So here Prolog tells us that luke has 4 ancestors, anakin
, a_slave_on_tattouin
, etc.luke
's genealogy. It's the power of declarative programming: you specify the data, that's what you care about, and the rest is the job of the clever guys handling the compiler.