加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl的哪些功能使其成为功能性编程语言?

发布时间:2020-12-15 21:27:03 所属栏目:大数据 来源:网络整理
导读:启发了一点点: https://stackoverflow.com/questions/30977789/why-is-c-not-a-functional-programming-language 我发现:Higher Order Perl 这让我想知道Perl是一种功能性编程语言.现在,我明白功能编程是一种技术(很像面向对象). 但是我找到了what makes a
启发了一点点: https://stackoverflow.com/questions/30977789/why-is-c-not-a-functional-programming-language

我发现:Higher Order Perl

这让我想知道Perl是一种功能性编程语言.现在,我明白功能编程是一种技术(很像面向对象).

但是我找到了what makes a functional programming language的列表:

>一级功能
>高阶函数
>词汇关闭
模式匹配
>单作业
>懒惰评估
>垃圾收集
>类型推断
>尾呼叫优化
>列表解析
单体效应

现在有些我很熟悉的:

例如,垃圾收集是不再需要的Perl引用计数和释放内存.

词汇关闭甚至是常见问题的一部分:What is a closure? – 这可能是一个更好的文章:http://www.perl.com/pub/2002/05/29/closure.html

但是我开始对其中的一些有些模糊 – 列表推理,例如 – 我认为这是指map / grep(List :: Util和reduce?)

我有谁能够帮助我填补这里的空白吗?以上哪一个可以让Perl轻松实现(有一个简单的例子),有哪些例子在哪里掉下来?

解决方法

有用的事情是相关的:

Perl monks rant about functional programming

Higher Order Perl

C2.com functional programming definitions

First Class functions

在计算机科学中,如果将程序语言视为一流公民,那么一种编程语言被认为具有一流的功能.具体来说,这意味着语言支持传递函数作为其他函数的参数,将它们作为其他函数的值返回,并将它们分配给变量或将它们存储在数据结构中.

所以在Perl:

my $print_something = sub { print "Somethingn" };

sub do_something {
    my ($function) = @_;
    $function->();
}

do_something($print_something);

判决:本地支持

Higher Order Functions

In mathematics and computer science,a higher-order function (also functional form,functional or functor) is a function that does at least one of the following:

  • takes one or more functions as an input

  • outputs a function

参考this post on perlmonks:

In Perl terminology,we often refer to them as callbacks,factories,and functions that return code refs (usually closures).

判决:本地支持

Lexical Closures

在perl常见问题解答中,我们对What is a closure?有疑问:

Closure is a computer science term with a precise but hard-to-explain meaning. Usually,closures are implemented in Perl as anonymous subroutines with lasting references to lexical variables outside their own scopes. These lexicals magically refer to the variables that were around when the subroutine was defined (deep binding).

Closures are most often used in programming languages where you can have the return value of a function be itself a function,as you can in Perl.

这可能在文章中更清楚一些:Achieving Closure

sub make_hello_printer {
    my $message = "Hello,world!";
    return sub { print $message; }
}

my $print_hello = make_hello_printer();
$print_hello->()

判决:本地支持

Pattern Matching

In the context of pure functional languages and of this page,Pattern Matching is a dispatch mechanism: choosing which variant of a function is the correct one to call. Inspired by standard mathematical notations.

调度表是最接近的近似值 – 本质上是匿名subs或代码引用的散列.

use strict;
use warnings;

sub do_it {
    print join( ":",@_ );
}
my $dispatch = {
    'onething'      => sub { print @_; },'another_thing' => &;do_it,};

$dispatch->{'onething'}->("fish");

因为它只是一个哈希,你可以添加代码引用和匿名子程序. (注意 – 不完全不同于面向对象编程)

判决:解决方法

Single Assignment

Any assignment that changes an existing value (e.g. x := x + 1) is disallowed in purely functional languages.070011 In functional programming,assignment is discouraged in favor of single assignment,also called initialization. Single assignment is an example of name binding and differs from assignment as described in this article in that it can only be done once,usually when the variable is created; no subsequent reassignment is allowed.

我不知道perl真的这样做.最接近的近似可能是引用/匿名subs或者可能是常量.

裁决:不支持

Lazy Evaluation

Waiting until the last possible moment to evaluate an expression,especially for the purpose of optimizing an algorithm that may not use the value of the expression.

Examples of lazy evaluation techniques in Perl 5?

再次,回到Higher Order Perl(我不是附属于这本书,诚实 – 它似乎是关于这个主题的关键文本之一).

这里的核心概念似乎是 – 在perl(使用面向对象的技术)中创建一个“链接列表”,但是在你的“结束标记”中嵌入一个代码引用,如果你有这么远的话.

判决:解决方法

Garbage Collection

“GarbageCollection (GC),also known as automatic memory management,is the automatic recycling of heap memory.”

Perl通过引用计数来执行此操作,并在不再引用时释放它们.请注意,这可能会对您在(功能性编程)时更容易遇到的某些事情产生影响.

具体 – perldoc perlref所涵盖的循环引用

判决:本地支持

Type Inference

TypeInference is the analysis of a program to infer the types of some or all expressions,usually at CompileTime

Perl根据需要隐含地传递价值.通常这个效果很好,你不需要混淆它.偶尔,你需要通过显式的数字或字符串操作“强制”进程.通常来说,这是通过添加0或连接一个空字符串.

您可以通过使用dualvars来超载标量来执行不同的操作

判决:本地支持

Tail Call Optimization

Tail-call optimization (or tail-call merging or tail-call elimination) is a generalization of TailRecursion: If the last thing a routine does before it returns is call another routine,rather than doing a jump-and-add-stack-frame immediately followed by a pop-stack-frame-and-return-to-caller,it should be safe to simply jump to the start of the second routine,letting it re-use the first routine’s stack frame (environment).

Why is perl so afraid of “deep recursion”?

它会工作,但如果您的递归深度为> 100,则会发出警告.您可以通过添加来禁用此功能:

no warnings 'recursion';

但显然,您需要对递归深度和内存足迹稍加谨慎.

据我所知,没有任何特别的优化,如果你想以高效的方式做这样的事情,你可能需要(有效地)展开你的递归,而不是迭代.

Tailcalls are supported by perl. Either see the goto ? notation,or see the neater syntax for it provided by Sub::Call::Tail

判决:本地

List Comprehensions

List comprehensions are a feature of many modern FunctionalProgrammingLanguages. Subject to certain rules,they provide a succinct notation for GeneratingElements? in a list.
A list comprehension is SyntacticSugar for a combination of applications of the functions concat,map and filter

Perl有map,grep,reduce.

它也适用于范围和重复的扩展:

my @letters = ( "a" .. "z" );

所以你可以:

my %letters = map { $_ => 1 } ( "A" .. "z" );

判决:Native(List :: Utils是一个核心模块)

Monadic effects

…不,仍然有这些麻烦.要么简单得多,要么比我更容易复杂.

如果有人有更多的东西,请填写或编辑这篇文章或…某事.我对某些涉及的概念仍然是粗略的,所以这篇文章更是一个起点.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读