Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Finding Average without using Recusrion only using Prog

96 views
Skip to first unread message

WJ

unread,
Nov 4, 2012, 3:11:36 AM11/4/12
to
Tim Bradshaw wrote:

> (defun avg (args)
> (loop for x in args
> for l upfrom 1
> summing x into tot
> finally (return (/ tot l))))

Racket:

(define (avg numbers)
(define count 0)
(/ (for/sum ([x numbers]) (inc! count) x)
count))


Given:

(define-syntax-rule (inc! var)
(set! var (add1 var)))

Nils M Holm

unread,
Nov 4, 2012, 3:21:32 AM11/4/12
to
WJ <w_a_...@yahoo.com> wrote:
> Racket:
>
> (define (avg numbers)
> (define count 0)
> (/ (for/sum ([x numbers]) (inc! count) x)
> count))

Come on, this is *ugly*. Even plain vanilla Scheme can do better:

(define (avg n*)
(/ (apply + n*)
(length n*)))

Substitute SRFI-1 FOLD for APPLY, if your APPLY is broken
(i.e. limited to some maxmimum number of arguments).

--
Nils M Holm < n m h @ t 3 x . o r g > www.t3x.org

WJ

unread,
Nov 4, 2012, 3:32:15 AM11/4/12
to
Using fold (reduce):

