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

Re: lisp function questions

20 views
Skip to first unread message

WJ

unread,
Sep 4, 2015, 1:41:45 PM9/4/15
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
drewc wrote:

> I spent a few hours today 'maturely optimizing' some code, and the fact
> that the above has to traverse LIST twice immediately popped out at me
....
> (defun l-average (list)
> "i love loop"
> (when list
> (loop for n in list
> count t into count
> sum n into sum
> finally (return (/ sum count)))))


Gauche Scheme:

(define (l-average nlist)
(if (null? nlist)
0
(apply /
(fold
(^(x acc) (map + (list x 1) acc))
'(0 0)
nlist))))


Another way:

(use srfi-42 :only (fold3-ec))

(define (l-average nlist)
(apply /
(fold3-ec
'(0 1)
(: x nlist)
(list x 1)
values
(map$ +))))

--
For it is an iron law of history that empires eventually turn against the
people who created them. All empires become corrupt as wealth and power begins
to gravitate to an increasingly smaller circle of the elite. Arrogant and
isolated, the elite treats the ordinary citizens of the empire as slaves and
potential enemies.
www.kolumbus.fi/aquilon/america-middle-class-and-the-end-of-growth.htm
0 new messages