Cody

Problem 201. Mimic foldl in functional programming

Solution 480592

Submitted on 30 Jul 2014
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   Fail
%% summation assert(isequal(foldl(@plus, 0, 1:100), 5050));

Error: Undefined function or variable "list".

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

Error: Undefined function or variable "list".

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

Error: Undefined function or variable "list".

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

Error: Undefined function or variable "list".

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

Error: Undefined function or variable "list".

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

[Warning: Function diary has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function license has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function mex has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function home has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function keyboard has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function more has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function pause has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function simulink has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] Saving to: /users/msssystem1/matlab.mat

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

[Warning: Function diary has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function license has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function mex has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function home has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function keyboard has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function more has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function pause has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function simulink has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In foldl at 8 In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] Saving to: /users/msssystem1/matlab.mat

Suggested Problems

More from this Author1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!