Try this:
(define lst '(1 2 3 4 5))
(define avg (average lst))
(define (square-of-deviation-2 x)
(expt (- x avg) 2))
(map square-of-deviation-2 lst)
Notice that you only need to calculate the average once, so you can do it before calling map
, because map
's function only expects a single value, which is each of the input list's elements in turn. An even nicer solution would be to pack everything in a single function:
(define (square-of-deviation lst)
(let ((avg (average lst)))
(map (lambda (x) (expt (- x avg) 2)) lst)))