Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: What technical benfits perl offers over python + few more questions.

by LanX (Sage)
on Nov 13, 2021 at 17:59 UTC ( #11138793=note: print w/replies, xml ) Need Help??


in reply to Re^3: What technical benfits perl offers over python + few more questions.
in thread What technical benfits perl offers over python + few more questions.

Hmm ... I don't know C's pointer arithmetic sufficiently well, but I wouldn't call this functional programming, so callbacks alone are not enough.

For instance, can you write a generator function which returns an iterator function?

What about closures?

So probably are First class citizen a prerequisite for FP but not the whole picture.

update

here we go: Some authors require it be possible to create new functions at runtime to call them 'first-class'. As a result, functions in C are not first-class objects; instead, they are sometimes called second-class objects, because they can still be manipulated in most of the above fashions (via function pointers).

see also First class functions

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

  • Comment on Re^4: What technical benfits perl offers over python + few more questions.

Replies are listed 'Best First'.
Re^5: What technical benfits perl offers over python + few more questions.
by talexb (Chancellor) on Nov 13, 2021 at 20:09 UTC
      For instance, can you write a generator function which returns an iterator function?

    My understanding of an iterator function tells me it's code that returns an item from a list -- which sounds like dynamic code. I don't believe C can do that easily.

      What about closures?

    C does have scoped variables, where a variable only exists within a pair of braces. When I was writing C code that got cross-compiled to 68000 assembler, I could see that the compiler just assigned space on the stack for that type of variable by moving the stack pointer down, and once the scope ended, the stack pointer was just adjusted back up.

    C also has the static keyword, which says the variable is to be stored in the lexical scope, meaning it's created at the beginning of the run (perhaps on the stack, but not anywhere that's going to get re-used), and only initialized once, if that initialization happens during declaration. So my guess is that closures are possible -- but keep in mind I studied engineering and not computer science, so that opinion is just what I've picked up since I left university.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

      C does have scoped variables, where a variable only exists within a pair of braces. When I was writing C code that got cross-compiled to 68000 assembler, I could see that the compiler just assigned space on the stack for that type of variable by moving the stack pointer down, and once the scope ended, the stack pointer was just adjusted back up.

      That's quite typical for a C compiler, and C works that way on all common platforms (x86, ARM, AVR, ...).

      C also has the static keyword, which says the variable is to be stored in the lexical scope, meaning it's created at the beginning of the run (perhaps on the stack, but not anywhere that's going to get re-used), and only initialized once, if that initialization happens during declaration.

      No, static variables inside a function are NOT on the stack. They work like normal static variables, except that the name of the variable is known only inside the function scope. If static local variables were on the stack, their content would be lost on return from the function.

      Example (on x86-64 Linux):

      /tmp>cat addr.c #include <stdio.h> static int global; void func(int param) { int local; static int static_local; printf( "global: %16p\n" "static local: %16p\n" "local: %16p\n" "param: %16p\n", (void *)&global, (void *)&static_local, (void *)&local, (void *)&param ); } int main(int argc, char ** argv) { func(0); return 0; } /tmp>gcc -o addr -Wall -pedantic addr.c /tmp>./addr global: 0x601048 static local: 0x60104c local: 0x7ffc40f001cc param: 0x7ffc40f001bc /tmp>

      The printf output is quite obvious: the global and the static local variable follow each other without any gap, in the data area. The local variable is far away, on the stack, as is the function parameter.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      > My understanding of an iterator function

      here an example of a generator creating an iterator and using closure variables.

      use strict; use warnings; sub gen_range_step { my ( $start,$stop,$step ) = @_ ; my $current = $start; return sub { my $val = $current; $current += $step; return $val if $val <=$stop; return; } } my $iter = gen_range_step(5,20,4); while ( my ($val) = $iter->() ) { print "$val\n"; }

      C:/Strawberry/perl/bin\perl.exe -w d:/tmp/pm/generator_iterator.pl 5 9 13 17 Compilation finished at Sat Nov 13 21:18:20

      > but keep in mind I studied engineering and not computer science

      I studied mathematics and some CS, and FP was never mentioned.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Thanks for the explanation -- the difference is that Perl can return a CODEREF, but C doesn't have that functionality. It would be possible to initialize a function to behave like an iterator, but without a great deal of jumping through hoops, you wouldn't be able to run multiple different iterators, as your Perl example could.

        That's OK -- without C, you wouldn't have pretty much any of the modern languages I can think of (Perl, Python, PHP). And C is still an amazing language.

        Alex / talexb / Toronto

        Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re^5: What technical benfits perl offers over python + few more questions.
by ikegami (Patriarch) on Nov 15, 2021 at 16:03 UTC

    C can't do closures. It can't even do nested functions. But even if it could do nested functions, it still couldn't do closures because function pointer are just addresses. There's no way to attach data to them.

    See here for a more detailed explanation of the above.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-05-21 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?