Search code examples
functional-programmingschemelispelisp

Apply sum-of-squares to multiple numbers with reduce


I define square and sum-of-squares as:

(defun square(x)
  (* x x))
(defun sum-of-squares (x y)
  (+ (square x) (square y)))

then apply it an array with reduce:

(reduce 'sum-of-squares '(2 2 2))

but get result as 68 rather than 12.

What's the problem?


Solution

  • The reduction does

    (sum-of-squares (sum-of-squares 2 2) 2)
    

    which is

        (sum-of-squares 8 2)
    

    which is 64 + 4

    You can get the result you expect with

    (reduce #'+ (mapcar #'square '(2 2 2)))
    

    or

    (reduce #'(lambda (x y) (+ x (square y))) '(2 2 2) :initial-value 0)
    

    or by defining sum-of-squares to take an arbitrary number of arguments.