Cody

# Problem 201. Mimic foldl in functional programming

Solution 132358

Submitted on 29 Aug 2012 by Khaled Hamed
• Size: 38
• This is the leading solution.
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));

start = 0

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

start = 0

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

start = 1

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

start = -Inf

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

start = 4

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

start = 5

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]));

start = Columns 1 through 6 0.5449 0.6862 0.8936 0.0548 0.3037 0.0462 Columns 7 through 10 0.1955 0.7202 0.7218 0.8778