这让我想知道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
C2.com functional programming definitions
在计算机科学中,如果将程序语言视为一流公民,那么一种编程语言被认为具有一流的功能.具体来说,这意味着语言支持传递函数作为其他函数的参数,将它们作为其他函数的值返回,并将它们分配给变量或将它们存储在数据结构中.
所以在Perl:
my $print_something = sub { print "Something\n" }; sub do_something { my ($function) = @_; $function->(); } do_something($print_something);
判决:本地支持
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
In Perl terminology,we often refer to them as callbacks,factories,and functions that return code refs (usually 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->()
判决:本地支持
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");
因为它只是一个哈希,你可以添加代码引用和匿名子程序. (注意 – 不完全不同于面向对象编程)
判决:解决方法
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或者可能是常量.
裁决:不支持
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(使用面向对象的技术)中创建一个“链接列表”,但是在你的“结束标记”中嵌入一个代码引用,如果你有这么远的话.
判决:解决方法
“GarbageCollection (GC),also known as automatic memory management,is the automatic recycling of heap memory.”
Perl通过引用计数来执行此操作,并在不再引用时释放它们.请注意,这可能会对您在(功能性编程)时更容易遇到的某些事情产生影响.
具体 – perldoc perlref
所涵盖的循环引用
判决:本地支持
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 (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 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是一个核心模块)
…不,仍然有这些麻烦.要么简单得多,要么比我更容易复杂.