Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

What do you write when learning a new language?

by kyle (Abbot)
on Jun 04, 2008 at 15:19 UTC ( [id://690164]=perlmeditation: print w/replies, xml ) Need Help??

In Re: [OT] How do I really learn javascript?, dragonchild remarks, "A lot of programmers have a program they always write to learn a new language." Upon reading this, I realized that I always write a prime number generator. Here's one I wrote in Perl and posted in Re: ulam's spiral too slow:

use strict; use warnings; my @primes = ( 2, 3 ); my $candidate = $primes[-1] + 2; my $max_prime = shift || 100_000; CANDIDATE: while ( $candidate < $max_prime ) { my $sqrt_candidate = sqrt $candidate; PRIME: foreach my $prime ( @primes ) { last PRIME if $prime > $sqrt_candidate; next CANDIDATE if 0 == $candidate % $prime; } push @primes, $candidate; } continue { $candidate += 2; } print join q{,}, @primes; print "\n";

I've written programs like this in Python, C, BASIC, and others. I wrote one in C when learning to use an arbitrary precision math library once. It's a fairly straight forward math problem, and I can tell if the output is correct without any test framework. It requires looping, and I can expand it to use subroutines when it's time to learn that.

I recall another programmer saying he always writes a hex dump program in a new language. Of course, there's also the ever-popular Hello world program.

I'd love to hear from the monks. What program do you write in a new language, and why?

Replies are listed 'Best First'.
Re: What do you write when learning a new language?
by gamache (Friar) on Jun 04, 2008 at 15:29 UTC
    It really depends on the strengths of the particular language.

    For example, I wrote a compact disc bookkeeping system (heavy on parsing text files) when I learned Perl; I learned Ruby and Rails by writing an auction site; I learned Fortran by writing a nonlinear least-squares fitter; I learned Erlang by writing a chat server...

    The list goes on, but you get the picture. Pick a language based on its being the right tool for a particular job, then do that job. :)

Re: What do you write when learning a new language?
by dragonchild (Archbishop) on Jun 04, 2008 at 17:08 UTC
    I tend to write a program that plays Go. It never actually gets finished, but that bootstraps me enough.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      Do you have any that play at all? How strong are they typically? curious.

      -Paul

        None of them actually play cause I suck at implementing the alphabeta algorithm. Though, the one I'm working on in JS right now will do so because I think I've figured out a way of short-circuiting the alphabeta so that only the best positions are evaluated first.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: What do you write when learning a new language?
by starX (Chaplain) on Jun 04, 2008 at 16:03 UTC
    I write a Fibonacci sequence generator for much the same reasons: straightforward math, predictable output, and it requires looping. When it comes time to learn subroutines, I expand it to use a recursive approach to determining if the number is even or odd.

    This likewise has the benefit of being expandable to include some simple input, whether it be on the command line or otherwise.

    Here's what it would look like if I were writing it in Perl...

    #!/usr/bin/perl use strict; use warnings; my $number = 1; my $limit = 20; my @list_of_nums; while ($number < $limit) { push @list_of_nums, $number; $number = $number + $list_of_nums[-2]; } foreach my $item (@list_of_nums) { if (odd($item) eq 'Odd') { print "$item is Odd\n"; } else { print "$item is Even\n"; } } sub even { my $num = shift @_; if ($num == 1) { return 'Even'; } else { return odd(--$num); } } sub odd { my $num = shift @_; if ($num == 1) { return 'Odd'; } else { return even(--$num); } }
Re: What do you write when learning a new language?
by wade (Pilgrim) on Jun 04, 2008 at 16:31 UTC

    Usually, I choose to learn a new language when I have a project for which that language would be especially good. So, I write something completely new.

    --
    Wade
Re: What do you write when learning a new language?
by philcrow (Priest) on Jun 04, 2008 at 18:54 UTC
    I start with Newton's method for the square root of two or three. This also leads to a simple answer that I don't need a debugger to check. I start with a simple loop, then move that to a subroutine, then I move the subroutine to a differnt file so I can learn to make a library. Then I expand the function in the library to handle different root finding.

    I did this in Pugs to try out Perl 6 and liked it so much I wrote an article about it. But, I've done it for many other languages over the years including Perl 5 and even (shudder) COBOL.

    Phil

    The Gantry Web Framework Book is now available.
Re: What do you write when learning a new language?
by jettero (Monsignor) on Jun 04, 2008 at 15:28 UTC
    I always write a simple mysql application that does a "select to_days(now()) - to_days('certain special date')" using bind vars if the language supports it -- which they all should by now I hope.

    I then write a Fibonacci generator as close to this recursive formula as possible (ignoring the fact that there are better ways to do it):

    use strict; for my $i (0 .. 35) { print "i=$i ... fib(i)=", fib($i), "\n"; } sub fib { my $n = shift; return fib($n-1) + fib($n-2) if $n>1; return 1; }

    -Paul

Re: What do you write when learning a new language?
by hsmyers (Canon) on Jun 05, 2008 at 06:51 UTC
    I write some aspect of a PGN parser. PGN stands for Portable Game Notation— an ASCII text standard for chess notation. I always finish the parser, it is the extras(postscript output, English to algebraic, etc.) that may or may not get done. Usually depends on the order I choose and how much time I devote. My first was BNF on an 8 bit machine. It did't fit the pattern of a full scale language and the result reflected that, but it was the basis for a set of 'C' utilities. Then various dialects of lisp, python, ruby, perl, und wie so...

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re: What do you write when learning a new language?
by roboticus (Chancellor) on Jun 05, 2008 at 13:49 UTC
    kyle:

    My favorite "first program" to write in a language is the Knight's Tour. My reasons are basically that it's easy to implement and since I don't often learn more than one language a year, it also helps me keep track of how much faster computers are getting. (I make the board size a parameter, and have it keep solving larger and larger boards until I'm bored watching it.)

    Once I've written it, I look around and see if I can make it more idiomatic for the language. (Why write in the C subset of a language? If that's what you want, just use C.) Some languages offer the opportunity to use a different data structure for the board or other implementation strategies. After I get it running nicely, I try to see if I can code up new heuristics to chop off subtrees. It's often a fun way to spend a day when you need to relax.

    I don't know exactly how many versions I've written, but it's likely between 25 and 35. (Not counting the hacking sessions of optimizing and idiomizing, etc. If I were to count those, it could possibly be well over 500.)

    I just realized something ... I've never done it in perl. If I weren't at work right now, I'd do that. As it stands, I think I'll do it this evening and post it tonight or tomorrow.

    ...roboticus
