Monday, March 21, 2005

Concision is equivalent to powerfulness

Personalization is a key feature of Common Lisp. Eventually, you are not writing Lisp, you are writing a custom language for solving your problem, and then writing in that custom language to solve your problem. I haven't gotten to the problem solving part yet, but I have been making great progress in language customization. (In fact I like to think of myself as the lead coder on the BruceLisp project, which is probably a life-long endeavour!)

There are many long function names in Lisp: MULTIPLE-VALUE-BIND, WITH-OUTPUT-TO-STRING, CHAR, etc. I have written a mkalias macro that lets me redefine certain Lisp functions to shorter, more aesthetically pleasing values. Here are a few examples:
  • with-open-file => w/file (similar fixes for other with-macros)
  • make-array => mkbuf (I always consider my arrays to be buffers for things, so many array functions have aliases with "buf" in the name. the only glitch is the clash between bref as "buffer reference" and my bref macro, "beautiful aref".)
  • char => chr
Now I can write:
(defun filestr (file)
(w/file (strm file)
(let-1 buf (mkbuf (strmlen strm) 'chr)
(dotimes (i (strmlen strm) buf)
(setf (chr buf i) (readchr strm))))))
See? Much more concise, and all within the limitations of Common Lisp (except for strmlen, which shells out to the "stat" shell command to get the file length).

8 Comments:

Anonymous said...

Sounds a little too much like C macro useage to me. Super.

11:50 AM  
#f said...

Your suggestion is nice, but it looks somehow retarded to me: you are moving the language in the direction of C! That's so WRONG. What you should be doing instead is moving toward a more Lispy, more metaprogramming way of doing things.

To read a file into a buffer, you should instead spawn a perl script that builds a Lisp file foo.lisp that you may LOAD afterwards. The Lisp file will contain a MAKE-ARRAY statement.

Note that you can also speed things up by pre-compiling the file to a FASL before you LOAD it!

6:24 PM  
Anonymous said...

"beautiful aref"? Sounds like you've eliminated another "onion". :-)

Let's see the code!

7:33 PM  
Anonymous said...

The irny is kllng me.

7:51 AM  
Anonymous said...

You can write a macro to facilitate this beautification:

(defmacro symbol-abbrev (symbol)
(intern (remove-if (lambda (c) (find (char-downcase c) "aeiou"))
(symbol-name symbol))))

See--macros are what makes lisp so powerful!

7:58 AM  
Anonymous said...

Yeah, I started doing this too after seeing some of Gene Michael Stover's stuff. Do you know about his site? http://www.lisp-p.org? If you haven't been there yet, he's got lots of articles and lisp source to learn from.

1:10 PM  
Anonymous said...

What you do is a usability/maintainability nightmare: it makes your code harder to read and maintain without having real advantage of beeing more expressive and extending the language to narrow the gap between your code and the problem domain.

The fact that Lisp is one of the most concise languages does not depend on the length of the names, but on the expressiveness of the language!

10:34 AM  
Anthony said...

"Concision is equivalent to powerfulness" is a sentiment I've heard before. As occasional APLer, I believe it's true at least part of the time. However, it echoes the lesson of Strunk and White: "Omit Needless Words".

Applying your dictum to itself a la Strunk and White, we get:

"Concision is power".

Just a suggestion :-)

10:19 AM  

Post a Comment

<< Home