gpt4 book ai didi

perl - Perl 的哪些特性使它成为一种函数式编程语言?

转载 作者:行者123 更新时间:2023-12-01 21:10:44 25 4
gpt4 key购买 nike

受以下启发: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::Utilreduce ?)

    有人能帮我填这里的空白吗? Perl 可以轻松完成以上哪一项(并且有一个简单的示例),是否有失败的示例?

    最佳答案

    相关的有用的东西:

    Perl monks rant about functional programming

    Higher Order Perl

    C2.com functional programming definitions

    First Class functions

    在计算机科学中,如果将函数视为一等公民,则称编程语言具有一等函数。具体来说,这意味着该语言支持将函数作为参数传递给其他函数,将它们作为来自其他函数的值返回,并将它们分配给变量或将它们存储在数据结构中。

    所以在 Perl 中:

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

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

    do_something($print_something);

    结论: native 支持

    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).



    结论: native 支持

    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->()

    结论: native 支持

    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 或 code refs 的散列。
    use strict;
    use warnings;

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

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

    因为它是 just哈希,您也可以添加代码引用和匿名子例程。 (注意 - 与面向对象编程并不完全不同)

    结论:解决方法

    Single Assignment

    Any assignment that changes an existing value (e.g. x := x + 1) is disallowed in purely functional languages.4 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 或者可能是 constant .

    判决:不支持

    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" ); 

    结论:原生( List::Utils 是核心模块)

    Monadic effects

    ...不,仍然有这些问题。它比我想象的要简单得多或复杂得多。

    如果有人有更多信息,请加入或编辑这篇文章或……什么。我对所涉及的一些概念仍然很粗略,所以这篇文章更像是一个起点。

    关于perl - Perl 的哪些特性使它成为一种函数式编程语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31046327/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com