用“让我们quot”的作业是不反对Clojure中功能编程的想法
应避免在功能编程中进行分配,但是在Clojure中,我们经常使用LET
。
让
只是一种实用的方式,还是与使用Let
不同的分配方式?我们不应该避免在功能编程中分配吗?
Assignment should avoided in functional programming, but in clojure we often use let
.
Is let
just a way of being practical or is assignment not the same as using let
? Should we not avoid assignment in functional programming?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可变状态通常是针对功能编程的核心概念。
但是,
令
仅绑定一个名称与值。如果该价值是不可变的,则没有理由与功能编程理想不一致。Mutable state is generally against the core concepts of functional programming.
However,
let
merely binds a name to a value. If that value is immutable, there's no reason for it to be inconsistent with functional programming ideals.人们不能说一般而言,分配是反对功能编程(FP)的想法。
def
表达式是分配以及LET
表达式。给事物和过程/功能的名称是抽象的平均值 - 编程意味着在重复出现的问题上采用抽象的很大一部分。命令性的样式滥用突变分配,从而创建/维护/映射(全局)状态。没有分配就无法突变。
因此,FP的目的是反对这种突变本身而不是分配。
实际上,FP甚至没有针对突变本身。
即使在功能性语言中,由于表现原因,突变也需要某些情况。
存在无害的突变 - 变量的突变,无论如何,这些变量再也不会引用了程序的其余部分 - 功能定义)。我倾向于称它们为“良性”突变。并且存在有害的突变 - 变量的突变,后来被称为 - 在其创建的范围之外生活的变量的突变 - 因此构成了某种无限状态。我称它们为“恶性”突变。
实际上,说fp避免了状态完全是错误的。
封闭实际上是fp中构成状态。通过关闭功能可以指隐藏变量,这些变量在不同函数调用之间保持“内存”/状态。但是它们以非常控制的方式应用。
可能这就是为什么定义FP如此困难的原因。很快,一个人过分简化了一些东西,从而造成更多的困惑,而不是澄清事物。
One cannot say that assignment in general is against the idea of functional programming (FP).
A
def
expression is an assignment as well as alet
expression. Giving names to things and procedures/functions is a mean of abstraction - and programming means to a big part applying abstraction on recurring problems.Imperative style misuses assignments for mutation and thus creating/maintaining/mapping of (global) states. Mutation is not possible without assignment.
So FP aims against such kind of mutations not assignments per se.
Actually FP is not even aiming against mutation per se.
Even in functional languages mutation is in some situations required for performance reasons.
There is harmless mutation - mutation of variables which are anyway never ever again referred to for the rest of the program - e.g. because they appear only within a certain scope (e.g. within the scope of a
let
expression or a function definition). I tend to call them 'benign' mutations. And there is harmful mutation - mutation of variables to which later is referred to - mutation of variables which go on living outside the scope they were created in - thus constituting some kind of an unlimited state. I call them 'malign' mutations.Actually it is also wrong to say FP avoids state alltogether.
Closures are actually constituting states in FP. Through closures functions can refer to hidden variables which keep a "memory"/state between different function calls. But they are applied in a very controlled manner.
Probably this is why defining FP is so difficult. Very quickly one has oversimplified something thereby causing more confusion than clarifying things.