LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2008, 01:41 AM   #16
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244

Quote:
Originally Posted by Sergei Steshenko View Post
Ability to do more with less effort from programmer.
how about some facts and examples to justify that statement.
 
Old 11-27-2008, 01:48 AM   #17
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
how about some facts and examples to justify that statement.
Code:
sergei@amdam2:~/junk> cat partial_eval.pl
#!/usr/bin/perl -w

use strict;

# x * y + z
my $sub =
sub
  {
  my $result = shift; # x

  sub
    {
    my $result = $result * shift; # x * y

    sub
      {
      my $result = $result + shift; # x * y + z
      }
    }
  };


foreach my $x(1 .. 2)
  {
  my $sub = &{$sub}($x);

  foreach my $y(3 .. 4)
    {
    my $sub = &{$sub}($y);

    foreach my $z(5 ..6)
      {
      my $sub = &{$sub}($z);

      warn "\$x=$x \$y=$y \$z=$z \$sub=$sub";
      } # foreach my $z(5 ..6)
    } # foreach my $y(3 .. 4)
  } # foreach my $x(1 .. 2)
sergei@amdam2:~/junk> ./partial_eval.pl
$x=1 $y=3 $z=5 $sub=8 at ./partial_eval.pl line 35.
$x=1 $y=3 $z=6 $sub=9 at ./partial_eval.pl line 35.
$x=1 $y=4 $z=5 $sub=9 at ./partial_eval.pl line 35.
$x=1 $y=4 $z=6 $sub=10 at ./partial_eval.pl line 35.
$x=2 $y=3 $z=5 $sub=11 at ./partial_eval.pl line 35.
$x=2 $y=3 $z=6 $sub=12 at ./partial_eval.pl line 35.
$x=2 $y=4 $z=5 $sub=13 at ./partial_eval.pl line 35.
$x=2 $y=4 $z=6 $sub=14 at ./partial_eval.pl line 35.
sergei@amdam2:~/junk>
- I have already publish this piece of code.

For starters, rewrite it in Python, and then we'll discuss it.

This is written in Perl's functional dialect.
 
Old 11-27-2008, 01:55 AM   #18
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
http://www.jvoegele.com/software/langcomp.html - an interesting comparison.

From that table (and not only from it) one should pay attention to Eiffel.
 
Old 11-27-2008, 01:56 AM   #19
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
sorry I can't immediately see what you are trying to do. can you describe the problem statement? Is it to create a multiplication table or something?
 
Old 11-27-2008, 02:18 AM   #20
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
sorry I can't immediately see what you are trying to do. can you describe the problem statement? Is it to create a multiplication table or something?
I am not creating a multiplication table.

I've written a higher order function which uses anonymity and scoping.

These features allow me not to think about variable names on the one hand and as a consequence to easily enhance/extend the code, i.e. instead of x * y + z to implement, say, (x * y + z) * u by adding just another nested scope.

All this is in a sense implementation of AST (Abstract Syntax Tree) for the x * y + z expression.

The nested loops illustrate calling this higher order function, as well as partial evaluation concept. In this example the concept allows to save on multiplications - not a big deal, but imagine the function operates on BigInt kind of operands - multiplication becomes much more expensive.
 
Old 11-27-2008, 02:52 AM   #21
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
I've written a higher order function which uses anonymity and scoping.
so you are talking about something like DSL (domain specific language?). I am not into computer science nor am i a programmer, but those can be implemented using generators/iterators/lambdas in Python.

Quote:
All this is in a sense implementation of AST (Abstract Syntax Tree) for the x * y + z expression.
as i said, i don't understand AST enough to produce similar code, but there ARE ways to do that in Python.

Talking about Language X being more "powerful" than Y is always very "sensitive" and subjective.
 
Old 11-27-2008, 03:23 AM   #22
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
so you are talking about something like DSL (domain specific language?). I am not into computer science nor am i a programmer, but those can be implemented using generators/iterators/lambdas in Python.


as i said, i don't understand AST enough to produce similar code, but there ARE ways to do that in Python.

Talking about Language X being more "powerful" than Y is always very "sensitive" and subjective.
Not always, and there indexes - see
http://www.jvoegele.com/software/langcomp.html published earlier.

But the point is not AST in this case. The point is to analyze and discuss equivalent code in another language.

And believe me, I am _not_ going to discuss things like whitespace indentation in Python vs curly brackets in Perl/C/C++/Java - it's not the point.

Another point of discussion - data export/import, it also used to be more powerful in Perl than in Python in year 2000. Maybe Python is better in this respect now.

