This question already has answers here:
How do I splice into a list outside of a macro in Common Lisp?
(2 answers)
Closed 7 years ago.
I have a function that returns something like this:
'(1 4 2 8)
and I want to apply the following type of functions to it:
(name &rest)
for example:
(max '(1 4 2 8))
produces an error, but:
(max 1 4 2 8)
does not.
so I have to "unlist" that list. How may I go about doing that?
The form apply is the traditional way:
(apply 'max '(1 2 3 4))
; => 4
Related
This question already has answers here:
What is the difference between Lisp-1 and Lisp-2?
(2 answers)
Closed 3 years ago.
While studying elisp I tried something which I know works in Scheme and discovered to my surprise that I couldn't replicate it in Elisp.
;; works in Scheme. result: 5
((if 1 + -) 3 2)
;; doesn't work in Elisp. result: error
((if 1 '+ '-) 3 2)
I would expect that the Elisp line evaluates to
(+ 3 2)
and that the evaluation of this list result in 5. However I get:
(invalid-function (if t '+ '-))
What am I missing here? Does Elisp not allow for such actions? Is this possibly related to the fact that Scheme is a lisp-1 language and Elisp a lisp-2?
Yes, the error is due to the fact that Emacs Lisp is a Lisp-2. This should work:
(funcall (if 1 '+ '-) 3 2)
This question already has answers here:
Test if array is inside a list in lisp
(1 answer)
Remove duplicate strings from a list
(1 answer)
Closed 8 years ago.
How do i remove duplicated lists inside a list in common-lisp?
I tried this:
(remove-duplicates '( (1 2 3) (1 2 3)))
But it evaluates to ((1 2 3) (1 2 3)), not ((1 2 3)).
Thanks.
Use the keyword argument :test to specify the function that defines whether or not two items are duplicates of each other. Most lisp functions, including remove-duplicates, use eql to test for equality by default. eql is much stricter than equal, which is what you probably want to be using.
(remove-duplicates '((1 2 3) (1 2 3)) :test #'equal)
This evaluates to '((1 2 3)).
See this post for more detail about the difference between eql and equal.
Try:
(remove-duplicates '((1 2 3) (1 2 3)) :test #'equal)
Is there equivalent of higher-order function filter in Emacs Lisp? Like function from python or Javascript.
(filter-equivalent (lambda (n) (= (% n 2) 0)) '(1 2 3 4 5 6 7 8))
==> (2 4 6 8)
It's cl-remove-if-not. A bit of a mouthful, but it works.
To elaborate a bit, you need
(require 'cl-lib)
to get this function. There's an alias for it, called remove-if-not, but
I prefer not to use it, since it may look like I'm using remove-if-not from cl.
It's a good practice to include the prefix, not doing using namespace std in C++,
but saying std::cout each time.
The third-party dash.el library provides a -filter function as an alternative to cl-remove-if-not.
(-filter 'evenp '(1 2 3 4 5 6 7 8))
;; => (2 4 6 8)
This question already has an answer here:
Test if array is inside a list in lisp
(1 answer)
Closed 2 years ago.
(remove '(1 2) '((1 2) (1 3)))
doesn't remove '(1 2) from list in common lisp. (I think it uses eq and not equal).
Do we have any other alternative to delete element from list of lists in common lisp?
(remove '(1 2) '((1 2) (1 3)) :test #'equal)
The Practical Common Lisp page 25, explains the WITH-STANDARD-IO-SYNTAX as follows. "It ensures that certain variables that affect the behavior of PRINT are set to their standard values".
The usage is as follows.
(with-open-file (...)
(with-standard-io-syntax
(print ...
Should (print) be used in this macro? If not, what would happen?
Various dynamic variables affect the output produced by print. with-standard-io-syntax ensures those variables are set to the default values.
For example:
(let ((list '(1 2 3 4 5 6 7 8 9 10))
(*print-length* 5))
(print list)
(with-standard-io-syntax
(print list)))
Prints:
(1 2 3 4 5 ...)
(1 2 3 4 5 6 7 8 9 10)
It's particularly important if you're printing things with the intention of being able to read them later (like with prin1).