0

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)))))
sds
  • 58,617
  • 29
  • 161
  • 278
  • Have you tried reading [the documentation](http://clhs.lisp.se/Body/f_funcal.htm)? – Kaz Nov 26 '13 at 17:25

1 Answers1

1

This function computes the sum of fields 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.

sds
  • 58,617
  • 29
  • 161
  • 278