声明性语言 [英] Declarative Language

查看:182
本文介绍了声明性语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于声明性编程语言的文章。



如果我不明白这种类型/范式的编程语言的特性,并且它与命令式语言形成对比,那么我是否应该阅读这类编程语言的编程,比如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时,更具可见性,下面是标准示例:


  1. 我们在不同的人之间声明一些关系:

     <$ c父亲(卢克,阿纳金)。 
    父亲(anakin,a_slave_on_tattouin)。
    父亲(a_slave_on_tattouin,某人)。
    父亲(某人,亚当)。


  2. 然后我们说明某人的祖先是他的父亲或他父亲的祖先:

     祖先(年轻,老): -  
    父亲(年轻,老)。

    祖先(年轻,非常老): -
    父亲(年轻,老),
    祖先(老,非常老)。


在那里,Prolog可以释放魔术和答案查询,如:

 ? - 祖先(luke,X)。 
X = anakin;
X = a_slave_on_tattouin;
X =某人;
X =亚当;
false。

有助于阅读以上内容: Head: - Body 表示 if Body 。因此,上面,

 祖先(年轻,非常老): -  
父亲(年轻,老),
祖先(Old,VeryOld)。

表示 VeryOld 年轻的的祖先,如果旧的是年轻的的父亲和 VeryOld 旧的的祖先。

; 表示。所以在这里,Prolog告诉我们卢克有4个祖先, anakin a_slave_on_tattouin 等。



正如你所看到的,我们没有指定算法的任何内容,Prolog仍然可以做很棒的事情,例如为我们提供 luke 的家谱。这是声明式编程的力量:指定数据,这就是你关心的内容,剩下的就是聪明人处理编译器的工作。


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 n, you could write (in C):

int f(int n) {
    int result = 0, i = 1;
    for(;i <= n; i ++)
        result += i;
    return result;
}

in a declarative setup, you would just declare what this sum is (in Haskell):

f 0 = 0
f n = n + f (n - 1)

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:

  1. We declare some relationships between various people:

    father(luke, anakin).
    father(anakin, a_slave_on_tattouin).
    father(a_slave_on_tattouin, someone).
    father(someone, adam).
    

  2. Then we state that someone's ancestor is his father or his father's ancestor:

    ancestor(Young, Old) :-
        father(Young, Old).
    
    ancestor(Young, VeryOld) :-
        father(Young, Old),
        ancestor(Old, VeryOld).
    

And there it is, Prolog can unleash the magic and answer queries such as:

?- ancestor(luke, X).
X = anakin ;
X = a_slave_on_tattouin ;
X = someone ;
X = adam ;
false.

Some help to read those things above: Head :- Body means Head if Body. So above,

ancestor(Young, VeryOld) :-
    father(Young, Old),
    ancestor(Old, VeryOld).

means VeryOld is Young's ancestor if Old is Young's father and VeryOld is Old's ancestor.

And ; means or. So here Prolog tells us that luke has 4 ancestors, anakin, a_slave_on_tattouin, etc.

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

这篇关于声明性语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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