Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^3: map sub to list?

by toolic (Bishop)
on Sep 09, 2013 at 17:25 UTC ( #1053050=note: print w/replies, xml ) Need Help??

in reply to Re^2: map sub to list?
in thread map sub to list?

No, using the comma makes it a case of EXPR, LIST.

Replies are listed 'Best First'.
Re^4: map sub to list?
by pldanutz (Acolyte) on Sep 09, 2013 at 18:12 UTC

    I can't seem to be able to reply to other replies (too much nesting probably). Perhaps I'll try the chatterbox or a separate question.

    I was trying to understand if the Perl compiler parses "f ($_)" in "map f ($_), @numbers" as a lambda. If it's not a block, but an expression, it must be either an arithmetic expression, a lambda expression etc. My question was from a syntactic/compiler point of view.

    And as to eager evaluation, obviously this is a counterexample, since map f($_) @nums doesn't immediately, eagerly evaluate f on the current $_, but instead passed a lambda to map()

Re^4: map sub to list?
by pldanutz (Acolyte) on Sep 09, 2013 at 17:28 UTC
    Where can I read about functions that have comma and non-comma parsings? This confuses me. Also, what expression is f ($_)? Is it a new function, i.e. a shorthand for sub f2 (_) { f ($_) }

      Your use of syntax leaves me with the impression that before Perl you only knew Haskell, or some other, lazy / currying functional language.

      The major difference between Perl and these others is that Perl is eager, so any unadorned mention of a function will simply call it. Unless you use built-in functions or fancy syntax tricks, all function parameters are passed in parentheses.

      Creating/returning new functions is quite possible in Perl, but the usual approach is more eager in the sense of passing around values instead of passing around functions that, at some time in the future will be called to produce the final output of the program.

      > Where can I read about functions that have comma and non-comma parsings?

      in my own words:

      BLOCK means { code } in curlies, like map { $_ +1 } 1..3, so no comma

      DB<124> map { $_ +1 } 1..3 => (2, 3, 4)

      EXPR can be any "atomar" code-snippet.

      DB<128> map sqrt , 1..3 => (1, "1.4142135623731", "1.73205080756888") DB<129> map 5 , 1..3 => (5, 5, 5)

      in both cases the list values are passed by setting a special variable (aka default variable) $_

      according to perlglossary

      BLOCK A syntactic construct consisting of a sequence of Perl stat +ements that is delimited by braces. The "if" and "while" statemen +ts are defined in terms of BLOCKs, for instance. Sometimes we als +o say "block" to mean a lexical scope; that is, a sequence of sta +tements that act like a "BLOCK", such as within an eval or a file, +even though the statements arenít delimited by braces. ... expression Anything you can legally say in a spot where a "value" is required. Typically composed of literals, variables, opera +tors, functions, and "subroutine" calls, not necessarily in that +order.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      f($_) means that you are calling the function, f, and passing it a scalar value contained in the $_ variable. See also perldoc perlsub

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1053050]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2022-12-08 10:08 GMT
Find Nodes?
    Voting Booth?

    No recent polls found