Quotation mark in lisp code macro output [closed] - macros

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can i get something like:
(my-macro name) => (foo "foo-transformed-arg-name")
I only obtained
(foo \#" foo-transformed-arg-name \#")
How can i avoid the #" in my macro output?

(defmacro foo (sym)
(symbol-name sym))
or
(defmacro foo (sym)
(string-downcase (symbol-name sym)))
I don't get why you would need any macro like this, but you will know best.

This is probably what you want to do:
(defmacro static-string (func &rest characters)
`(,func ,(coerce characters 'string)))
Perhaps it may make sense if you need to define some string constants instead of creating them at run time...
And this is the only way I can think of you being able to arrive at your current result:
(defmacro quote-symbol-quote (func symbol)
(list func #\" symbol #\"))
Stackoverflow highlighting is misleading. What you are getting in the result is a function called with three arguments: the quote character, the symbol foo-transformed-arg-name and the quote character again.

The resolution was very simple. My real probem was how can I convert mi argument symbol of a macro to an string.
(my-macro foo) => (other-func "foo")
That is convert the sybol foo to "FOO".
For that i used the symbol-name function

Related

Define a subset of Racket [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Since Racket is well-known for his ability to create new programming language, the following shouldn't be too difficult.
I would like to create a subset of Racket (let's name it min) for an educational purpose renaming some function (tail instead of cdr) and ignoring others (such as string=? and = to just have equal?).
The first step I made is to start my min files with #lang s-exp "min.rkt" but I am stuck at the expander stage.
The following module is an example of a minimal subset of Racket that allows top-level definitions, constants, and arithmetic using + and *:
;; min.rkt
#lang racket/base
(provide #%module-begin #%top-interaction
#%app #%datum #%top
define + *)
Here's what the provides mean:
#%module-begin must be provided by a module for that module to be considered a "language"; it determines what a module body means. You can just reuse Racket's module-begin macro. (The #%module-begin export gives you a hook to implement non-local constraints or transformations. For example, if you wanted to add a typechecker or check that variables are defined in alphabetical order, you could do that in the module-begin hook.)
#%top-level is necessary for interactive languages. If you leave it out, you can't use a REPL for your language (eg, with racket -t "min.rkt" -i).
#%app and #%datum make function application and self-evaluating constants (like numbers and booleans) work.
#%top makes forward references work at the REPL, like in mutually-recursive functions. You still must define a name before you evaluate a reference to it, of course.
The rest of the exports are the special forms and functions you want to include in your language.
Here's a program in this "min.rkt" language:
#lang s-exp "min.rkt"
(define x 2)
(define y (+ x 5))
(* y 7)
(define (f x) (+ x x 1))
(f 8)
Note that since the language includes Racket's define, it allows function definition, even though the language doesn't include lambda. If you wanted a restricted version of define, you would have to define your own macro and provide it as your language's define, like (provide (rename-out [my-define define])).
You can also use rename-out to provide Racket's cdr as tail, but the procedure would still print as #<procedure:cdr> and if it raised an error the error message would still say cdr. To change that, you'd need to define your own wrapper function that does its own error checking.

Adapting UCI lisp function with a loop to common lisp [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm currently trying to recreate an old program written in UCI Lisp using Common lisp but I'm not very fluent with Lisp.
The original function is:
(DE SETROLE (ROLE FILLER CD)
(CONS (HEADER:CD CD)
(APPEND (FOR (PAIR IN (ROLES:CD CD))
(WHEN (NOT (EQUAL (ROLE:PAIR PAIR) ROLE)))
(SAVE PAIR))
(LIST (LIST ROLE FILLER]
Here's my common lisp interpretation:
(defun setrole (role filler cd)
(cons (header/cd cd)
(append (loop for pair in (roles/cd cd)
do (when (not (equal (role/pair pair) role))
(save pair)))
(list (list role filler)))))
This is the error that comes up:
*** - EVAL: variable WHEN has no value
My initial thinking is that there is no equivalent 'save' function. The description of the function is:
SETROLE Makes a new CD form with (ROLE FILLER) added or replacing the old (ROLE ...) pair.
Kindly help, I'm stumped
I found the bug. There was a loose 'when' in the wild below the code.

Lisp: Find the setf way of doing the equivalent of fset [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Find the setf way of doing the equivalent of fset in lisp.
(symbol-function 'foo) is a Generalized Variable in elisp, so you can use:
(setf (symbol-function 'forward-word) #'backward-word)
as an alternative to:
(fset 'forward-word #'backward-word)
(As a side-note, you can do the same thing with cl-letf as a replacement for the deprecated flet when you want to override a function using dynamic scope.)

What does the `#` mean in elisp? [duplicate]

This question already has answers here:
What is the role of the # character in Emacs Lisp?
(2 answers)
Closed 7 years ago.
Quite likely this is a dumb question, but I haven't come across the # symbol in the bits of elisp I have read, and was wondering what it means (preceded by a , as well) in this code? I have had some difficulty providing the proper search phrase I think.
In case of link rot:
(defmacro zenburn-with-color-variables (&rest body)
"`let' bind all colors defined in `zenburn-colors-alist' around BODY.
Also bind `class' to ((class color) (min-colors 89))."
(declare (indent 0))
`(let ((class '((class color) (min-colors 89)))
,#(mapcar (lambda (cons)
(list (intern (car cons)) (cdr cons)))
zenburn-colors-alist))
,#body))
This is an elisp macro definition, it defines a template for code to be substituted by other code at compile time. A decent intro is chapter 7 of Paul Graham's On Lisp
http://www.paulgraham.com/onlisptext.html
Ask Emacs, by checking the index of the elisp manual:
C-hig (elisp) RET
I # RET
Follow result: * ,# (with backquote) [Index]: Backquote. (line 29)
You can also "splice" an evaluated value into the resulting list,
using the special marker ‘,#’. The elements of the spliced list become
elements at the same level as the other elements of the resulting list.
The equivalent code without using ‘`’ is often unreadable. Here are
some examples:
[...]

LISP function substitute does not substitute my string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to understand the function "substitute" in LISP, but there is something that I don't understand.
When I perform this:
(defparameter *mytree* nil)
(push 1 *mytree*)
(substitute '8 '1 *mytree*)
Everything runs ok, the value of mytree is (8).
But, when I perform:
(defparameter *mytree* nil)
(push "A" *mytree*)
(substitute '8 '"A" *mytree*)
Then mytree is ("A") instead of (8) like I expect.
Any idea why this is happening?
You need to use the correct equality checking method by using the :test keyword in substitute.
The signature for substitute is
(substitute newitem olditem sequence &key from-end test test-not start end count key)
Substitute is using eql by default, but that won't work in this scenario
(eql "A" "A") ;; nil
(equal "A" "A") ;; t
This will yield the correct results
(defparameter *mytree* nil)
(push "A" *mytree*)
(substitute 8 "A" *mytree* :test 'equal) ;; note the test, returns (8)