5 ms·
For example, a typical C program to compute the sum of a list of numbers includes three kinds of parentheses and three kinds of assignment operators in five lin
by grapevines 10y ago
For example, a
typical C program to compute the sum of a list of numbers includes three kinds of parentheses and three
kinds of assignment operators in five lines of code
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:
let rec sum list =
let are_we_at_the_end = 0 in
let take_a_number_and_the_rest_of_the_list n list =
add n ( sum list )
in
list
are_we_at_the_end
take_a_number_and_the_rest_of_the_list
in
Maybe we need to zoom out of ancient languages into more intuitive paradigms if programming is to become easier for more people to access
- freditup 10y agoCould you elaborate on how this is more understandable to a novice than the typical C program? My guess is that neither is immediately understandable, and that the C program is clearer to someone experienced.
- simoncion 10y ago> Maybe we need to zoom out of ancient languages... 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]). 15
- im_down_w_otp 10y ago-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 :-)
- simoncion 10y ago> sorry @simoncion... 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.
- eru 10y agoThat's because they are optimized for that. No reason, apart from economics, why even with current IDE technology we couldn't make one that works well for specific programming languages.
- simoncion 10y ago> That's because they are optimized for that. 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.
- thrownaway2424 10y agoTypical C program, eh? Yet another reason to not program in C, I guess. sum = std::accumulate(begin, end, 0);
- eru 10y agoWow, that's pretty verbose. But I guess the long variable names are partly to blame.
- jackweirdy 10y agoHaskell: let listsum = foldl (+)