If you need help, I can explain in more detail what is happening in the code. For starters, in Perl the 'sub' keyword returns anonymous code reference, and {...} block returns the value of the last executed expression, so in most of the cases there is no need to use 'return' keyword.

'shift' with no arguments means 'shift @_', where '@_' is a predefined elastic array through which function arguments are passed - in my example it's always only one argument.
 
Old 11-27-2008, 04:00 AM   #23
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
If you need help, I can explain in more detail what is happening in the code. For starters, in Perl the 'sub' keyword returns anonymous code reference, and {...} block returns the value of the last executed expression, so in most of the cases there is no need to use 'return' keyword.

no its ok. I am not interested as I don't write code like that anyway. Its too untidy for me. If it really interests you, you can post your Perl code to comp.lang.python and see how others implement it in Python.
 
Old 11-27-2008, 05:40 AM   #24
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
no its ok. I am not interested as I don't write code like that anyway. Its too untidy for me. If it really interests you, you can post your Perl code to comp.lang.python and see how others implement it in Python.
And what is untidy ?
 
Old 11-27-2008, 06:13 AM   #25
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
And what is untidy ?
to me, "untidy" (as pertaining to the code you gave) means
1) not easy to read, for example, too many nested functions.
2) not easy to decipher such code in times of debugging and troubleshooting
3) I always try to give meaningful names to my variables, at least to production code.
4) i like to do things in simple ways. although you have not shown a use case for that Perl code you posted, I am sure there are easier ways to do what you are doing.

of course, these are just my preferences. No need to disagree/agree with me on that.

Last edited by ghostdog74; 11-27-2008 at 06:16 AM.
 
Old 11-27-2008, 06:33 AM   #26
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Quote:
Originally Posted by ghostdog74 View Post
as i said, i don't understand AST enough to produce similar code, but there ARE ways to do that in Python.
I might be wrong, but as far as I know, python does not support partial evaluation, which this is really about.
 
Old 11-27-2008, 06:42 AM   #27
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
to me, "untidy" (as pertaining to the code you gave) means
1) not easy to read, for example, too many nested functions.
2) not easy to decipher such code in times of debugging and troubleshooting
3) I always try to give meaningful names to my variables, at least to production code.
4) i like to do things in simple ways. although you have not shown a use case for that Perl code you posted, I am sure there are easier ways to do what you are doing.

of course, these are just my preferences. No need to disagree/agree with me on that.
The _purpose_ of the code is nested functions, this what higher order functions are about, it is the _definition_.

The practical result, as I wrote earlier, is execution time economy provided we consider multiplication expensive - multiplication is executed in outer loops, so less frequently than addition.

"not easy to decipher" - what is not easy to decipher ? There is relationship between the code and source line numbers, and every variable can be watched at the time of debugging.

"I always try to give meaningful names to my variables". Me too. But I think you are missing the point - there are _no_ meaningful names in this case. i.e. compare two function written, in say, "C":

Code:
double f1(double a, double b, double c)
  {
  return a * b + c;
  }

double f2(double x, double y, double z)
  {
  return x * y + z;
  }
- in which of the two f1, f2 functions variable names are more meaningful ?

If you honestly answer "in none", you'll logically come to AST concept and proof of function equality - my approach just uses current argument to the expression under consideration ("shift") and partial/final result ("$result") - so, Occam's razor is well respected.

If you can think of a simpler way, post it. I think my code is pretty close to what CS books say on the subject, but I may be wrong.

Again, regarding nestedness - Java proponents would write a sea of classes, each having one data member and a setter an a getter for it, then they'll create instances of the classes, and call it the only right way to do things.

I.e. they'll replace scopes with classes/instances, but scopes _already_ give inheritance, and you do not have to spend you energy inventing class and instance names and potentially creating conflicts of the said names.

Again and again, write as close as possible the same thing in Python, and we'll discuss maintainability.
 
Old 11-27-2008, 07:00 AM   #28
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
Again and again, write as close as possible the same thing in Python, and we'll discuss maintainability.
i don't study CS, nor am i a programmer like you do. alot of what i do are procedural. If you are interested to see how it can be done in Python, post your code to comp.lang.python.
 
Old 11-27-2008, 07:06 AM   #29
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by burschik View Post
I might be wrong, but as far as I know, python does not support partial evaluation, which this is really about.
how about extensions, like Psyco?
 
Old 11-27-2008, 07:32 AM   #30
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
I don't think so.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Best Programming Language for an Old Programmer falcon56215 Linux - General 5 12-22-2007 01:40 PM
LXer: Software tools detect bugs by inferring programmerís intentions LXer Syndicated Linux News 0 07-07-2006 09:54 PM
Programmer/System's Analyst tools Citizen Bleys Programming 0 01-16-2002 05:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration