Add with alternating signs
Posted: Fri May 18, 2007 5:20 pm
Among the Code Snippets, "Add with alternating signs" can be improved by replacing the principal procedure, given currently by the following macro definition:
by a function which does the same thing:
In addition to shortening the punchline, the function version will execute faster. Please let me know if I missed anything. Thanks! --Rick
Code: Select all
(define-macro (+-)
(let (signs (cons 1 (series 1 -1 (- (length (args)) 1))))
(apply 'add (map 'mul (map eval (args)) signs))))
Code: Select all
(define (+-)
(let (signs (cons 1 (series 1 -1 (- (length (args)) 1))))
(apply add (map mul signs (args)))))