(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: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 00:51:57 |
Calls: | 10,385 |
Calls today: | 2 |
Files: | 14,057 |
Messages: | 6,416,573 |