Page 1 of 2
Perl's qw as a macro
Posted: Tue Jul 14, 2009 10:06 am
by Kwaltee
I am trying (again) to wrap my head around macros. As an exercise, I am trying to write Perl's qw function as a macro.
(qw this is a test) should expand to '("this" "is" "a" "test")
Unfortunately, I'm having difficulty getting at the words in the form. Lisp keeps looking for a symbol called "this" or "a" and not finding it. I'd appreciate some pointers that will move me in the right direction, as I've managed to so confuse myself that I don't even know where to start looking any more.
Thanks
Re: Perl's qw as a macro
Posted: Tue Jul 14, 2009 6:12 pm
by nuntius
You could type in your output as (list "this" "is" "a" "test")
You can use FORMAT to convert a symbol or number or whatever into a string.
So your macro should return a list of 'list followed by the strings.
Re: Perl's qw as a macro
Posted: Tue Jul 14, 2009 8:17 pm
by gugamilare
Kwaltee wrote:I am trying (again) to wrap my head around macros. As an exercise, I am trying to write Perl's qw function as a macro.
(qw this is a test) should expand to '("this" "is" "a" "test")
Unfortunately, I'm having difficulty getting at the words in the form. Lisp keeps looking for a symbol called "this" or "a" and not finding it. I'd appreciate some pointers that will move me in the right direction, as I've managed to so confuse myself that I don't even know where to start looking any more.
Thanks
Use either
string or
symbol-name to transform a symbol into a string. The rest shouldn't be too hard
Re: Perl's qw as a macro
Posted: Wed Jul 15, 2009 6:35 am
by Kwaltee
Here's what I have so far:
(defmacro qw (&rest r)
"Mimic perl's qw function"
(cons 'list
(iter (for s in r)
(collect (string s)))))
The only problem with that is it upcases the string, and I don't want that. Symbols are always uppercase, so maybe this is unavoidable, but I keep thinking there has to be a way to fix it.
Re: Perl's qw as a macro
Posted: Wed Jul 15, 2009 8:25 am
by gugamilare
The symbol's name is by default uppercase. If you want symbols with different behaviour, you have to use the || markers. For instance:
Code: Select all
(intern "FOO") => foo
(intern "foo") => |foo|
'|FOO| => foo
'|FoO| => |FoO|
Either use
string-downcase or put || around your symbols to make them lowercase.
Re: Perl's qw as a macro
Posted: Wed Jul 15, 2009 1:12 pm
by Jasper
Code: Select all
(defun downcase-symbol-name (symbol)
(string-downcase (symbol-name symbol)))
(defun downcase-symbol-name-list (symbol-list)
(mapcar #'downcase-symbol-name symbol-list))
You can find stuff like this
here, or better, add a search keyword for your browser to
this search. Of course going from documentation to using/understanding the functions is a skill on itself, so feel free to keep asking
.
Re: Perl's qw as a macro
Posted: Wed Jul 15, 2009 10:08 pm
by Paul
Kwaltee wrote:I am trying (again) to wrap my head around macros. As an exercise, I am trying to write Perl's qw function as a macro.
(qw this is a test) should expand to '("this" "is" "a" "test")
You can't do what you're trying to do.
Well, obviously, you
can, but it won't be what you want. The reader has already converted the characters into symbols; using format, or princ-to-string, etc., to turn them back into strings is just wrong...and will break easily - don't do it. You can use a reader macro, but you probably don't want to name it "("
Re: Perl's qw as a macro
Posted: Thu Jul 16, 2009 4:42 am
by Kwaltee
Ah, that makes sense. Thanks much.
Re: Perl's qw as a macro
Posted: Thu Jul 16, 2009 6:26 am
by gugamilare
Paul wrote:Kwaltee wrote:I am trying (again) to wrap my head around macros. As an exercise, I am trying to write Perl's qw function as a macro.
(qw this is a test) should expand to '("this" "is" "a" "test")
You can't do what you're trying to do.
Well, obviously, you
can, but it won't be what you want. The reader has already converted the characters into symbols; using format, or princ-to-string, etc., to turn them back into strings is just wrong...and will break easily - don't do it. You can use a reader macro, but you probably don't want to name it "("
There is no problem of him trying to do it, since it is an exercise.
Re: Perl's qw as a macro
Posted: Thu Jul 16, 2009 7:37 am
by Kwaltee
It was an exercise, true, but I actually have a use for it in mind. As a learning exercise, this was good. As a path to making a portion of my program easier to read, not so much! Thanks to all who provided guidance. I appreciate your taking the time.