C++17, 82 66 bytes
int f(){return 0;}int f(int H,auto... L){return(H*...*L)+f(L...);}
Uses the C++17 template parameter fold expression and essentially the same idea as Dennis. Saving 16 bytes by using Generic Variadic Lambda.
Explanation:
int f(){return 0;} //base case for empty list
int f(int H, auto... L) { //first element, variadic arguments
return (H*...*L) //a_0*a_1*a_2*...
+ f(L...); //+ f(a_1,a_2,...)
}
Usage:
f(1,1,0,1,1,0,1,1,1,1,1) -> 5
f() -> 0
f(1,0,1,0) -> 0
Non competing
Albeit longer, this also works with template constants:
template <int...L> int C=0;
template <int H, int...L> int C<H,L...> = (H*...*L)+C<L...>;
Usage:
std::cout << C<1,0,1,1> << std::endl;
std::cout << C<1,0,1,0,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1,1,1,1,1> << std::endl;
std::cout << C<> << std::endl;
Can we take the list as a string of zeros and ones? e.g.
01100
? – Adnan – 2016-11-06T12:57:58.223@Adnan only if that is the most normal way for your language to represent boolean lists. – Adám – 2016-11-06T15:42:10.700
71Sorry for your loss. – Martin Ender – 2016-11-06T17:03:37.233
6@MartinEnder Thank you. It will be tough going forward. Dan taught me all I needed to know to work for Dyalog. – Adám – 2016-11-06T17:32:02.163
5Farewell to Dan. RIP... – Erik the Outgolfer – 2016-11-07T12:46:17.323
Are we allowed to take the input as actual bits in a row instead of individual bytes? i.e. {1,0,1,0} would be 1010000 not 10000000 00000000 10000000 00000000 – moonheart08 – 2018-10-04T19:05:31.697
@moonheart08 Only if that is the most normal way for your language to represent boolean lists. – Adám – 2018-10-04T19:58:12.397