This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Perl's qw as a macro

11 posts · 6278 views

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

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

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

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

The symbol's name is by default uppercase. If you want symbols with different behaviour, you have to use the || markers. For instance:
(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

(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

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

Ah, that makes sense. Thanks much.

Re: Perl's qw as a macro

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

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.

Re: Perl's qw as a macro

The way to do this would be with a reader macro. As others have pointed out, you need to get access to the raw characters in the input stream before the standard reader fiddles with them. Thus, a standard macro won't be enough since it's always invoked after the reader has done its job. Fortunately, Common Lisp comes to the rescue with the right hooks. :D
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/