Perl's qw as a macro
Perl's qw as a macro
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
(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
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.
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.
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: Perl's qw as a macro
Use either string or symbol-name to transform a symbol into a string. The rest shouldn't be too hardKwaltee 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
Re: Perl's qw as a macro
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.
(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.
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: Perl's qw as a macro
The symbol's name is by default uppercase. If you want symbols with different behaviour, you have to use the || markers. For instance:
Either use string-downcase or put || around your symbols to make them lowercase.
Code: Select all
(intern "FOO") => foo
(intern "foo") => |foo|
'|FOO| => foo
'|FoO| => |FoO|
Re: Perl's qw as a macro
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))
Re: Perl's qw as a macro
You can't do what you're trying to do.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")
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
Ah, that makes sense. Thanks much.
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: Perl's qw as a macro
There is no problem of him trying to do it, since it is an exercise.Paul wrote:You can't do what you're trying to do.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")
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
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.