(archive 'newLISPer)

November 11, 2007

Looks OK to me

Filed under: newLISP — newlisper @ 18:10

I recently came across this deftly argued defence of Lisp’s use of parentheses, from François-René Rideau ‘s blog. Here’s an extract:

Firstly, the nested parenthesized syntax is actually not bad at all, as far as understanding goes. It is actually so simple that anyone can grasp it and master it in 20 minutes. Parentheses might be tricky to balance by hand, but since the 1970s at least, interactive development environments match parentheses visually for you, so you never have to worry about counting them. Moreover, properly indenting code, which may be done automatically by editors, makes code quite readable. Using Lisp with improper user interfaces meant for other languages may be tedious, but this is hardly a technical claim against Lisp.

By comparison, it took me many weeks to initially master the intricacies of the C syntax: the operator precedence, the semi-colon as terminator not separator, the confusion between assignment and comparison, the overloading of the parenthesis and comma syntax (precedence overriding, function calling, or sequencing of expressions?), the misleading (and in C++ ambiguous) similarity between declarations and statements, the trap of nested braceless if-else constructs, the trickiness of declaring type of pointers to functions, the weird limitations of the preprocessor and its macros, etc.

This interesting post doesn’t get round to addressing the aesthetic visual appeal of programming languages. Even writing that phrase sounds odd, so I expect it’s not an area which has been explored very much. Or perhaps I’ve just never thought about it much.

I think that Lisp code – more precisely newLISP code – can be pleasing to the eye. I don’t claim to be a programmer, so I have no problem about valuing a programming language for its appearance, rather than for its technical abilities or deficiencies. Once you’ve overcome your initial reaction to the omnipresent parentheses, it’s the relative scarcity of other punctuation, a more English-like vocabulary, a less algebraic feel, and a flexible approach to layout that give a relaxed and uncluttered space in which to work. At least, that’s how it feels to me. But it’s a lot to do with what you’re used to, and your own tastes and experiences. It’s a personal thing.

For example, there’s no doubt that many people would find the following easy to read and comfortable to work with. It’s a short extract from a Python script, and it should have been indented 22 characters in from the left, I think, but that’s another story:

if title:
   title_str = ' title="%s"' \
       % title.replace('*', g_escape_table['*']) \
              .replace('_', g_escape_table['_']) \
              .replace('"', '"')
else:
   title_str = ''
if is_img:
   result = '= anchor_allowed_pos:
   result_head = '' % (url, title_str)
   result = '%s%s' % (result_head, link_text)
   curr_pos = start_idx + len(result_head)
   anchor_allowed_pos = start_idx + len(result)
   text = text[:start_idx] + result + text[match.end():]
else:
   curr_pos = start_idx + 1
continue

Good stuff – compact, expressive, powerful, I would guess. But I find it visually more taxing than a similar passage from a newLISP script:

(if alt-text
     (replace {"} alt-text {"} 0))          
(if  title 
     (begin 
         (replace {"}  title {"} 0)
         (replace {\*} title (lookup {*} escape-table) 0)
         (replace {_}  title (lookup {_} escape-table) 0))           
(replace {\*} url (lookup {*} escape-table) 0)
(replace {_} url (lookup {_} escape-table) 0)          
(string 
      {}  alt-text {}))

I don’t really know why, though. Perhaps it’s because, after all this time, I’ve started to visually ‘tune out’ (odd phrase) the effect of the parentheses.

Let’s try another one. What about this? See if you can guess the language.

source = try $ do 
  char '('
  optional (char '<')
  src  \t\n")
  optional (char '>')
  tit <- option "" linkTitle
  skipSpaces
  char ')'
  return (removeTrailingSpace src, tit)

linkTitle = try $ do 
  skipSpaces
  optional newline
  skipSpaces
  delim <- char '\''  char '"'
  tit > skipSpaces >>
                notFollowedBy (noneOf ")\n")))
  return $ decodeCharacterReferences tit

I don’t know this language at all, but I slightly prefer it to the Python, and prefer the newLISP more.

Is it worth putting in a Perl extract for comparison?

$alt_text =~ s/"/"/g;
    if (defined $g_urls{$link_id}) {
        my $url = $g_urls{$link_id};
        $url =~ s! \* !$g_escape_table{'*'}!gx;
        $url =~ s!  _ !$g_escape_table{'_'}!gx;
        $result = "<img src=\"$url\" alt=\"$alt_text\"";
        if (defined $g_titles{$link_id}) {
            my $title = $g_titles{$link_id};
            $title =~ s! \* !$g_escape_table{'*'}!gx;
            $title =~ s!  _ !$g_escape_table{'_'}!gx;
            $result .=  " title=\"$title\"";
        }
        $result .= $g_empty_element_suffix;
    }
    else {
        $result = $whole_match;
    }

They say that it’s no use arguing about matters of taste. But discussion is OK, so – what’s your opinion? Does it matter what code looks like? What’s your favourite?

Comment from m i c h a e l

As an artist (a bricoleur to be exact), I’m particularly sensitive to the aesthetic qualities of a language.

newLISP is a beautiful language. And that beauty is more than skin-deep. Its simplicity is what makes it beautiful, and newLISP is simple through-and-through.

I feel another motto coming on:

newLISP: Simply beautiful :-)

m i c h a e l

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post.

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

Create a free website or blog at WordPress.com.

%d bloggers like this: