Add with alternating signs

Started by rickyboy, May 18, 2007, 10:20:55 AM

Previous topic - Next topic

rickyboy

Among the Code Snippets, "Add with alternating signs" can be improved by replacing the principal procedure, given currently by the following macro definition:
(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:
(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

#1
Thanks Rick, I updated: http://newlisp.org/index.cgi?Tips_and_Tricks">http://newlisp.org/index.cgi?Tips_and_Tricks



Lutz