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!
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!