# HG changeset patch # User John W. Eaton # Date 1328474132 18000 # Node ID 9a276049f18b31efc0e04fd9de394c2df407abc4 # Parent 23782766da081e95797673e53c7f381c2a6b5c60 maint: add file omitted from last commit diff -r 23782766da08 -r 9a276049f18b test/test_bug_31371.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/test_bug_31371.m Sun Feb 05 15:35:32 2012 -0500 @@ -0,0 +1,40 @@ +%!test +%! % Work around MATLAB bug where f(x)(y) is invalid syntax +%! % (This bug does not apply to Octave) +%! C = @(f,x) f(x); +%! C2 = @(f,x,y) f(x,y); +%! +%! % Church Booleans +%! T = @(t,f) t; +%! F = @(t,f) f; +%! +%! % Church Numerals in MATLAB +%! Zero = @(f,x) x; +%! One = @(f,x) f(x); +%! Two = @(f,x) f(f(x)); +%! Three = @(f,x) f(f(f(x))); +%! Four = @(f,x) f(f(f(f(x)))); +%! +%! % Arithmetic Operations +%! Inc = @(a) @(f,x) f(a(f,x)); % Increment +%! Add = @(a,b) @(f,x) a(f,b(f,x)); +%! Mult = @(a,b) @(f,x) a(@(x) b(f,x),x); +%! Dec = @(a) @(f,x) C(a(@(g) @(h) h(g(f)), @(u) x), @(u) u); % Decrement +%! Sub = @(a,b) b(Dec, a); +%! +%! % Renderer - Convert church numeral to "real" number +%! render = @(n) n(@(n) n+1,0); +%! +%! % Predicates +%! iszero = @(n) n(@(x) F, T); +%! +%! % Y combinator implements recursion +%! Y = @(f) C(@(g) f(@(x) C(g(g), x)), ... +%! @(g) f(@(x) C(g(g), x))); +%! +%! Factorial = Y(@(f) @(n) C(C2(iszero(n), ... +%! @(d) One, @(d) Mult(n, f(Dec(n)))),0)); +%! +%! assert (render(Factorial(Two), 2)) +%! assert (render(Factorial(Three), 3)) +%! assert (render(Factorial(Four), 12))