简体   繁体   English

在编程语言和范例的背景下,“纯粹”意味着什么?

[英]What does “Pure” mean, in the context of programming languages and paradigms?

In other words, what makes a language pure? 换句话说,是什么让一种语言变得纯洁?

For example, Smalltalk is considered to be a purely object-oriented language. 例如,Smalltalk被认为是纯粹的面向对象语言。 Haskell and Lisp are arguably known to be purely functional languages. 可以说Haskell和Lisp是纯函数式语言。

When we say pure, does that mean they're not capable of other programming paradigms (which is far from the truth) or does it mean they were designed to be used in that "purely" X paradigm? 当我们说纯粹时,这是否意味着他们不具备其他编程范式(这远非事实),还是意味着它们被设计用于那种“纯粹的”X范式?

The word pure has different meanings in different contexts. 纯粹这个词在不同的语境中有不同的含义。

Functional Programming 功能编程

When people talk about Haskell being a pure language, they mean that it has referential transparency . 当人们谈论Haskell是一种纯语言时,他们意味着它具有参考透明度 That is, you can replace any expression with its value without changing the meaning of the program. 也就是说,您可以使用其值替换任何表达式,而无需更改程序的含义。 For example, in Haskell: 例如,在Haskell中:

square :: Int -> Int
square x = x * x

main = print (square 4)

the expression square 4 can be replaced with its value (16) without changing the meaning of the program. 表达式square 4可以用其值(16)替换而不改变程序的含义。 On the other, hand, in this Java code: 另一方面,在这个Java代码中:

public int square(int x) {
    System.out.println("Done!");
    return (x*x);
}

public static void main(String [] args) {
   System.out.println(square(4));
}

you can't replace square(4) with its value (16) because it would change the meaning of the program - it would no longer print Done! 你不能用它的值(16)替换square(4) ,因为它会改变程序的含义 - 它将不再打印Done! to stdout. 到stdout。 In Haskell it is impossible for functions to have side effects like printing to stdout or changing memory locations, so referential transparency is enforced. 在Haskell中,函数不可能具有打印到stdout或更改内存位置等副作用,因此强制执行引用透明度。

Note that with this meaning of pure, Lisp is not a pure functional language, as its functions can have side effects (if you want to get picky, Haskell isn't a pure functional language because of the existence of unsafePerformIO , but everyone knows that you are consigned to one of the nastier circles of hell if you ever use that function). 请注意,有了pure的这个含义,Lisp 不是一个纯粹的函数式语言,因为它的函数可能有副作用(如果你想挑剔,Haskell不是一个纯函数式语言,因为unsafePerformIO的存在,但是每个人都知道如果你曾经使用过这个功能,你就会被委托给地狱之一。)

Of course, it is always possible to adopt a pure style in an impure language, and many programmers will do this in order to make reasoning about their programs easier. 当然,总是可以采用不纯语言的纯粹风格 ,许多程序员会这样做,以便更容易推理他们的程序。 It's just that referential transparency isn't enforced by the compiler, as it is in a pure language. 只是引用透明度不是由编译器强制执行的,因为它是纯语言。

Examples of purely functional languages include Haskell , Clean and Miranda . 纯功能语言的例子包括HaskellCleanMiranda Examples of impure functional languages include OCaml , F# and Scheme . 不纯函数语言的示例包括OCamlF#Scheme

Object Oriented Programming 面向对象编程

When people talk about Smalltalk or Ruby being a pure object-oriented language, they mean that there is no distinction between objects and primitive values. 当人们谈论Smalltalk或Ruby是纯粹的面向对象语言时,他们意味着对象和原始值之间没有区别。 In Smalltalk and Ruby, values like integers, booleans and characters are also objects, in the sense that they can receive messages (Smalltalk) or have methods (Ruby). 在Smalltalk和Ruby中,整数,布尔值和字符等值也是对象,因为它们可以接收消息(Smalltalk)或具有方法(Ruby)。 For example, you can do 例如,你可以做到

1.to_s

in Ruby, ie call the method of the integer 1 that converts it to a string. 在Ruby中,即调用整数1的方法,将其转换为字符串。 Compare this with an 'impure' OO language like Java, in which there are objects (which are instances of classes, and can have methods, etc) and primitive values (eg int , double , bool , which can't have methods). 将其与Java之类的“不纯”OO语言进行比较,其中有对象(类的实例,可以有方法等)和原始值(例如intdoublebool ,它们不能有方法)。

When an OO language is pure, people often say that "everything is an object", which isn't strictly true (for example, an if statement isn't an object) but is is true to say that "every value is an object". 当一个OO语言是纯粹的时候,人们经常说“一切都是一个对象”,这不是严格正确的(例如,一个if语句不是一个对象),但是说“每个值都是一个对象” 真的”。

Examples of pure object oriented languages include Ruby and Smalltalk . 纯面向对象语言的示例包括RubySmalltalk Examples of impure object oriented languages include Java and C++ . 不纯的面向对象语言的示例包括JavaC ++

The word "pure" in functional language is arguably "Stateless", the output does not depend on states. 功能语言中的“纯粹”一词可以说是“无国籍”,输出不依赖于国家。

For imperative languages, you assign x := 0, but you can reassign the value of variable x later. 对于命令式语言,您可以指定x:= 0,但稍后可以重新指定变量x的值。 The value of x depends on the current state. x的值取决于当前状态。

But for pure functional languages, if f(x) = 1, then the result will always be 1. 但对于纯函数式语言,如果f(x)= 1,则结果将始终为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM