(archive 'newLISPer)

October 21, 2009

Syntax matters

Filed under: newLISP — newlisper @ 22:22
Tags:

My fellow newLISP blogger Kazimir observes in his poll analysis that one of the main reasons why people don’t use Lisp is the syntax:

Lisp syntax is the most important reason for majority of people who do not use Lisp.

The thing I don’t understand, though, is what exactly is wrong with Lisp syntax that makes people avoid the language. Perhaps that’s another research opportunity.

Here’s a simple scripting problem. Given a text file containing a series of random paragraphs, separated by percent signs, sort them so that they are ordered according to the first significant word in each paragraph. That is, words such as “a” and “the” shouldn’t affect the sort order.

Here’s one solution:

#!/usr/bin/perl -w

my %sort_buckets;
my %exclusions;

my $file_to_sort = '/path/to/file_to_sort';
my $sorted_file = '/path/to/sorted_file';

while () {
    chomp;
    $exclusions{$_}++;
}

open $in, "<", $file_to_sort
    or die "Can't open file: $!";

$/ = "%\n";

while () {
    my $line = $_;
    my @words = split " " => $line;
    my $sort_key = '';
    for (0..$#words) {
        if ($exclusions{lc($words[$_])}) {
            next;
        } else {
            $sort_key = join "" => map { lc($_) } @words[$_..$#words];
            last;
        }
    }
    $sort_buckets{$sort_key} = $line;
}

close $in;

open $out, ">", $sorted_file
    or die "Can't open file: $!";

foreach (sort keys %sort_buckets) {
    print $out $sort_buckets{$_};
}

close $out;

__END__
a
the
this
that
you
when
is
may
be
if
and

I’m not sure I know what’s going on there, but it’s a typical practical (and probably quick) solution from a Perler. For comparison, here is a newLISP version:

#!/usr/bin/env newlisp

(set 'common-words
  '("a" "the" "this" "that" "you" "when" "is" "may" "be" "if" "and"))

(define (remove-common text)
    (difference
       (find-all {[a-zA-Z]+} (lower-case text))
        common-words))

(define (compare text1 text2)
    (< (remove-common text1)
       (remove-common text2)))

(write-file {/path/to/sorted-file.txt}
    (join (sort
             (parse (read-file {/path/to/file_to_sort.txt}) "%")
             compare)
     "%"))

It’s not clear to me why this syntax is considered unappealing, or indeed how it could be improved. I’ve written about this before, so obviously I haven’t sought hard enough for an answer.

Can we measure some aspect of these two files? How about looking at the use of alphabetic characters and parentheses. For the Perl example:

Parentheses: (7 7)
Braces: (11 11)
Brackets: (2 2)
Alphabetic: 403
Non-alphabetic: 642
ratio 0.6277258567

And the newLISP example:

Parentheses: (17 17)
Braces: (3 3)
Brackets: (1 1)
Alphabetic: 256
Non-alphabetic: 168
ratio 1.523809524

The preponderance of the Lisp parentheses is apparent. But otherwise, the Lisp example is plainly much more alphabetic, more readable, more civilized. It’s just possible that the non-mathematical syntax, and the lack of familiar and friendly “x = x + 3″ forms (as learned by schoolchildren from 10 years and upwards) are enough to disarm and perplex the non-Lisper.

About these ads

5 Comments »

  1. I like Lisp *especially* because of its syntax.
    To me, it is still the most beautiful language around (putting aside all the other aspects), really because of its “aesthetics”.
    And I feel parenthesis are a big part of that “feeling”.

    When I read well written Lisp code I feel my awareness of the subject gets cleaner and tidier and lighter. I never experienced a similar thing with other languages. I am not belittling them, or criticising. It is just a deep personal feeling, that has been lasting since university times, back in the 90s.

    Thank you so much for the nice blog!

    Comment by Guido — August 23, 2011 @ 18:37 | Reply

    • Thanks. I hope some of the code here has some elegance…!

      Comment by newlisper — August 23, 2011 @ 18:42 | Reply

      • Your code is VERY nice. Elegant, terse but very readable, and just beautiful.

        I just hope that you will give us more and more of it!

        Thank you!

        PS: If you need some “inspiration” to write some code tidbits , please let me know. I have decided to learn newlisp as my first lisp, and I do have a few request I would love to submit for your coding consideration. All not trivial, I promise :)

        Comment by Guido — August 24, 2011 @ 16:02

    • I also like Lisp especially because of its s-expression syntax.
      Car and Cdr are probably derived from old old architectures where early lisp was implemented. Car means `Common Address Register”. Maybe it was even an assembly command that happened to push data into said register, and this was where the beginning of a list happened to be stored. Cdr was some other register on that architecture.
      And newlisp rocks especially as it takes the qualification of code as data most serious. Fexprs and eval all the way! Picolisp goes a similar way yet in a different style.

      Comment by schillingklaus — June 23, 2012 @ 17:52 | Reply

  2. Sorry to bump an old post, but as a non-Lisper who has always been intrigued with the language, I feel like I can comment:

    Narrative and story are deeply embedded in the human race. Formal mathematics, on the other hand, is a very recent bolt-on to the human psyche. For example, the number of romance novel readers greatly exceeds the number of people who solve math puzzles for fun.

    Similarly, I think most programmers find a language syntax that encourages narrative (such as Ruby) approachable, but are put off by the cold mathematical abstraction of Lisp. The ‘superfluous structure’ that Lisp lacks is actually vital to telling the story of ‘how the paragraphs get sorted by their first significant word’. Of course, this applies much less to the ‘mathlete’ crowd, who have a natural affinity for the terse, big-giant-formula feel of Lisp.

    But, I feel the Lispers have a valid point: the same narrative-style syntax that many find so helpful for solving easier programming tasks becomes an impediment when tackling harder, increasingly abstract problems.

    Some other (less valid) objections:
    * My first response to seeing Lisp code samples was ‘I am going to spend my life counting parenthesis to see where my syntax error is’. But a modern syntax-highlighting editor makes this task much easier.
    * Car, Cdr and Cons — every Lisper knows what these cryptic words mean, but no one wanted to explain them to outsiders. newLISP mercifully uses ‘head’ and ‘tail’ for ‘car’ and ‘cdr’, and the manual has a clear explanation of cons.
    * Forums filled with hyper-intellectuals who are happy to endlessly argue minutia, but seem to have little interest in getting things done. Again, the newLISP forums are a refreshing change of pace.

    After going through the materials, I am convinced that newLISP is an excellent introductory Lisp for the newcomer, and has the potential for a wide range of practical uses.

    Comment by kismert — November 18, 2011 @ 03:08 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Rubric Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: