LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-08-2008, 07:24 PM   #31
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454

Quote:
Originally Posted by mannclay View Post
Can't place it... I love the utility of the kernel and its place in the structure of a machine.

Also, it seems that I have a taste for C syntax, which may help to understand most other programming languages. (maybe not perl).

But, really, to get whatever small programming gigs here and there while doing my other freelance job.

I read that companies outsource coders, but that they outsource to other countries as well which may not help me get started, you know, get SOME sort of exp.

I just wonder that if you know C and Python, simply what it can do, then when someone offers a problem, you can solve it wit those languages. But what are examples of some basic problems that can to be solved in C or Python?!

Also, what would be the basics learned in a language that can be transferred to other languages??
loops, do...while, all that sort of thing?
Perl also has more or less C-like syntax.

Feature-wise Python is inferior compared to Perl - the former has neither C-like scoping rules nor closures, which makes it much less convenient in everyday use.
 
Old 11-08-2008, 07:54 PM   #32
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
Feature-wise Python is inferior compared to Perl - the former has neither C-like scoping rules nor closures, which makes it much less convenient in everyday use.
provide examples of how C-like scoping rules/closures can make it convenient in everyday use.
 
Old 11-08-2008, 08:35 PM   #33
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
provide examples of how C-like scoping rules/closures can make it convenient in everyday use.


Code:
sergei@amdam2:~/junk> cat -n partial_eval.pl
     1  #!/usr/bin/perl -w
     2
     3  use strict;
     4
     5  # x * y + z
     6  my $sub =
     7  sub
     8    {
     9    my $result = shift; # x
    10
    11    sub
    12      {
    13      my $result = $result * shift; # x * y
    14
    15      sub
    16        {
    17        my $result = $result + shift; # x * y + z
    18        }
    19      }
    20    };
    21
    22
    23  foreach my $x(1 .. 2)
    24    {
    25    my $sub = &{$sub}($x);
    26
    27    foreach my $y(3 .. 4)
    28      {
    29      my $sub = &{$sub}($y);
    30
    31      foreach my $z(5 ..6)
    32        {
    33        my $sub = &{$sub}($z);
    34
    35        warn "\$x=$x \$y=$y \$z=$z \$sub=$sub";
    36        } # foreach my $z(5 ..6)
    37      } # foreach my $y(3 .. 4)
    38    } # 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>
Pay attention how scoping rules allow me to all the time use '$result' and '$sub' names.

If you think about it, the C/Pascal scoping rules are the basis for inheritance + overriding - inner scopes inherit everything from outer scopes, and newly introduced items in inner scopes override items with the same name from outer scopes.

Also, if we are talking about efficiency, compare my example with the traditional $x * $y + $z implementation - compare from the point of view of multiplications.
 
Old 11-09-2008, 02:34 AM   #34
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
Quote:
Pay attention how scoping rules allow me to all the time use '$result' and '$sub' names.
Which, I guess, many would consider a major causes of "obscurity" and therefore poor/confusing coding. Why would it be interesting in most cases to give different things the same name? Just because you can does not mean it is a good idea. These scoping tricks have all but disappeared from newer languages and I tend to think that as a rule it is a carefully considered decision, not lack of thought.

Last edited by jay73; 11-09-2008 at 02:36 AM.
 
Old 11-09-2008, 07:07 AM   #35
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jay73 View Post
Which, I guess, many would consider a major causes of "obscurity" and therefore poor/confusing coding. Why would it be interesting in most cases to give different things the same name? Just because you can does not mean it is a good idea. These scoping tricks have all but disappeared from newer languages and I tend to think that as a rule it is a carefully considered decision, not lack of thought.
Because the thing is in many senses the same - (partial) result.

Because this is what allows me to easily change the code without the fear of name collision, i.e. because it allows me to easily reuse and modify the code - which OOP proponents achieve through what they call inheritance.

By the way, inheritance by itself has no practical value, but easy reuse and modification do.

Because, actually, 'x', 'y', 'z' in 'x * y + z' have no meaning; the true meaning is: there are 3 operands anf the first should be multiplied by the second, and the product added with the third - see AST (abstract syntax trees)

