0
votes

Standard Deviation of a list of integers in Scheme.

Write two expressions to solve this problem:

  • First expression, Compute the sum of the squares of the difference between > each number and the population average
  • Second, Using that number calculate the standard deviation.

My code so far:

(define (average lst)
  (/ (apply + lst) (length lst)))

(define (std-dev lst)
    (sqrt
        (/  
            (innerFunction lst (average lst))
            (length lst)
        )
    )
)

(define (innerFunction lst avg)
    (if
        (null? lst)
        0
        (+ 
            (*
                (- (car lst) avg)
                (- (car lst) avg)
            )
            (innerFunction (cdr lst) average)
        )
    )
)

(error is "number required, but got #Closure").

I've only started Scheme a week ago so I am not very familiar with it, do you see what's wrong with my code? and how do I fix it, also is there a simpler way to do this possibly with mapping like how I simplified average with apply?

1
"innerFunction" == "sum-devs-squared" or something like that. Simple rule of thumb: give your functions meaningful names, but name your variables by short abbreviations (even one-letter, where the intent is clear). --- yes, your innerFunction can be coded as a call to map. - Will Ness

1 Answers

0
votes

You're passing a procedure, not a number in the last line. Try this:

(define (innerFunction lst avg)
  (if (null? lst)
      0
      (+  (* (- (car lst) avg)
             (- (car lst) avg))
          (innerFunction (cdr lst) avg)))) ; here was the error