Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!cwjcc!hal!nic.MR.NET!shamash!nis!ems!srcsip!eagle!rogers From: rogers@eagle.SRC.Honeywell.COM (Brynn Rogers) Newsgroups: comp.lang.lisp Subject: Summing a list Message-ID: <10794@srcsip.UUCP> Date: 25 Oct 88 13:33:42 GMT References: <1131@amelia.nas.nasa.gov> <5671@eagle.ukc.ac.uk> Sender: news@src.honeywell.COM Reply-To: rogers@eagle.UUCP (Brynn Rogers) Organization: Honeywell Systems & Research Center, Camden, MN Lines: 29 I Have a silly question. I need to take the sum of a list of numbers. (SETQ NUMLIST '(1 2 3 6 7)) This is quite simple, really, but what is the BEST way to do it? I like: (EVAL (CONS '+ NUMLIST)) But someone else says this would be faster:: (LET ((SUM 0)) (DOLIST (NUM NUMLIST) (SETQ SUM (+ SUM NUM)))) ;; or maybe (INCF SUM NUM) of course, there is always: (DEFUN SUM (NUMLIST) (COND ((NULL NUMLIST) 0) (T (+ (FIRST NUMLIST) (SUM (CDR NUMLIST)))))) I think it's obvious that the last would be slow. Also it's Obvious that there are Many ways to do this. What is the BEST (for speed and/or elegance) way to sum a list?? (I Run Gold Hills Common 3.0 on a Compaq 386/20 with a 387) Thanks, Brynn Rogers rogers@src.honeywell.com