Which features of Perl make it a functional programming language? -
inspired little by: https://stackoverflow.com/questions/30977789/why-is-c-not-a-functional-programming-language
i found: higher order perl
it made me wonder assertion perl functional programming language. now, appreciate functional programming technique (much object oriented).
however i've found list of what makes 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 of these i'm quite familiar with:
garbage collection, example, perl reference counting , releasing memory when no longer required.
lexical closures part of faq: what closure? - there's better article here: http://www.perl.com/pub/2002/05/29/closure.html
but start bit fuzzy on of these - list comprehensions, example - think that's referring map
/grep
(list::util
, reduce
?)
i able me fill in blanks here? of above can perl (and there easy example) , there examples falls down?
useful things relevant:
perl monks rant functional programming
c2.com functional programming definitions
first class functions
in computer science, programming language said have first-class functions if treats functions first-class citizens. specifically, means language supports passing functions arguments other functions, returning them values other functions, , assigning them variables or storing them in data structures.
so in perl:
my $print_something = sub { print "something\n" }; sub do_something { ($function) = @_; $function->(); } do_something($print_something);
verdict: natively supported
higher order functions
in mathematics , computer science, higher-order function (also functional form, functional or functor) function @ least 1 of following:
takes 1 or more functions input
outputs function
with reference this post on perlmonks:
in perl terminology, refer them callbacks, factories, , functions return code refs (usually closures).
verdict: natively supported
lexical closures
within perl faq have questions regarding what closure?
:
closure computer science term precise hard-to-explain meaning. usually, closures implemented in perl anonymous subroutines lasting references lexical variables outside own scopes. these lexicals magically refer variables around when subroutine defined (deep binding).
closures used in programming languages can have return value of function function, can in perl.
this explained perhaps little more in article: achieving closure
sub make_hello_printer { $message = "hello, world!"; return sub { print $message; } } $print_hello = make_hello_printer(); $print_hello->()
verdict: natively supported
pattern matching
in context of pure functional languages , of page, pattern matching dispatch mechanism: choosing variant of function correct 1 call. inspired standard mathematical notations.
dispatch tables closest approximation - hash of either anonymous subs or code refs.
use strict; use warnings; sub do_it { print join( ":", @_ ); } $dispatch = { 'onething' => sub { print @_; }, 'another_thing' => \&do_it, }; $dispatch->{'onething'}->("fish");
because it's just
hash, can add code references , anonymous subroutines too. (note - not entirely dissimilar object oriented programming)
verdict: workaround
single assignment
any assignment changes existing value (e.g. x := x + 1) disallowed in purely functional languages.4 in functional programming, assignment discouraged in favor of single assignment, called initialization. single assignment example of name binding , differs assignment described in article in can done once, when variable created; no subsequent reassignment allowed.
i'm not sure perl
this. closest approximation might references/anonymous subs or perhaps constant
.
verdict: not supported
lazy evaluation
waiting until last possible moment evaluate expression, purpose of optimizing algorithm may not use value of expression.
examples of lazy evaluation techniques in perl 5?
and again, coming higher order perl (i'm not affiliated book, honest - seems 1 of key texts on subject).
the core concept here seems - create 'linked list' in perl (using object oriented techniques) embed code reference @ 'end marker' evaluates if ever far.
verdict: workaround
garbage collection
"garbagecollection (gc), known automatic memory management, automatic recycling of heap memory."
perl via reference counting, , releasing things when no longer referenced. note can have implications things you're (probably!) more encounter when functional programming.
specifically - circular references covered in perldoc perlref
verdict: native support
type inference
typeinference analysis of program infer types of or expressions, @ compiletime
perl implicitly cast values , forth needs to. works enough don't need mess it. need 'force' process, making explicit numeric or string operation. canonically, either adding 0, or concatenating empty string.
you can overload scalar different things in using dualvars
verdict: native support
tail call optimization
tail-call optimization (or tail-call merging or tail-call elimination) generalization of tailrecursion: if last thing routine before returns call routine, rather doing jump-and-add-stack-frame followed pop-stack-frame-and-return-to-caller, should safe jump start of second routine, letting re-use first routine's stack frame (environment).
why perl afraid of "deep recursion"?
it'll work, it'll warn if recursion depth >100. can disable adding:
no warnings 'recursion';
but - need cautious recursion depth , memory footprint.
as far can tell, there isn't particular optimisation , if want in efficient fashion, may need (effectively) unroll recursives , iterate instead.
tailcalls supported perl. either see goto ⊂ notation, or see neater syntax provided
sub::call::tail
verdict: native
list comprehensions
list comprehensions feature of many modern functionalprogramminglanguages. subject rules, provide succinct notation generatingelements? in list. list comprehension syntacticsugar combination of applications of functions concat, map , filter
perl has map
, grep
, reduce
.
it copes expansion of ranges , repetitions:
my @letters = ( "a" .. "z" );
so can:
my %letters = map { $_ => 1 } ( "a" .. "z" );
verdict: native (list::utils
core module)
monadic effects
... nope, still having trouble these. it's either simpler or more complex can grok.
if anyone's got more, please chip in or edit post or ... something. i'm still sketchy on of concepts involved, post more starting point.
Comments
Post a Comment