(sum '(2 3 4)) => 9
(sum '(5 6)) => 11
The usual way of doing this would be
(reduce #'+ '(2 3 4))
which is considered to be preferable to using APPLY. LOOP can do the
job almost as well:
(loop for x in '(2 3 4) sum x)
The thing about LOOP is that it's much more flexible, in case you need
some slight variation of the theme, like for example:
(loop for x in '(2 3 a 4) when (numberp x) sum x)
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 76 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 55:58:28 |
| Calls: | 1,265 |
| Calls today: | 1 |
| Files: | 1,362 |
| D/L today: |
3 files (2,684K bytes) |
| Messages: | 285,718 |