Reputation: 1884
Any idea why
(+ nil ) ;-> returns nil
(apply + nil) ;-> return 0 ?
thank you very much
Upvotes: 1
Views: 108
Reputation: 16035
(apply + nil)
means "use no arguments" which is equivalent to just (+)
.
(+)
=> 0
Upvotes: 1
Reputation: 10865
The two cases are different.
In the case of (+ nil)
, the nil argument is in place of a number. In the case of (apply + nil)
, the nil is in place of a list of numbers.
The equivalent of
user> (+ nil)
nil
using apply would be
user> (apply + '(nil))
nil
which returns the same result.
On the other hand, by calling (apply + nil)
, you are calling +
on an empty list, in other words calling +
with no arguments, which returns 0. The following are all equivalent:
user> (+)
0
user> (apply + nil)
0
user> (apply + '())
0
Upvotes: 6