Add with alternating signs

For the Compleat Fan
Locked
rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Add with alternating signs

Post by rickyboy »

Among the Code Snippets, "Add with alternating signs" can be improved by replacing the principal procedure, given currently by the following macro definition:

Code: Select all

(define-macro (+-)
  (let (signs (cons 1 (series 1 -1 (- (length (args)) 1))))
    (apply 'add (map 'mul (map eval (args)) signs))))
by a function which does the same thing:

Code: Select all

(define (+-)
  (let (signs (cons 1 (series 1 -1 (- (length (args)) 1))))
    (apply add (map mul signs (args)))))
In addition to shortening the punchline, the function version will execute faster. Please let me know if I missed anything. Thanks! --Rick
(λx. x x) (λx. x x)

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »


Locked