Cody

# Problem 201. Mimic foldl in functional programming

Solution 792912

Submitted on 10 Dec 2015 by Jean-Marie Sainthillier
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
%% summation assert(isequal(foldl(@plus, 0, 1:100), 5050));

a = 3 a = 6 a = 10 a = 15 a = 21 a = 28 a = 36 a = 45 a = 55 a = 66 a = 78 a = 91 a = 105 a = 120 a = 136 a = 153 a = 171 a = 190 a = 210 a = 231 a = 253 a = 276 a = 300 a = 325 a = 351 a = 378 a = 406 a = 435 a = 465 a = 496 a = 528 a = 561 a = 595 a = 630 a = 666 a = 703 a = 741 a = 780 a = 820 a = 861 a = 903 a = 946 a = 990 a = 1035 a = 1081 a = 1128 a = 1176 a = 1225 a = 1275 a = 1326 a = 1378 a = 1431 a = 1485 a = 1540 a = 1596 a = 1653 a = 1711 a = 1770 a = 1830 a = 1891 a = 1953 a = 2016 a = 2080 a = 2145 a = 2211 a = 2278 a = 2346 a = 2415 a = 2485 a = 2556 a = 2628 a = 2701 a = 2775 a = 2850 a = 2926 a = 3003 a = 3081 a = 3160 a = 3240 a = 3321 a = 3403 a = 3486 a = 3570 a = 3655 a = 3741 a = 3828 a = 3916 a = 4005 a = 4095 a = 4186 a = 4278 a = 4371 a = 4465 a = 4560 a = 4656 a = 4753 a = 4851 a = 4950 a = 5050

2   Pass
%% summation 2 r = rand(1, 100); y = foldl(@plus, 0, r); yc = sum(r); assert(abs(y - yc)/abs(y) < 1e-6);

a = 1.0041 a = 1.4920 a = 2.2115 a = 2.4931 a = 3.3646 a = 4.1928 a = 4.7900 a = 5.5761 a = 6.1932 a = 6.4470 a = 6.6423 a = 7.1996 a = 7.8639 a = 8.0671 a = 8.7596 a = 9.6362 a = 9.9364 a = 10.3122 a = 10.4983 a = 10.6288 a = 11.4109 a = 11.7500 a = 11.9871 a = 12.4373 a = 12.8000 a = 13.5486 a = 13.7616 a = 14.4397 a = 14.6794 a = 14.7829 a = 15.2840 a = 16.2398 a = 16.3638 a = 16.8337 a = 17.6996 a = 17.7518 a = 18.2362 a = 18.3059 a = 18.6108 a = 19.5420 a = 19.8837 a = 20.1484 a = 21.1023 a = 21.7046 a = 22.2378 a = 23.1747 a = 23.5817 a = 23.6134 a = 23.9435 a = 24.2060 a = 25.1637 a = 25.2319 a = 25.7381 a = 25.7836 a = 25.8805 a = 26.6487 a = 27.2462 a = 27.9124 a = 28.0148 a = 28.5663 a = 29.4790 a = 30.3371 a = 30.8976 a = 31.5086 a = 31.8628 a = 32.6159 a = 32.7374 a = 33.3675 a = 33.9508 a = 34.4096 a = 34.9562 a = 35.6288 a = 35.8283 a = 35.9849 a = 36.9168 a = 37.4155 a = 38.3372 a = 39.1021 a = 40.0250 a = 40.7798 a = 40.9261 a = 41.9149 a = 42.1245 a = 42.6007 a = 43.3544 a = 43.6147 a = 43.7844 a = 44.3480 a = 44.4491 a = 45.0251 a = 45.9839 a = 46.7817 a = 47.3257 a = 47.6204 a = 48.0280 a = 48.7376 a = 49.7218 a = 50.2443 a = 51.2111

3   Pass
%% factorial assert(isequal(foldl(@times, 1, 1:15), factorial(15)));

a = 2 a = 6 a = 24 a = 120 a = 720 a = 5040 a = 40320 a = 362880 a = 3628800 a = 39916800 a = 479001600 a = 6.2270e+09 a = 8.7178e+10 a = 1.3077e+12

4   Pass
%% find maximum r = rand(1, 1000); assert(isequal(foldl(@max, -Inf, r), max(r)));

