I was reading subr.el and saw this code:
(defalias 'backward-delete-char 'delete-backward-char)
(defalias 'search-forward-regexp (symbol-function 're-search-forward))
Interestingly, the first line doesn't use symbol-function while the second line does. The only difference I know of these two ways of using defalias is that the help for backward-delete-char (the first one) displays that it is an alias for delete-backward-char while the help for search-forward-regexp doesn't.
Is there a case when the second way is better than the first way?
The two defalias usages are slightly different. The first links the function cell for 'backward-delete-char to that of 'delete-backward-char. The second links the 'search-forward-regexp to the function that is currently called by 're-search-forward.
The difference is that if you later change the definition of 'delete-backward-char, 'backward-delete-char will now have the new behavior. Whereas in the second case, changing the function for 're-search-forward has no effect on the behavior of 'search-forward-regexp.
Perhaps some ascii art can help:
+-------------------------+ +-----------------+
|#<subr re-search-forward>| <-- |re-search-forward|
+-------------------------+ +-----------------+
^ +---------------------+
\------ |search-forward-regexp|
+---------------------+
+----------------------------+ +--------------------+ +--------------------+
|#<subr delete-backward-char>| <-- |delete-backward-char| <-- |backward-delete-char|
+----------------------------+ +--------------------+ +--------------------+
This documentation might help clear things up.
Well, it really is not the same thing... Here is a little game I just played:
(defun a () (message "hello"))
a
(a)
"hello"
(defalias 'b (symbol-function 'a))
(lambda nil (message "hello"))
(defalias 'c 'a)
a
(b)
"hello"
(c)
"hello"
(defun a () (message "howdy"))
a
(a)
"howdy"
(b)
"hello"
(c)
"howdy" ' c changed meaning, b did not...
Related
I want to run a function but not have it output the result in the terminal. For example, (set 'A 'B) normally returns B in the console like the following:
>>> (set 'A 'B)
B
>>> A
B
I don't want it to return anything; I still want the function to do what it's supposed to, just silently:
>>> (set 'A 'B)
>>> A
B
It's not perfect, but you can use (values) at the end of your expression to suppress output. You get a blank line instead.
Common Lisp:
(progn (set 'A 'B) (values))
I'm not sure of the equivalent in Scheme.
A lisp REPL always prints some return value. If you really didn't want output, you could run your code as a script in the terminal.
Example:
#!/path/to/interpreter
(set 'A 'B)
[rest of program]
Since the value printed is actually a return value of your function, and the return value of a function is the value of last expression evaluated, you can simply add an "empty" (returning e.g. "") instruction at the end of/after your call.
I came to the same solution as user1613254, however I made a macro for this (have it in my .sbclrc):
(defmacro m-ignore (fun &body body)
"ignores the return value of a function"
`(progn (,fun ,#body)
(values)))
You use it like this:
(m-ignore format t "text")
The output would be:
text
instead of:
text
NIL
which would be printed when using
(format t "text")
Is there any way by which cons can be implemented in Common LISP using list, append, first, rest etc?
In the following code
(defun my_list (&rest arguments)
`(,#arguments) ; Line 1
)
What does complete line 1 mean ?
First question: No, because cons is the building block for list and append, not the other way around. It is like trying to construct a brick out of houses.
Second question: The backquote syntax is explained in the CLHS (http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/sec_2-4-6.html).
Stylistic comments:
It is spelt "Common Lisp".
Do not use underscores to separate parts of names, but hyphens: my-list.
Do not let parentheses dangle around. Your snippet should be formatted like this:
(defun my-list (&rest arguments)
`(,#arguments)) ; Line 1
Using the backquote syntax outside of macros is usually not a good idea. In this case, it is completely superfluous:
(defun my-list (&rest arguments)
arguments)
Yes, you could in theory define cons in terms of list and append, like so:
(defun cons (car cdr) (append (list car) cdr))
I have the answer for the second question:
for the ,:
if my_symbol = 1
`(my_symbol 2 3) = (my_symbol 2 3), but with the ,:
`(,my_symbol 2 3) = (1 2 3)
The , evaluates the next symbol in a ` statement
Now for the # (which is a symbol, so it needs the , to be activated)
`(,#('a 'b 'c) ('d 'e 'f)) = ('a 'b 'c ('d 'e 'f) )
`(,#('a 'b 'c) ,#('d 'e 'f) ) = ('a 'b 'c 'd 'e 'f)
I hope these examples could help.
So the line 1 is simply extracting the arguments from a list and putting them into another one.
How about this?
(defun cons (a b) '(a . b))
Or, if you desperately need to use lists...
(defun cons (a b) (first (list '(a . b))))
For example, I pass the function name to another function
(personal-function 'func-name '(attr1 attr2 ...))
and what I want to do is
(defun personal-function (func-name)
(defun func-name '(attr1 attr2 ...) (dosomething)))
However, it said I can't defun with a symbol... What should I do?
Use
(setf (symbol-function my-symbol) some-function)
create a new function with
(compile nil (list 'lambda args body))
where args and body have meaningful values.
Solve it as follows:
e.g1
(defun create-function(a1)
(defun plus-function(x) (+ x a1)))
(create-function 2) -> PLUS-FUNCTION
(plus-function 3) ->5
e.g2
(setf (symbol-function 'printx) #'(lambda (x) (print x)))
(printx '(1 2 3)) -> (1 2 3)
Previously I also had the same problem when I defined the function.
Example:
(defun test-function(fn)
(defun fn ((lambda() (print "aaa")))))
After I run
(test-function 'aaafunction)
The evaluation result is
FN
It does not return a function named "aaafunction"...
To the person who downvote my answer:
We are newbies of Lisp, but we are working hard to learn knowledge, and you are not so respectful.
You could use a lambda
http://www.n-a-n-o.com/lisp/cmucl-tutorials/LISP-tutorial-21.html
If you're trying to make a new globally-accessible function inside a function, I don't think the language's grammar allows for that. If you create a lambda, you can initialize a variable to this lambda value and pass the variable around to your functions. In Common LISP, you can call (functionp x) to determine if a variable is a function before you try to call it.
I use defmacros for metaprogramming, for example, this emacs lisp example.
Using a macro will be a good choice. such as
(defmacro personal-function (func-name)
`(defun ,func-name '(attr1 attr2 ...) (dosomething)))
please have a try, hope that helps you.
It seems one is not supposed to quote KEYMAP when using define-key.
(define-key org-remember-mode-map "\C-c\C-r" 'org-remember-kill)
I'm confused because I think that all arguments of a function that is not quoted are evaluated, and according to the help, define-key is a function, not a macro. I don't see why the value of KEYMAP can be modified after a call of define-key.
(defun increment-value (a)
(setq a (+ 1 a)))
(setq my-num 10)
(increment-value my-num)
my-num ; ===> 10
Update: The answers explain everything, but for those still confused, let me clear up with more examples.
My increment-value example above is equivalent to this:
(let ((n 0))
(print n) ; prints 0
(let ((a n))
(setq a (+ 1 a))
(print a) ; prints 1
)
(print n) ; prints 0
)
What's going on above is, I think, similar to what's going on in this some-map example:
(let ((some-map '(1 2)))
(print some-map) ; prints (1 2)
(let ((a some-map))
(setq a (list 4 (second a)))
(print a) ; prints (4 2)
)
(print some-map) ; prints (1 2)
)
What's going on in define-key is similar to this second some-map example:
(let ((some-map '(1 2)))
(print some-map) ; prints (1 2)
(let ((a some-map))
(setcar a 4)
(print a) ; prints (4 2)
)
(print some-map) ; prints (4 2)
)
Now read the answers again with these three examples in mind and you will get it. Read also http://www.emacswiki.org/emacs/ListModification
You aren't actually changing what 'org-remember-map is (a pointer to a particular list structure), you are modifying the actual structure. Read this info page for details on modifying lists.
Specificially, if you take a look at the documentation for 'make-keymap:
(make-keymap &optional string)
Construct and return a new keymap, of
the form (keymap CHARTABLE . ALIST).
CHARTABLE is a char-table that holds
the bindings for all characters
without modifiers. All entries in it
are initially nil, meaning "command
undefined". ALIST is an assoc-list
which holds bindings for function
keys, mouse events, and any other
things that appear in the input
stream. Initially, ALIST is nil.
You'll see that keymap is a list with three elements. Let me draw that for you (yay M-x artist-mode):
org-remember-map
|
|
v
+----+----+ +----+----+
| | | --+--->+ / | \ |
+-+--+----+ +-/--+--\-+
| | |
v v v
keymap CHARTABLE ALIST
So, the value of the 'org-remember-map is something like the above structure, and when you define a key, what you are doing is changing what is pointed to in the ALIST blob part of the structure.
you're confusing value and name-value mapping.
in your increment-value function, you're not changing a's value as much as changing the mapping of the name a to a new value.
Fundamentally, there is no way to change the value of 10. 10 is 10!
But in the first case, you can either modify the mapping of the name org-remember-mode-map to a fully different map (set a new value), or you can alter the map pointed by that name (its current value). This is what define-key does.
Illustration:
(setq a '(1 2)) -> (1 2)
(setcar a 4) -> 4
a -> (4 2)
Everything you write is completely correct. The thing you are missing is that lists (keymaps are represented as lists) are not values themselves, but containers of values. Thus, you can pass a list to a function and have that function change the values of the list, but the list you have is still the same list.
All the details are in the Cons Cell Type section of the elisp manual.
How can I write the following code quickly in emacs?
\newcommand{\cA}{\mathcal A}
\newcommand{\cB}{\mathcal B}
\newcommand{\cC}{\mathcal C}
...
...
\newcommand{\cY}{\mathcal Y}
\newcommand{\cZ}{\mathcal Z}
Is there a way faster than writing
A
B
C
D
.
.
.
Y
Z
and then doing macro on each line? (changing A to \newcommand{\cA}{\mathcal A})
I agree that a keyboard macro will get the result the quickest.
More fun is a programmatic approach:
(loop
for n from (string-to-char "A") to (string-to-char "Z")
for c = (char-to-string n)
do (insert (concat "\\newcommand{\\c" c "}{\\mathcal " c "}\n")))
In general, the first time you are confronted with this kind of problem, you'd use keyboard macros, as JB already said.
The second time, check out this very very interesting article by Steve Yegge: http://steve-yegge.blogspot.com/2006/06/shiny-and-new-emacs-22.html, which contains solutions for problems exactly like yours.
For your convenience, and my own illumination, I actually went ahead and tested it:
I would start with
A
...
A
26 times
and do a
M-x replace-regexp
A
\\newcommand{\\c\,(string (+ ?A \#))}{\\mathcal \,(string (+ ?A \#))}
A to Z is only 26 lines. You'd waste more time automating generation than just naturally using keyboard macros, imho.
It depends on your background. I'd just type M-! and:
perl -e"print map {q(\newcommand{\c).$_.q(}{\mathcal ).qq($_}\n)} A..Z
Do you know the rectangle selection ?
There's also string-rectangle:
place point (the cursor) at the beginning of the text, mark (C-SPC)
place point at the beginning of the last line
type M-x string-rectangle
type a string (\newcommand{\c)
This will insert that string before each line since the mark.
I don't have enough karma or whatever to comment, but I wanted to improve HD's style a bit.
Here is the original:
(loop
for n from (string-to-char "A") to (string-to-char "Z")
for c = (char-to-string n)
do (insert (concat "\\newcommand{\\c" c "}{\\mathcal " c "}\n")))
First off, Emacs Lisp has reader syntax for chars. Instead of (string-to-char "X"), you can just write ?X. Then, you can use the printf-style format instead of char-to-string and concat to produce the final result:
(loop for n from ?A to ?Z
do (insert (format "\\newcommand{\\c%s}{\\mathcal %s}\n" n n)))
Now it's concise enough to type without thinking into the M-: prompt.
I will also point out that TeX has macros too, if this is indeed TeX.
Edit: Another bit of style advice for Joe Casadonte; (incf foo) is much easier to type than (setq foo (+ foo 1)).
Or the interactive way if you want to do them one at a time
(defun somefun(inputval)
(interactive "sInputVal: ")
(insert ( format "\\newcommand{\\c%s}{\\mathcal %s}" inputval inputval) )
)
just eval and M-x somefun every time you want the text
Not exactly an answer.
For people who don't use emacs or vim, I'd like to add that you can open Excel and use its autofill feature and text concatenation function to generate such code.
I like HD's loop a little better than mine, but here's an alternate, more generalized approach:
(defun my-append (str)
"Substitute A-Z in STR and insert into current buffer.
Looks for '--HERE--' (without quotes) in string STR, substitutes the
letters A-Z in the string and then inserts the resulting string into
the current buffer."
(interactive "sInput String: ")
(let ((lcv 0)
letter newstr)
(while (< lcv 26)
(setq letter (+ ?A lcv))
(insert (replace-regexp-in-string "--HERE--" (char-to-string letter) str t t) "\n")
(setq lcv (+ lcv 1)))))
Shouldn't be too hard to combine the two.