4

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 ?

2
  • 1
    It's not exactly the same, but I think stackoverflow.com/q/24433035/1281433 may have an answer for you, or at least some helpful tips. Commented Mar 28, 2016 at 13:23
  • @JoshuaTaylor Thank you. Commented Mar 29, 2016 at 11:52

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Note that passing strings as suffix is going to have problems with readtable's whose case is modified
string-concat does not seem to be common-lisp. I use: (concatenate 'string ...etc)
@user115813 string-concat is imported into common-lisp-user in CLISP, but it's not a part of CL package. Changed to concatenate.
Thank you very much. I get it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.