user6119377
user6119377

Reputation: 43

How to define a macro like defstruct?

We define a structure like this way

(defstruct point
  x 
  y)

Then we get a function point-p

It seems easy to define a macro

(defmacro mac (fun-name)
  `(defun ,fun-name ()
     t))

But how can I add "-p" as a suffix on fun-name ? I think it may be something like

(defmacro mac (fun-name)
  `(defun ,(suffix ,fun-name) ()
     t))

suffix may be a function. So how in fact can I implement this ?

Upvotes: 4

Views: 206

Answers (1)

Sylwester
Sylwester

Reputation: 48775

You simply use string to make a string out of your symbol. Do whatever you'd like with it and intern it to get an interned symbol you can use as identifier.

(defun suffix (symbol suffix)
  (intern (concatenate 'string (string symbol) (string suffix))))

(defmacro make-predicate (fun-name)
  `(defun ,(suffix fun-name '#:-p) ()
     t))
(make-predicate test)
(test-p) ; ==> t

NB: You have an error in your macro as you use the comma (unquote) inside an already unquoted expression.

Upvotes: 6

Related Questions