Discussion:
Help with GA, and critique my Lisp (please ;-))
(too old to reply)
B. Pym
2024-09-26 02:27:30 UTC
Permalink
(defun evaluate-poly (p x)
(loop for coeff in p
for power from 0
sum (* coeff (expt x power)))
A little wasteful, but what the heck.
(defun evaluate-poly (p x)
(reduce #'(lambda (a c) (+ c (* x a)))
(reverse p) :initial-value 0))
It ought to be "(lambda", not "#'(lambda". However, disciples
of CL (COBOL-Like) always try to make their code as ugly and
as prolix as possible. He would have been even more pleased
if he could have written:

(#'reduce #'#'#'#'#'#'#'#'#'(lambda (a c) (#'+ c (#'* x a)))

Gauche Scheme:

(define (eval-poly p x)
(fold-right
(^(c a) (+ c (* x a)))
0 p))
Kaz Kylheku
2024-09-26 03:53:27 UTC
Permalink
Post by B. Pym
(defun evaluate-poly (p x)
(loop for coeff in p
for power from 0
sum (* coeff (expt x power)))
A little wasteful, but what the heck.
(defun evaluate-poly (p x)
(reduce #'(lambda (a c) (+ c (* x a)))
(reverse p) :initial-value 0))
It ought to be "(lambda", not "#'(lambda". However, disciples
of CL (COBOL-Like) always try to make their code as ugly and
as prolix as possible. He would have been even more pleased
(#'reduce #'#'#'#'#'#'#'#'#'(lambda (a c) (#'+ c (#'* x a)))
(define (eval-poly p x)
(fold-right
(^(c a) (+ c (* x a)))
0 p))
TXR Lisp:

1> (poly 0.5 '(2 3 4))
6.0
2> (rpoly 0.5 '(2 3 4))
4.5
--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @***@mstdn.ca
Loading...