0
votes

I am making a program in Scheme (simply-scheme in Dr. Racket) that takes in a number of integers followed by the integer values. I need to be able to find the average of the list, which I have already got working and to be able to count the numbers greater than the average and display how many there are.

;;Function for the sum of list
(define (sum listlen) 
  (if
    (null? listlen)
    0
    (+ (car listlen) (sum (cdr listlen)))
  )
)






;;This is the part I am having trouble with
(let ((avg  (/ (apply + (cdr numbers)) (car numbers))))
  (count (lambda (x) (< x avg)) (cdr numbers)))

My function for finding the greater than average function is giving me an arity mismatch. it says "count: arity mismatch; the expected number of arguments does not match the given number expected: 1 given: 2 arguments...:"

I'm sure it's something simple that I doing wrong if my greater than average function but for the life of me I can't figure out what it is

1

1 Answers

1
votes
;;Function for the sum of list
(define (sum listlen) 
  (if
    (null? listlen)
    0
    (+ (car listlen) (sum (cdr listlen)))
  )
)

This is actually simply (define (sum lst) (apply + lst)).

Next,

(< x avg)

Should it actually be (> x avg) (to count numbers greater than the average)?

Finally, http://docs.racket-lang.org/manual@simply-scheme/index.html says for count:

Return the number of letters in a word or number of words in a sentence.

Did you mean something like (length (filter (lambda (x) (> x avg)) (cdr numbers)))? (Can be done with reduce too, or named let, or whatever.)