[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Help with GA, and critique my Lisp (please ;-

William James

8/14/2015 12:56:00 PM

Gareth McCaughan wrote:

> > (defun evaluate-poly (p x) ; (7 3 3.14) represents 7 + 3x + 3.14x^2
> > (labels ((looper (p acc power)
> > (if p
> > (looper (rest p) (+ acc (* (first p) (expt x power)))
> > (1+ power))
> > acc)))
> > (looper p 0 0)))
>
> Very Scheme-y indeed. :-) Also, all those EXPTs are going to
> be inefficient when you're doing a lot of them. (And when you
> do GA, you inevitably do a lot of whatever you do.)
>
> (loop for coeff in p
> for x^n = 1 then (* x^n x)
> sum (* coeff x^n))

Gauche Scheme:

(define (evaluate-poly p x)
(do ((x^n x (* x x^n))
(sum (pop! p) (+ sum (* (pop! p) x^n))))
((null? p) sum)))

Another way:

(define (evaluate-poly p x)
(do ((x^n 1 (* x x^n))
(sum 0 (+ sum (* (pop! p) x^n))))
((null? p) sum)))

Another way:

(define (evaluate-poly p x)
(let go ((p p) (x^n 1))
(+ (* (car p) x^n)
(if (null? (cdr p)) 0 (go (cdr p) (* x^n x))))))


Another way:

(use gauche.collection :only (fold2))

(define (evaluate-poly p x)
(fold2
(lambda (coeff sum x^n) (values (+ sum (* coeff x^n)) (* x^n x)))
0 1
p))


Another way:

(define-syntax mut
(syntax-rules ()
((_ init proc arg ...)
(let ((x init) (stale #f))
(lambda ()
(if stale
(set! x (proc x arg ...))
(set! stale #t))
x)))))

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

(define (evaluate-poly p x)
(let1 m (mut 1 * x)
(sum-ec (: c p) (* c (m)))))

--
Africans gang-rape and clitorectomize Finnish girl; government arrests Finn
whom they accuse of complaining:
conservative-headlines.com/2009/03/another-european-awaits-extradition-for-hate-speech