a = 0.4028 a = 0.4028 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.8855 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9538 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9745 a = 0.9798 a = 0.9798 a = 0.9798 a = 0.9798 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9806 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9824 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9905 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9945 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9968 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9979 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0.9991 a = 0...

5   Pass
%% assert(isequal(foldl(@(x_, y_) 2*x_ + y_, 4, 1:3), 43));

a = 20 a = 43

6   Pass
%% handle 1 sumplusfive = foldl(@plus, 5); assert(isequal(sumplusfive(1:100), 5055));

a = 8 a = 11 a = 15 a = 20 a = 26 a = 33 a = 41 a = 50 a = 60 a = 71 a = 83 a = 96 a = 110 a = 125 a = 141 a = 158 a = 176 a = 195 a = 215 a = 236 a = 258 a = 281 a = 305 a = 330 a = 356 a = 383 a = 411 a = 440 a = 470 a = 501 a = 533 a = 566 a = 600 a = 635 a = 671 a = 708 a = 746 a = 785 a = 825 a = 866 a = 908 a = 951 a = 995 a = 1040 a = 1086 a = 1133 a = 1181 a = 1230 a = 1280 a = 1331 a = 1383 a = 1436 a = 1490 a = 1545 a = 1601 a = 1658 a = 1716 a = 1775 a = 1835 a = 1896 a = 1958 a = 2021 a = 2085 a = 2150 a = 2216 a = 2283 a = 2351 a = 2420 a = 2490 a = 2561 a = 2633 a = 2706 a = 2780 a = 2855 a = 2931 a = 3008 a = 3086 a = 3165 a = 3245 a = 3326 a = 3408 a = 3491 a = 3575 a = 3660 a = 3746 a = 3833 a = 3921 a = 4010 a = 4100 a = 4191 a = 4283 a = 4376 a = 4470 a = 4565 a = 4661 a = 4758 a = 4856 a = 4955 a = 5055

7   Pass
%% handle 2 concat = foldl(@(x_, y_) [x_ y_]); r1 = rand(1, 10); r2 = rand(1, 10); assert(isequal(concat(r1, r2), [r1 r2]));

a = Columns 1 through 9 0.6655 0.5199 0.8456 0.5948 0.7524 0.1167 0.9580 0.4398 0.1792 Columns 10 through 12 0.7958 0.8487 0.7308 a = Columns 1 through 9 0.6655 0.5199 0.8456 0.5948 0.7524 0.1167 0.9580 0.4398 0.1792 Columns 10 through 13 0.7958 0.8487 0.7308 0.3516 a = Columns 1 through 9 0.6655 0.5199 0.8456 0.5948 0.7524 0.1167 0.9580 0.4398 0.1792 Columns 10 through 14 0.7958 0.8487 0.7308 0.3516 0.1548 a = Columns 1 through 9 0.6655 0.5199 0.8456 0.5948 0.7524 0.1167 0.9580 0.4398 0.1792 Columns 10 through 15 0.7958 0.8487 0.7308 0.3516 0.1548 0.0163 a = Columns 1 through 9 0.6655 0.5199 0.8456 0.5948 0.7524 0.1167 0.9580 0.4398 0.1792 Columns 10 through 16 0.7958 0.8487 0.7308 0.3516 0.1548 0.0163 0.1529 a = Columns 1 through 9 0.6655 0.5199 0.8456 0.5948 0.7524 0.1167 0.9580 0.4398 0.1792 Columns 10 through 17 0.7958 0.8487 0.7308 0.3516 0.1548 0.0163 0.1529 0.2723 a = Columns 1 through 9 0.6655 0.5199 0.8456 0.5948 0.7524 0.1167 0.9580 0.4398 0.1792 Columns 10 through 18 0.7958 0.8487 0.7308 0.3516 0.1548 0.0163 0.1529 0.2723 0.9298 a = Columns 1 through 9 0.6655 0.5199 0.8456 0.5948 0.7524 0.1167 0.9580 0.4398 0.1792 Columns 10 through 18 0.7958 0.8487 0.7308 0.3516 0.1548 0.0163 0.1529 0.2723 0.9298 Column 19 0.4924 a = Columns 1 through 9 0.6655 0.5199 0.8456 0.5948 0.7524 0.1167 0.9580 0.4398 0.1792 Columns 10 through 18 0.7958 0.8487 0.7308 0.3516 0.1548 0.0163 0.1529 0.2723 0.9298 Columns 19 through 20 0.4924 0.3038