Difference in function evaluation between Scheme and Elisp [duplicate] - emacs

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)

Related

#<PACKAGE COMMON-LISP> is locked [duplicate]

This question already has an answer here:
Lisp SYMBOL-PACKAGE-LOCKED-ERROR
(1 answer)
Closed 7 years ago.
I am learning the Lisp programming language. I have written a simple program:
(defun abs(x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (- x))))
(print (abs 5))
when I compile and run this code, i get the following warning:
WARNING: DEFUN/DEFMACRO(ABS): #<PACKAGE COMMON-LISP> is locked
Ignore the lock and proceed
WARNING: DEFUN/DEFMACRO: redefining function ABS in /home/lisp/file.lisp, was defined in C
I didn't understand why is this warning given, what does it mean?
can anybody explain it?
You are trying to redefine an ANSI Common Lisp function
abs
and the system is warning you about it.
Please see "package locking"
in the manual.
(You now owe me 1 zorkmid).

Filter function in Elisp

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)

Lisp: Macros vs Functions [duplicate]

This question already has answers here:
What can you do with Lisp macros that you can't do with first-class functions?
(8 answers)
Closed 5 years ago.
In my quest to fully understand the so powerful lisp macros a question came to my mind. I know that a golden rule about macros is the one saying "Never use a macro when a function will do the work".
However reading Chapter 9 - Practical: Building a Unit Test Framework - from the book Practical Common Lisp I was introduced to the below macro whose purpose was to get rid of the duplication of the test case expression, with its attendant risk of mislabeling of results.
;; Function defintion.
(defun report-result (result form)
(format t "~:[FAIL~;pass~] ... ~a~%" result form))
;; Macro Definition
(defmacro check (form)
`(report-result ,form ',form))
OK, I understand its purpose but I could have done it using a function instead of a macro, for instance:
(setf unevaluated.form '(= 2 (+ 2 3)))
(defun my-func (unevaluated.form)
(report-result (eval unevaluated.form) unevaluated.form))
Is this only possible because the given macro is too simple ?
Furthermore, is Lisp Macro System so powerful relatively its opponents due to the code itself - like control structures, functions, etc - is represented as a LIST ?
But if it were a macro you, could have done:
(check (= 2 (+ 2 3)))
With a function, you have to do:
(check '(= 2 (+ 2 3)))
Also, with the macro the (= 2 (+ 2 3)) is actually compiled by the compiler, whereas with the function it's evaluated by the eval function, not necessarily the same thing.
Addenda:
Yes, it's just evaluating the function. Now what that means is dependent upon the implementation. Some can interpret it, others can compile and execute it. But the simple matter is that you don't know from system to system.
The null lexical environment that others are mentioning is also a big deal.
Consider:
(defun add3f (form)
(eval `(+ 3 ,form)))
(demacro add3m (form)
`(+ 3 ,form))
Then observe:
[28]> (add3m (+ 2 3))
8
[29]> (add3f '(+ 2 3))
8
[30]> (let ((x 2)) (add3m (+ x 3)))
8
[31]> (let ((x 2)) (add3f '(+ x 3)))
*** - EVAL: variable X has no value
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead of X.
STORE-VALUE :R2 Input a new value for X.
ABORT :R3 Abort main loop
Break 1 [32]> :a
That's really quite damning for most use cases. Since the eval has no lexical environment, it can not "see" the x from the enclosing let.
The better substitution would be not with eval, which won't perform as expected for all cases (for example, it doesn't have access to the lexical environment), and is also overkill (see here: https://stackoverflow.com/a/2571549/977052), but something using anonymous functions, like this:
(defun check (fn)
(report-result (funcall fn) (function-body fn)))
CL-USER> (check (lambda () (= 2 (+ 2 3))))
By the way, this is how such things are accomplished in Ruby (anonymous functions are called procs there).
But, as you see, it becomes somewhat less elegant (unless you add syntax sugar) and, there's actually a bigger problem: ther's no function-body function in Lisp (although there may be non-standard ways to get at it). Overall, as you see, for this particular task the alternative solutions are substantially worse, although in some cases such approach could work.
In general, though, if you want to do something with the source code of the expressions passed into the macro (and usually this is the primary reason of using macros), functions would not be sufficient.
The report-result function needs both the source code and the result of the execution.
The macro CHECK provides both from a single source form.
If you put a bunch of check forms into the file, they are easily compiled using the usual process of compiling Lisp files. You'll get a compiled version of the checking code.
Using a function and EVAL (better use COMPILE) you would have deferred the source evaluation to a later time. It would also not be clear if it is interpreted or compiled. In case of compilation, you would then later get the compiler's checks.

Lisp initialize variable with list

I'm learning Lisp. I'm implementing solution to some relatively simple problem. I'm thinking of list that represents initial state of problem like this
((0 1) (2 3) (5 4))
I want to create variable and assign that list to it. I've tried
(let ((initial-state ((0 1) (2 3) (5 4)))))
but this won't compile. After that I've tried
(let ((initial-state list (list 0 1) (list 2 3) (list 5 4))))
this works, but it's too long. Is there better way to do this?
(let ((initial-state '((0 1) (2 3) (4 5))))
...)
The ' expands to (quote ...) which basically means "don't evaluate this, just return it to me as a list". It's used to separate data from code (which in lisp are related concepts).
Do you mean this?
(let ((initial-state '((0 1) (2 3) (5 4)))) ...)
That single quote is a quote. :)
More about quoting here:
When to use 'quote in Lisp
Wikipedia article on Lisp

Using lists with Common LISP

I'm just starting out with LISP, as in, just opened the book, I'm two pages into it. I'm trying to understand what is and what is not an acceptable fn call. Every time I try to execute
(1 2 3 4)
I get an illegal fn call error
same goes for
(cdr (1 2 3 4))
(first (1 2 3 4))
(a b c d)
Are CL programs unable to return lists? How would I go about using these functions or printing a list? I'm using the SLIME implementation if it matters. LISP is very different than anything I've worked with before and I want to be sure I'm getting it conceptually.
You need to quote lists if you are using them as constants. Otherwise, the system will try to call the function 1 on the arguments 2 3 4, which will not work (note that function calls have the same syntax as lists). Your examples should be:
'(1 2 3 4)
(cdr '(1 2 3 4))
(first '(1 2 3 4))
'(a b c d)
Hooo boy.
Look up Practical Common Lisp by Seibel. He's such a nice guy, he put it online for free reading. It's very useful.
Part of the definition of Lisp is this rule:
When a list is seen: Using the first element of the list, apply it to the rest of the list.
But wait: How do you actually enter lists then? There are two functions to do this: QUOTE and LIST.
As an example, let's print a list to the screen on standard out:
(format *standard-output* "~a" '(1 2 3 4))
For format, *standard-output* is aliased to t (well, at least in SBCL!), so usually we see (format t ....