in reply to Take the Arc Challenge

And they say perl is obfuscated!!! I liked almost every other example in that thread far better, what good does concise do if you have to learn a whole knew language to read it? I don't program Python or Ruby but the examples in those languages should make sense to any programmer. Below is my perl code, i think its easier to read, easier to expand, and easier to understand than the arc example. The only thing is it isn't shorter! Should I re-write it in a compact form like his? That seems insane.

#!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Session; my $cgi = new CGI; my $session = new CGI::Session(); print $session->header(); $session->param('name', $cgi->param('name')) if ($cgi->param("name")); $session->clear('name') if ($cgi->param("c")); if ($cgi->param("m") eq 'show') { print "You said:", $session->param("name"), " ", $cgi->a({href +=>'?c=1'},'Start Over'); } elsif ($session->param("name")) { print $cgi->a({href=>'?m=show'},'See what you said'); } else { print $cgi->startform, $cgi->textfield('name'), $cgi->submit(' +Submit'), $cgi->endform; }


___________
Eric Hodges

Replies are listed 'Best First'.
Re^2: Take the Arc Challenge
by Joost (Canon) on Feb 05, 2008 at 11:36 UTC

      I don't think it is worth learning a whole new language just for brevity, I think in the end more time will be spent learning all those strange shortcuts than is saved by how short it is. I wonder if a regular Lisp program would be easier to read or if my confusion more centered around Lisp than Arc.

      If you consider that pretty easy to read then could you explain the example to me? Because I have no idea where its saving stuff in a sessions, where its generating HTML, whats responding to each HTML request, etc. I've never learned lisp and maybe thats the problem.


      ___________
      Eric Hodges
        A couple of things help:

        1. Arc is based on lisp/scheme. Lisp function and macro calls are normally of the form (functionname arg1 arg2 ... argn).

        2. the only syntactically new thing in this example (compared to standard lisp/scheme) is the [  _ ] construct.

        [  ... _ ... ] creates a function (like an anonymous sub as in perl) that takes one argument "_". Everything between the brackets is the function body.

        2. The w/link construct doesn't put data in the session, but a closure. In other words, it constructs a link, which when clicked will retrieve the provided function and execute it. This makes the code extremely brief. Look up continuation-based web programming.

        3. I would assume defop registers a function with a URL, and that input and submit are functions that generate form fields. I haven't looked into the web-based stuff too deeply yet.

        4. pr prints.

        See also http://ycombinator.com/arc/tut.txt

        Update: lisp and scheme are neither build with the focus on brevity (though scheme thends to have shorter names for built-in functions) but either will allow you to write code that is very compact, because you can build your own language constructs right on top of the base language.

        Depending on your point of view, Arc is not a language, it's just a scheme library written in scheme (though a pretty drastic one).

        Update 2: the perl/Continuity example is actually very close the the original Arc code.