Reputation: 1
I don't understand what funcall
would do in this example. I need an explanation about when the code will execute.
(defun total-value (field L)
"Answer average value of fields of complex entries in list L"
(if (null L)
0
(+ (funcall field (first L))
(total-value field (rest L)))))
Upvotes: 0
Views: 278
Reputation: 60014
This function computes the sum of field
s in L
. It is equivalent to
(reduce #'+ L :key field)
or (much worse! don't ever do this!)
(apply #'+ (mapcar field L))
Here field
is a function which extracts a numeric value from an element of L
; funcall
is the artifact of Common Lisp being Lisp-2: (funcall field ...)
in Scheme (or any other Lisp-1) would look like (field ...)
.
More specifically; funcall
takes its first argument and coerces it into a function; then it calls this function on all its other arguments.
Upvotes: 1