(show&tell) What Are Words For?

Started by m i c h a e l, April 27, 2006, 08:44:51 PM

Previous topic - Next topic

m i c h a e l

Hello again. Time for more show and tell.



Wrote my first macro in newLISP. Actually, this was only my third macro to date. I wrote one in Scheme and one in Common Lisp. How's that for symmetry?



When I mentioned in the  http://www.alh.net/newlisp/phpbb/viewtopic.php?t=1068">Gotcha post that I'd translated all of my old code into newLISP, I of course was lying for dramatic effect :-) With still mountains of code yet to newLISPify, I began rewriting one of the classes I'd written in Ruby. It's called FullName and one of its methods, prefix?, uses Ruby's %w to make a list of strings from unquoted words. This is then matched against the argument to see if it is a prefix:



def prefix?(s)
%w(Dr. FraĆ¼lein Frau Herr Madame Mademoiselle Miss Mlle. Mme. Monsieur Mr. Mrs. Ms. Prof. Professor Senor Senora Senorita Sr. Sra. Srta.).member?(s)
end
# N.B. absent most prefixes


I bet I could write a macro to do what Ruby's %w does, I thought. So I did. But not without a few hiccups along the way.



The first attempt:

(define-macro (words)
(map
(fn (ea)
(eval (string ea)))
(args)))


... seemed to work right out of the box (isn't newLISP swell?), but just try using that within a context and see what happens. The context is prepended to each of the strings! Back to the drawing board.



I remembered reading about name in the manual, and this seemed like the right place to use it. After some fumbling and bumbling and realizing that the eval wasn't necessary, I ended up with these 5 lines:



(define-macro (words)
(map
(fn (ea)
(string (name ea)))
(args)))


Now (words this is the way we wash our clothes) returns

("this" "is" "the" "way" "we" "wash" "our" "clothes")
in or out of a context.



That was a fun ten minutes. I wonder why I thought Ruby's %w was so cool? Oh yeah! It must be the %w. No, wait.


(constant '%w words)

 

:-)



m i c h a e l

cormullion

#1
Another useful post! Thanks.



I see that you've got a mountain of code, including some Ruby. I'd be interested to know how you're finding the translation, and why you've decided to move some of it to newLISP. I quite liked Ruby when I tried it, although I prefer the elegance of newLISP.



Perhaps you have another place where you write about this sort of thing?



BTW, can you do this?:


(define-macro (%w) (map (fn (ea) (string (name ea))) (args)))

m i c h a e l

#2
Quote from: "cormullion"Another useful post! Thanks.


You're welcome :-)


Quote from: "cormullion"I'd be interested to know how you're finding the translation, and why you've decided to move some of it to newLISP.


So far, the translation has been rather straightforward. I like to rewrite some of my not-too-vital (but sometimes even vital!) code in the language under study to better understand it and to have the familiarity of the original code as a comforting guide. This seems to help speed up the learning process, and it gives clues into the areas I'm ignorant of in the new language. Also, I try not to do straight translations, but to utilize the idioms and constructs appropriate to the new language.



As is plain in the above, I tend to do this process for each and every language I study, so the decision to move my old code over to newLISP was practically a given. But in newLISP's case, I think I may have found the balance I've been looking for. Not too verbose, not too terse. Feels modern and quick. Unobtrusive and malleable. But, best of all, we won't have to hear that our language is only some pathetic distance down that path to a complete LISP implementation. Or did those curmudgeons in the Common Lisp camp already do that ;-)


Quote from: "cormullion"Perhaps you have another place where you write about this sort of thing?


Sadly, no. We (my wife and I) are working on getting something up soon (perhaps with the help of newLISP Wiki!), but right now we only have the cafepress site. Can you believe that someone who first learned to use the Internet with Archie and Gopher has never had a web site!


Quote from: "cormullion"BTW, can you do this?:

(define-macro (%w) (map (fn (ea) (string (name ea))) (args)))


Yes. Actually, that's the same implementation I arrived at for words. I was just trying to be funny by making an alias called %w to the original macro. Guess I'd better stick with art :-)





m i c h a e l