Perl's qw as a macro

Discussion of Common Lisp
Kwaltee
Posts: 4
Joined: Tue Jul 14, 2009 9:08 am

Perl's qw as a macro

Post by Kwaltee » Tue Jul 14, 2009 10:06 am

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

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Perl's qw as a macro

Post by nuntius » Tue Jul 14, 2009 6:12 pm

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.

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Perl's qw as a macro

Post by gugamilare » Tue Jul 14, 2009 8:17 pm

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 ;)

Kwaltee
Posts: 4
Joined: Tue Jul 14, 2009 9:08 am

Re: Perl's qw as a macro

Post by Kwaltee » Wed Jul 15, 2009 6:35 am

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.

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Perl's qw as a macro

Post by gugamilare » Wed Jul 15, 2009 8:25 am

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.

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: Perl's qw as a macro

Post by Jasper » Wed Jul 15, 2009 1:12 pm

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 :).

Paul
Posts: 106
Joined: Tue Jun 02, 2009 6:00 am

Re: Perl's qw as a macro

Post by Paul » Wed Jul 15, 2009 10:08 pm

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 "(" :)

Kwaltee
Posts: 4
Joined: Tue Jul 14, 2009 9:08 am

Re: Perl's qw as a macro

Post by Kwaltee » Thu Jul 16, 2009 4:42 am

Ah, that makes sense. Thanks much.

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Perl's qw as a macro

Post by gugamilare » Thu Jul 16, 2009 6:26 am

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.

Kwaltee
Posts: 4
Joined: Tue Jul 14, 2009 9:08 am

Re: Perl's qw as a macro

Post by Kwaltee » Thu Jul 16, 2009 7:37 am

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.

Post Reply