(define (avg numbers)
(apply /
(foldl ($(x acc) (map + (list x 1) acc)) '(0 0) numbers)))


Given:

(define-syntax-rule ($ expr ...) (lambda expr ...))

WJ

unread,
Nov 4, 2012, 3:41:55 AM11/4/12
to
Nils M Holm wrote:

> WJ <w_a_...@yahoo.com> wrote:
> > Racket:
> >
> > (define (avg numbers)
> > (define count 0)
> > (/ (for/sum ([x numbers]) (inc! count) x)
> > count))
>
> Come on, this is ugly. Even plain vanilla Scheme can do better:
>
> (define (avg n*)
> (/ (apply + n*)
> (length n*)))
>
> Substitute SRFI-1 FOLD for APPLY, if your APPLY is broken
> (i.e. limited to some maxmimum number of arguments).

You completely missed the point, which was to avoid
traversing the list more than once. Consequently,
you solved an entirely different problem.

Next time, think twice before you go off half-cocked.

See my other post to learn the correct way to use
fold to solve this.

Nils M Holm

unread,
Nov 4, 2012, 3:47:07 AM11/4/12
to
WJ <w_a_...@yahoo.com> wrote:
> > (define (avg n*)
> > (/ (apply + n*)
> > (length n*)))
> >
> > Substitute SRFI-1 FOLD for APPLY, if your APPLY is broken
> > (i.e. limited to some maxmimum number of arguments).
>
> You completely missed the point, which was to avoid
> traversing the list more than once. Consequently,
> you solved an entirely different problem.

APPLY does not traverse the list. Problem solved.

RG

unread,
Nov 4, 2012, 10:07:39 AM11/4/12
to
In article <afmocb...@mid.individual.net>,
Nils M Holm <news...@t3x.org> wrote:

> WJ <w_a_...@yahoo.com> wrote:
> > > (define (avg n*)
> > > (/ (apply + n*)
> > > (length n*)))
> > >
> > > Substitute SRFI-1 FOLD for APPLY, if your APPLY is broken
> > > (i.e. limited to some maxmimum number of arguments).
> >
> > You completely missed the point, which was to avoid
> > traversing the list more than once. Consequently,
> > you solved an entirely different problem.
>
> APPLY does not traverse the list. Problem solved.

Of course it does. How else could it access the arguments to the
function being applied?

rg

Nils M Holm

unread,
Nov 4, 2012, 11:26:03 AM11/4/12
to
RG <rNOS...@flownet.com> wrote:
> > APPLY does not traverse the list. Problem solved.
>
> Of course it does. How else could it access the arguments to the
> function being applied?

Glad to see that obvious flaw in my argumentation revealed!

Anyway, I would still argue that (apply + x) is more efficient
than (fold + 0 x).

Sam Steingold

unread,
Nov 4, 2012, 12:07:45 PM11/4/12
to
> * Nils M Holm <arjf...@g3k.bet> [2012-11-04 16:26:03 +0000]:
>
> Anyway, I would still argue that (apply + x) is more efficient
> than (fold + 0 x).

yes, but the former may fail on lists longer than `call-arguments-limit'
which is only guaranteed to be at least 50.
http://www.lispworks.com/documentation/HyperSpec/Body/v_call_a.htm

--
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://honestreporting.com http://think-israel.org
http://camera.org http://palestinefacts.org http://memri.org http://iris.org.il
If you want it done right, you have to do it yourself

Nils M Holm

unread,
Nov 4, 2012, 1:24:50 PM11/4/12
to
Sam Steingold <s...@gnu.org> wrote:
> > * Nils M Holm <arjf...@g3k.bet> [2012-11-04 16:26:03 +0000]:
> > Anyway, I would still argue that (apply + x) is more efficient
> > than (fold + 0 x).
>
> yes, but the former may fail on lists longer than `call-arguments-limit'
> which is only guaranteed to be at least 50.
> http://www.lispworks.com/documentation/HyperSpec/Body/v_call_a.htm

Yes, I know. Originally, what I replied to was about Racket and
Scheme, whose standards do not say anything about the number of
arguments of APPLY.

Alas, I guess this is enough off-topic musing for now.

WJ

unread,
Jan 4, 2015, 2:53:26 AM1/4/15
to
WJ wrote:

> Tim Bradshaw wrote:
>
> > (defun avg (args)
> > (loop for x in args
> > for l upfrom 1
> > summing x into tot
> > finally (return (/ tot l))))


Gauche Scheme:

(use srfi-42)
(define (avg numbers)
(let1 cnt 0
(/ (sum-ec (: n numbers) (begin (inc! cnt)) n)
(max 1 cnt))))

(avg '(10 20 30 40 50))
===>
30

(avg ())
===>
0

Mike

unread,
Jan 4, 2015, 11:25:01 AM1/4/15
to
I didn't see the orignal thread. Why would this not work?

(defun avg (values)
(/ (+ avg) (length values)))

Mike

Raymond Wiker

unread,
Jan 4, 2015, 1:52:32 PM1/4/15
to
Mike <mi...@mikee.ath.cx> writes:

[ WJ garbage snipped ]

>
> I didn't see the orignal thread. Why would this not work?
>
> (defun avg (values)
> (/ (+ avg) (length values)))
>
> Mike

Because it is incorrect?

Note that even if you fix the obvious problem with your code, you still
end up with something that traverses the list twice.


Mike

unread,
Jan 4, 2015, 2:59:01 PM1/4/15
to
Duh. :)
I was typing and not thinking about what I was typing. True, the list is looked at twice.
Premature optimization?

(defun avg (values)
(/ (+ values) (length values)))

Pascal J. Bourguignon

unread,
Jan 4, 2015, 3:05:28 PM1/4/15
to
Still not thinking.

values is a list. What would + a list be?

--
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk

Kaz Kylheku

unread,
Jan 4, 2015, 3:11:41 PM1/4/15
to
On 2015-01-04, Raymond Wiker <rwi...@gmail.com> wrote:
> Note that even if you fix the obvious problem with your code, you still
> end up with something that traverses the list twice.

If it's good enough for Santa, it's good enough for me!

Mike

unread,
Jan 4, 2015, 6:01:41 PM1/4/15
to
On 2015-01-04, Pascal J. Bourguignon <p...@informatimago.com> wrote:
> Mike <mi...@mikee.ath.cx> writes:
>
>> On 2015-01-04, Raymond Wiker <rwi...@gmail.com> wrote:
>>> Mike <mi...@mikee.ath.cx> writes:
>>>
>>> [ WJ garbage snipped ]
>>>
>>>>
>>>> I didn't see the orignal thread. Why would this not work?
>>>>
>>>> (defun avg (values)
>>>> (/ (+ avg) (length values)))
>>>>
>>>> Mike
>>>
>>> Because it is incorrect?
>>>
>>> Note that even if you fix the obvious problem with your code, you still
>>> end up with something that traverses the list twice.
>>>
>>>
>>
>> Duh. :)
>> I was typing and not thinking about what I was typing. True, the list is looked at twice.
>> Premature optimization?
>>
>> (defun avg (values)
>> (/ (+ values) (length values)))
>
> Still not thinking.
>
> values is a list. What would + a list be?
>

Duh again.
This time I typed the defun into clisp. I have:

(defun avg (values)
(/ (apply #'+ values) (length values)))
(setf x '(1 2 3 4 5 6))
(avg x)
=> 7/2

Potentially premature optimization aside, what's wrong with this
simple version rather than a LOOP macro?

Mike

Pascal J. Bourguignon

unread,
Jan 5, 2015, 3:49:10 AM1/5/15
to
Better, but it will fail on:

(avg (make-list call-arguments-limit :initial-element 1))

Raymond Wiker

unread,
Jan 5, 2015, 2:30:24 PM1/5/15
to
Use reduce instead of apply; there is a maximum size to argument lists
passed to apply.

Assuming that this function is meant to be used without any assumptions
on the size of the input, the phrase "premature optimization" does not
make a lot of sense.

tar...@google.com

unread,
Jan 5, 2015, 2:46:55 PM1/5/15
to
:)
0 new messages