本文介绍了Perl的哪些功能使其成为一种功能性编程语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

灵感来源:



它让我怀疑Perl是一个函数编程语言。现在,我明白功能编程是一种技术(很像面向对象)。

然而,我已经找到了:


  • 首先




  • 词法分析

  • 单一分配

  • 懒惰评估

  • 垃圾收集

  • 类型推断

  • 尾巴呼叫优化

  • 列表理解

  • Monadic特效



现在我熟悉一些这些:

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

词法分析甚至是FAQ的一部分: - 这里可能有更好的文章:



但是例如,我认为这是指 map / grep ( List :: Util 和 reduce ?)

http://www.perlmonks.org/?node_id=450922 =nofollow noreferrer> Perl僧侣们对功能性程序设计的咆哮





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



所以在Perl中:

  my $ print_something = sub {printSomething \\\
};

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

do_something($ print_something);



判决:本地支持





参考:



结论:本地支持





在perl FAQ中,我们有关于:

这篇文章的解释可能更清楚一点:

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

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



结论:本地支持





调度表是最接近的近似值 - 本质上是匿名子代码或代码引用的散列。

  use strict; 
使用警告;

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

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

因为它只是一个散列,你可以添加代码引用和匿名子例程。 (注意 - 与面向对象编程不完全不同)
$ b $ h2>结论:解决方法



我不确定 perl 真的没有这个。最接近的近似值可能是reference / anonymous subs或者 constant 。

结论:不支持





(I'这本书不属于本书,诚实 - 它似乎是关于这个主题的关键文本之一)。

这里的核心概念似乎是 - 在perl中创建一个'链表'(使用面向对象的技术),但是在你的'结束标记'处嵌入一个代码引用来评估如果你有这么远。

判决:解决方法





判决:原生支持







Perl会根据需要来隐式地转换值。通常这种方式工作得很好,以至于你不需要搞砸它。偶尔你需要通过明确的数字或​​字符串操作来强制进程。通常,这是通过加0或连接一个空字符串。

你可以通过使用



结论:原生支持







它会工作,但它会警告你的递归深度是否大于100。您可以通过添加以下内容来禁用此功能:

 无警告'递归'; 

但显然 - 您需要对递归深度和内存占用情况稍微谨慎。



据我所知,没有任何特别的优化,如果您想以高效的方式执行此类操作,你可能需要(有效地)展开你的递归并迭代。


$ b $提供的整理语法b

结论:本地





Perl具有 map , grep , reduce 。



它也可以处理范围和重复次数的扩展:

pre $ my @letters =(a。 。z);

所以你可以:

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



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



Monadic effects

a>

...不行,仍然遇到这些问题。它要么比我想象的要简单得多,要么复杂得多。

如果任何人有更多的东西,请切入或编辑这篇文章或...东西。对于一些涉及的概念,我仍然是一个粗略的概念,所以这篇文章更多的是一个起点。

Inspired a little by: https://stackoverflow.com/questions/30977789/why-is-c-not-a-functional-programming-language

I found: Higher Order Perl

It made me wonder about the assertion that Perl is a functional programming language. Now, I appreciate that functional programming is a technique (much like object oriented).

However I've found a list of what makes a functional programming language:

  • First Class functions
  • Higher Order Functions
  • Lexical Closures
  • Pattern Matching
  • Single Assignment
  • Lazy Evaluation
  • Garbage Collection
  • Type Inference
  • Tail Call Optimization
  • List Comprehensions
  • Monadic effects

Now some of these I'm quite familiar with:

Garbage collection, for example, is Perl reference counting and releasing memory when no longer required.

Lexical closures are even part of the FAQ: What is a closure? - there's probably a better article here: http://www.perl.com/pub/2002/05/29/closure.html

But I start to get a bit fuzzy on some of these - List Comprehensions, for example - I think that's referring to map/grep (List::Util and reduce?)

I anyone able to help me fill in the blanks here? Which of the above can Perl do easily (and is there an easy example) and are there examples where it falls down?

解决方案

Useful things that are relevant:

Perl monks rant about functional programming

Higher Order Perl

C2.com functional programming definitions

First Class functions

In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. Specifically, this means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.

So in Perl:

my $print_something = sub { print "Something\n" };

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

do_something($print_something);

Verdict: Natively supported

Higher Order Functions

With reference to this post on perlmonks:

Verdict: Natively supported

Lexical Closures

Within the perl FAQ we have questions regarding What is a closure?:

This is explained perhaps a little more clearly in the article: Achieving Closure

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

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

Verdict: Natively supported

Pattern Matching

Dispatch tables are the closest approximation - essentially a hash of either anonymous subs or code refs.

use strict;
use warnings;

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

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

Because it's just a hash, you can add code references and anonymous subroutines too. (Note - not entirely dissimilar to Object Oriented programming)

Verdict: Workaround

Single Assignment

I'm not sure perl really does this. The closest approximation might be references/anonymous subs or perhaps constant.

Verdict: Not Supported

Lazy Evaluation

Examples of lazy evaluation techniques in Perl 5?

And again, coming back to Higher Order Perl (I'm not affiliated with this book, honest - it just seems to be one of the key texts on the subject).

The core concept here seems to be - create a 'linked list' in perl (using object oriented techniques) but embed a code reference at your 'end marker' that evaluates if you ever get that far.

Verdict: Workaround

Garbage Collection

Perl does this via reference counting, and releasing things when they are no longer referenced. Note that this can have implications for certain things that you're (probably!) more likely to encounter when functional programming.

Specifically - circular references which are covered in perldoc perlref

Verdict: Native support

Type Inference

Perl does implicitly cast values back and forth as it needs to. Usually this works well enough that you don't need to mess with it. Occasionally you need to 'force' the process, by making an explicit numeric or string operation. Canonically, this is either by adding 0, or concatenating an empty string.

You can overload a scalar to do different things in by using dualvars

Verdict: Native support

Tail Call Optimization

Why is Perl so afraid of "deep recursion"?

It'll work, but it'll warn if your recursion depth is >100. You can disable this by adding:

no warnings 'recursion';

But obviously - you need to be slightly cautious about recursion depth and memory footprint.

As far as I can tell, there isn't any particular optimisation and if you want to do something like this in an efficient fashion, you may need to (effectively) unroll your recursives and iterate instead.

Verdict: Native

List Comprehensions

Perl has map, grep, reduce.

It also copes with expansion of ranges and repetitions:

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

So you can:

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

Verdict: Native (List::Utils is a core module)

Monadic effects

... nope, still having trouble with these. It's either much simpler or much more complex than I can grok.

If anyone's got anything more, please chip in or edit this post or ... something. I'm still a sketchy on some of the concepts involved, so this post is more a starting point.

这篇关于Perl的哪些功能使其成为一种功能性编程语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 15:50