[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: lisp function questions

William James

9/4/2015 5:40:00 PM

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