Re: What do you write when learning a new language?
by TStanley (Canon) on Jun 04, 2008 at 20:39 UTC
    I usually try to implement the Fisher-Yates shuffle. Its a neat little algorithm, that is usually easy to implement.

    TStanley
    --------
    People sleep peaceably in their beds at night only because rough men stand ready to do violence on their behalf. -- George Orwell
Re: What do you write when learning a new language?
by sasdrtx (Friar) on Jun 06, 2008 at 12:18 UTC
    I have a little calendar and events display that started out as an ISPF application written in PL/I back in the mid 1980s. Later I rewrote it in C, and made it generate HTML as a CGI app. Later on, it was ported to Perl, and oddly, also forked into a C++ program. For each version, what started out as a "word-for-word" translation was refactored and reworked to match the features and idioms of the new language. However, I haven't gotten around to making it "use strict" yet. Once I do, maybe I'll submit it for a scathing review in the monastery. :-)

    It has indeed been very useful for learning new languages for me... I can start with what's common with a language I already know, and incrementally add features and techniques as I learn them. And it lets me concentrate on the language, instead of solving a new problem. Which would be the next step.

    Here's a site that shows it working: http://sas-crash.homelinux.net/sas/cgi-bin/logon9c.pl. Bug reports are welcome.

    Update: fixed the link.. I swear it was good when I put it in.


    sas
Re: What do you write when learning a new language?
by kgraff (Monk) on Jun 05, 2008 at 17:20 UTC

    I start with "Hello World" then cut to the chase with what I need to do with the program. Useless politeness?

Re: What do you write when learning a new language?
by Ehtyar (Initiate) on Jun 11, 2008 at 02:57 UTC
    Being a windows user (and not quite the math wizz everyone else in this thread seems to be) I will usually write a Hello World (preferably in multiple ways if possible) then move on to a temp file cleaner that deletes all files older than a week, and any empty folders resulting from the file deletion. Ehtyar.
Re: What do you write when learning a new language?
by vrk (Chaplain) on Jul 02, 2008 at 17:59 UTC

    I program because I have problems. (No, not that kind of problems.) There is some task I can make the computer do for me, so I start programming. Picking the programming language comes after having a problem to solve. Most often it's Perl, since it's a pragmatic programming language and available on most computers I have access to (including in particular $work). Sometimes I pick C if I need better access to hardware or libraries or manual memory control. Sometimes I pick $functional_language[rand @functional_language] because the solution may be more easily expressed in it. Sometimes the environment the program must execute in limits the choice (e.g. Javascript).

    Of course, this kind of an approach presupposes that you know the programming language very well, or at least are familiar with a wide range of different concepts.

    When I was still taking first steps into programming in Pascal and C, I used to write a program that queries the user for a radius, then prints the circumference and area of a circle or sphere. But that was a decade ago.

    I mean no offence, but "learning how to write a subroutine or function" in a new programming language sounds silly. It's a matter of syntax, if the programming language has some form of functions or goto. (If it doesn't, this is an interesting problem, but it was already solved in 1950s.) It's much more worth your while to learn a new concept through exposure to a new programming language.

    --
    print "Just Another Perl Adept\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://690164]
Approved by Arunbear
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2024-04-19 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found