PDA

View Full Version : Scheme problem, need HELP!


SchemeVirgin
09-06-2004, 09:06 AM
Hi,
Im a beginner in Scheme and I really need help. Im trying to return the average of a complex list of numbers.
So basically its just adding the numbers in a list and dividing by the number of elements in the list. Im having trouble coming up with the output that my teacher has.

Teachers example output:

> (f10 '((1 2) 2 (3 4 (5 6))))
3 2/7

Here is what my code looks so far:
===================================
>(define f9
(lambda (lis)
(COND
((null? lis) 0)
((LIST? (car lis)) (+ (f9 (car lis)) (f9 (cdr lis))))
(ELSE (+ 1 (f9 (cdr lis)))))))

;returns average of a complex list of numbers
>(define f10
(lambda (lis)
(IF (NULL? lis)
0
(/(+ (car(cdr lis))(car (cdr lis)))(f9 lis)))))

====================================

The output im getting is:
> (f10 '((1 2) 2 (3 4 (5 6))))
4/7

So can anyone help me. I know i have to a recursive function but I just don't know how. Thanks!

SolarBear
10-14-2004, 03:46 PM
Maybe I'm just not getting what you mean, but in your f10 function, you use
(+ (car(cdr lis))(car (cdr lis)))
You're adding the same expression to itself, so you really get
(* 2 (car (cdr lis)))
Or maybe this is just my old Scheme-uneducated self.