Here is a modified example of the above example - now it's (x * y + z) * t:


Code:
sergei@amdam2:~/junk> cat -n partial_eval_1.pl
     1  #!/usr/bin/perl -w
     2
     3  use strict;
     4
     5  # x * y + z
     6  my $sub =
     7  sub
     8    {
     9    my $result = shift; # x
    10
    11    sub
    12      {
    13      my $result = $result * shift; # x * y
    14
    15      sub
    16        {
    17        my $result = $result + shift; # x * y + z
    18
    19        sub
    20          {
    21          my $result = $result * shift;
    22          }
    23        }
    24      }
    25    };
    26
    27
    28  foreach my $x(1 .. 2)
    29    {
    30    my $sub = &{$sub}($x);
    31
    32    foreach my $y(3 .. 4)
    33      {
    34      my $sub = &{$sub}($y);
    35
    36      foreach my $z(5 ..6)
    37        {
    38        my $sub = &{$sub}($z);
    39
    40        foreach my $t(7 .. 8)
    41          {
    42          my $sub = &{$sub}($t);
    43          warn "\$x=$x \$y=$y \$z=$z \$t=$t \$sub=$sub";
    44          } # foreach my $t(7 .. 8)
    45        } # foreach my $z(5 ..6)
    46      } # foreach my $y(3 .. 4)
    47    } # foreach my $x(1 .. 2)
sergei@amdam2:~/junk> ./partial_eval_1.pl
$x=1 $y=3 $z=5 $t=7 $sub=56 at ./partial_eval_1.pl line 43.
$x=1 $y=3 $z=5 $t=8 $sub=64 at ./partial_eval_1.pl line 43.
$x=1 $y=3 $z=6 $t=7 $sub=63 at ./partial_eval_1.pl line 43.
$x=1 $y=3 $z=6 $t=8 $sub=72 at ./partial_eval_1.pl line 43.
$x=1 $y=4 $z=5 $t=7 $sub=63 at ./partial_eval_1.pl line 43.
$x=1 $y=4 $z=5 $t=8 $sub=72 at ./partial_eval_1.pl line 43.
$x=1 $y=4 $z=6 $t=7 $sub=70 at ./partial_eval_1.pl line 43.
$x=1 $y=4 $z=6 $t=8 $sub=80 at ./partial_eval_1.pl line 43.
$x=2 $y=3 $z=5 $t=7 $sub=77 at ./partial_eval_1.pl line 43.
$x=2 $y=3 $z=5 $t=8 $sub=88 at ./partial_eval_1.pl line 43.
$x=2 $y=3 $z=6 $t=7 $sub=84 at ./partial_eval_1.pl line 43.
$x=2 $y=3 $z=6 $t=8 $sub=96 at ./partial_eval_1.pl line 43.
$x=2 $y=4 $z=5 $t=7 $sub=91 at ./partial_eval_1.pl line 43.
$x=2 $y=4 $z=5 $t=8 $sub=104 at ./partial_eval_1.pl line 43.
$x=2 $y=4 $z=6 $t=7 $sub=98 at ./partial_eval_1.pl line 43.
$x=2 $y=4 $z=6 $t=8 $sub=112 at ./partial_eval_1.pl line 43.
sergei@amdam2:~/junk>
So, as you can see, the change is trivial and straightforward.

And I haven't yet started showing GUI examples - the GUI world is ideal for such approaches due to widgets nestedness.

Anonymous references in Perl is a very powerful feature, it allows to safely write a lot of code.

By the way, AFAIK, closures are part computer science very basics, aren't they - I am not a CS guy by education.
 
  


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
Learning AWK language: BadBoy1954 Programming 6 09-18-2008 01:05 PM
learning a foreign language using linux fisher99 Slackware 2 05-20-2003 10:27 PM
Is Python a good language to learn for a newbie to programming? prophet621 Programming 2 05-19-2003 03:48 PM
Try Python, O'reilly Learning Python haknot Programming 5 02-15-2002 08:27 AM

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

All times are GMT -5. The time now is 03:15 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