[plt-scheme] science fair help
Laboratory data is often processed in the manner of your example, so
it is practical to transpose the data once. That way, operations
other than average can be performed without having to rewrite the
intricate part (and reprocess the data.) Furthermore, the concept of
passing the operations (functions) as values can be exploited more
sensibly if the data is prepared first.
(define (transpose-lists-of-lon lists-of-lon)
(apply map list lists-of-lon))
(define (average-lon lon)
(/ (apply + lon) (length lon)))
(define (average-lists-of-lon lon)
(map average-lon lon))
(check-expect (average-lists-of-lon
(transpose-lists-of-lon
(list (list 1 2 3 4) (list 2 1 0 3) (list 4 0 1 7))))
(list 7/3 1 4/3 14/3))
Welcome to DrScheme, version 4.1.4 [3m].
Language: Intermediate Student with lambda custom; memory limit: 128
megabytes.
Teachpack: world.ss.
The only test passed!
>
rac
On Dec 13, 2009, at 6:54 PM, Todd O'Bryan wrote:
> It was not knowing how many lists he'd have ahead of time. Here's what
> I came up with:
>
> ;; avg: (listof number) -> number
> ;; consumes: a non-empty list of numbers
> ;; produces: the average of its elements
> (define (avg alon)
> (/ (foldr + 0 alon)
> (length alon)))
>
> (check-expect (avg (list 1 2 3 4 5)) 3)
> (check-expect (avg (list 2 1 0 3 7)) 13/5)
>
> ;; avg-lists: (listof (listof number)) -> (listof number)
> ;; consumes: a list of lists of numbers -- all the same length
> ;; produces: a list of the element-wise averages
> (define (avg-lists alolon)
> (cond
> [(andmap empty? alolon) empty]
> [(cons? alolon) (cons (avg (map first alolon))
> (avg-lists (map rest alolon)))]))
>
> (check-expect (avg-lists (list (list 1 2 3 4) (list 2 1 0 3) (list 4
> 0 1 7)))
> (list 7/3 1 4/3 14/3))
>
> On Sun, Dec 13, 2009 at 8:45 PM, Matthias Felleisen
> <matthias at ccs.neu.edu> wrote:
>>
>>
>> (check-expect (average (list (list 1 3 5 7) (list 2 1 3 3) (list 3
>> 8 1 5)))
>> (list 2 4 3 5))
>>
>> (define (average lol)
>> (map (lambda (a b c) (/ (+ a b c) 3)) (first lol) (second lol)
>> (third lol)))
>>
>>
>> yields
>>
>>> Welcome to DrScheme, version 4.2.3.4-svn4dec2009 [3m].
>>> Language: Intermediate Student with lambda.
>>> The test passed!
>>>>
>>
>> So what's the problem? -- Matthias
>>
>>
>>
>> On Dec 13, 2009, at 8:02 PM, Todd O'Bryan wrote:
>>
>>> I have a student who's doing a science fair project and needs to
>>> find
>>> the average for each location in a bunch (number unspecified) of
>>> lists.
>>>
>>> For example, given
>>>
>>> (list 1 3 5 7) (list 2 1 3 3) (list 3 8 1 5)
>>>
>>> he'd like
>>>
>>> (list 2 4 3 5)
>>>
>>> I can write the function pretty easily in Module by writing an
>>> average
>>> function that takes a variable number of arguments, but my
>>> attempts to
>>> do it in Intermediate or Advanced are pretty complicated. Is there a
>>> fairly easy way to do it in the teaching languages that I'm missing?
>>> If not, I can just put together a teachpack for him that provides
>>> that
>>> function so he can use it.
>>>
>>> Todd
>>> _________________________________________________
>>> For list-related administrative tasks:
>>> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
>>
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme