Sim
Sim

Reputation: 4185

how to remove quotationmarks of strings inside a backquote

Imagine I got some kind of function called by a macro to return an expression like

(do-something 'argument)

therefore the function has to look like

(defun form-expression (arg)
  `(do-something ',arg))

But when called (form-expression "foobar"), as the wrapping macro has to receive strings as parameters, the result is:

(do-something '"foobar")

How can I "remove" the quotation marks of the given string?

Upvotes: 1

Views: 1284

Answers (2)

Rainer Joswig
Rainer Joswig

Reputation: 139251

Your question is: How to I get rid of the quotationmarks?

This question is not really useful.

1: we need to ask, why are there 'quotationmarks'?

Answer: because it is the syntax for a string.

This is a string: "buddha".

2: we need to ask what do we have without 'quotationmarks'?

Answer: symbols.

This is a symbol: buddha.

3: if we have a string, how can we create or get the corresponding symbol?

We need to use INTERN or FIND-SYMBOL.

> (intern "buddha")
|buddha|

4: why has it vertical bars?

By default all symbols are internally in uppercase (in the early days there were a lot of computers with only uppercase characters and Lisp has been discovered long before the big flood).

So Common Lisp use the vertical bar to escape symbols when they have non-uppercase characters.

5: how can I get a normal internally uppercase symbol?

Use the function STRING-UPCASE:

> (intern (string-upcase "buddha"))
BUDDHA 

Now we have interned a large buddha.

Upvotes: 12

Inaimathi
Inaimathi

Reputation: 14065

If I'm understanding you correctly, for an input of "foobar", you want the result of form-expression to be (do-something 'foobar).

If that's the case, use the intern function. You'll either need to pass the interned string (as in (form-expression (intern "foobar"))), or you'll need to do type dispatch in the function

(defun form-expression (arg)
   `(do-something ',(if (stringp arg) (intern arg) arg)))

Actually, since you're dispatching on the type of the argument, this is a perfect place to use a method.

(defmethod form-expression (arg) `(do-something ',arg))
(defmethod form-expression ((str string)) `(do-something ',(intern str)))

Upvotes: 2

Related Questions