dorai@titan.rice.edu (Dorai Sitaram) (03/15/89)
Could someone please tell me how to define brackets ("[", "]") as macro characters in CommonLisp so that they have the same effect as the usual parens (but make for more readable code)? E.g., (let ([x 2]) (+ x x)) should parse as (let ((x 2)) (+ x x)). Since this is about the only place where I'll ever (?) have to mess with tricky readmacros, and the chapter on Input/Output in CLtL is way too humongous right now, I request indulgence for not figuring it out for myself. Email would be divine. --dorai
raymond@ptolemy.arc.nasa.gov (Eric A. Raymond) (03/15/89)
In article <2848@kalliope.rice.edu> dorai@titan.rice.edu (Dorai Sitaram) writes: >Could someone please tell me how to define brackets ("[", "]") as >macro characters in CommonLisp so that they have the same effect as >the usual parens (but make for more readable code)? Here goes, but I bet you'll never use them if you let your editor do your formatting (and are too lazy to rewrite the indenter). ;;; This may reference LISPM specific code (defun bracket->list-reader (stream sub-char) "This allows you to use the characters [ and ] to delimit lists. This is especially useful when using nested list structures. (i.e. (let ([this 1] [that 2]) (+ this that)) ) One drawback is that the editor will not flash the opening/closing bracket like it will a parenthesis. Also, it will not catch unbalanced brackets or indent some special forms properly. Hint: Use brackets for inner most forms (i.e. (let ([a 1]) ) not (let [(a 1)])) Do NOT mix parenthesis and brackets." (declare (ignore sub-char)) (values (read-delimited-list #\] stream t))) (set-macro-character #\[ #'bracket->list-reader T) (set-macro-character #\] (get-macro-character #\))) Here's something for progn's: (defun curly-brace->progn-reader (stream sub-char) "This allows a shorthand for progn (ala C). Uses balance pairs of {} to delimit progns." (declare (ignore sub-char)) (list* 'progn (read-delimited-list #\} stream t))) (set-macro-character #\{ #'curly-brace->progn-reader T) ;;; This prevents } from being part of form (set-macro-character #\} (get-macro-character #\))) -- Eric A. Raymond (raymond@pluto.arc.nasa.gov) Nothing left to do but :-) :-) :-)