STFU noobs we're dickfencing!
You're just lazy to study it.
unbin = fst . foldr (\n (sum, pos) -> (sum + digitToInt n * pos, pos*2)) (0, 1)
function unbin (str) // strings are lists of chars in Haskell
{
var sum = 0;
var pos = 1; // this is the initial data passed to the fold = (0, 1)
while (str.length)
{ // this is the fold
sum += Number (str.pop ()) * pos; // foldr grabs the rightmost element = pop
pos *= 2; // function returns the new tuple (sum + rightmost, pos*2)
}
return sum; // return the first value of the tuple = fst . foldr = fst (foldr (str))
}
Of course you find the imperative destructive version more straightforward but it's not. Functional function functions are mostly evident because definitions are short and abstract while imperative functions might go into twenty lines of code to do the same which you have to follow through to understand shit. http://thedailywtf.com/Articles/nice_num ,-mean_programmer.aspx
The C vs Haskell quicksort is a popular example (though it's an array vs a linked list).
I can write language interpreters in under 500 lines that outperform certain Macromedia/Adobe programs, coughcough, which they have to throw money at to get anywhere with; if anyone wants to add to mine they only need ten minutes to skim through it to see everything.