Reputation: 64
(+ '(1 2 3 4 5) '(3 4 5 6 7))
Evaluate this expression. I don't know much about CLISP. It returns an error when I runs it on CLISP.
Explain the reason for error ??
Thanks in advance
Upvotes: 1
Views: 96
Reputation: 8854
CLISP itself will give you part of the answer:
[1]> (+ '(1 2 3 4 5) '(3 4 5 6 7))
*** - +: (1 2 3 4 5) is not a number
+
is a function that is only defined on numbers, so when the interpreter sees that a list of numbers has been offered as an argument to +
, it can't go any further.
The interpreter sees (1 2 3 4 5)
as the first argument to +
after evaluating '(1 2 3 4 5)
, i.e. (quote (1 2 3 4 5))
, which returns (1 2 3 4 5)
.
You may be thinking that +
is more flexible, sort of like the way that it works in Javascript. No: It's just the math plus function.
(Why doesn't the interpreter complain about (3 4 5 6 7)
? Because it stopped when it saw (1 2 3 4 5)
, and went to the debugger prompt.)
By the way, that error message is CLISP-specific, but any Common Lisp will give you an error on that input.
EDIT: Per @RainerJoswig's comment, it may be that the correct way to describe this is not that interpreter code responds to (1 2 3 4 5)
being paired with +
, but the +
function code that does so, and doesn't bother with the second argument, etc. What I wrote was my best guess about the right way to describe the situation, and partially addresses OP's question, but I am not a Lisp internals expert.
Upvotes: 1
Reputation: 70397
It's possible you were expecting this to concatenate
(+ '(1 2 3 4 5) '(3 4 5 6 7)) ==> '(1 2 3 4 5 3 4 5 6 7)
Common Lisp doesn't do this because it wouldn't make sense. In some languages like Python, there are a limited number of infix operators, so overloading +
makes a certain amount of sense. However, in Common Lisp, there are infinitely many function names and +
is but one of them, so we have different functions, such as append
to do this.
It's also possible you were expecting this to add pointwise.
(+ '(1 2 3 4 5) '(3 4 5 6 7)) ==> '(4 6 8 10 12)
It doesn't do this as it's also against Lisp's philosophy. Adding elements pointwise like this is a feature of tacit languages such as APL or J. These languages go to a lot of trouble to get features like this to work in the most general possible cases. As such, they tend not to focus so much on certain other features, such as the object system or metaprogramming.
This is where Lisp shines: Lisp is a metaprogramming language, so rather than spend all their time developing corner cases for mathematical functions, they made a simple function that simply adds numbers together and does nothing more, and then spent the bulk of their development time making a good macro system. Common Lisp in particular adds to this by having one of the best object systems (with fully generic dispatch) that I've seen. Languages aren't good at everything, so the best languages have to define a philosophy and stick to it. Accepting all kinds of input simply isn't Lisp's philosophy; metaprogramming is.
Upvotes: 4