-module(math).
-export([sum/1]).
sum(ListOfNumbers) ->
sum(ListOfNumbers, 0).
sum([Number | RestOfList], Subtotal) ->
sum(RestOfList, Subtotal + Number);
sum([], Total) ->
Total.
edit: blargh... sorry @simoncion, I didn't see your reply. It apparently takes me longer than 14min to type that out on my phone without typos + proper spaces to treat it like code :-)No worries. It's astonishing how absolutely awful on-screen phone keyboards still are for doing anything more involved than writing a brief human-language message.
No reason, apart from economics, why even with current IDE technology we couldn't make one that works well for specific programming languages.
That's like a big chunk of my point. :) Phone/tablet keyboards really suck for anything other than short human-language text entry. You wanna write a five-page paper? A code snippet to demonstrate a problem? Forget about it.
It's astonishing that these devices have been around for at least seven years and their packed-in keyboards still fail at these tasks.
Maybe. Compare your function to the equivalent Erlang one:
sum(L) ->
sum(L, 0).
sum([], Acc) ->
Acc;
sum([Num|Rest], Acc) ->
sum(Rest, Acc+Num).
Assuming that it's in a module called 'math', run it like so: math:sum([5,4,3,2,1]).
15sum = std::accumulate(begin, end, 0);
let listsum = foldl (+)
Let me take the opportunity to plug a new language which I have spent the last 5 months designing: github.com/jbodeen/ava
An ava solution -- 9 lines of code, and 1 set of parentheses -- would look like this:
Maybe we need to zoom out of ancient languages into more intuitive paradigms if programming is to become easier for more people to access