annotate test/test_bug_31371.m @ 14331:9a276049f18b

maint: add file omitted from last commit
author John W. Eaton <jwe@octave.org>
date Sun, 05 Feb 2012 15:35:32 -0500
parents
children affda9a8f7d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14331
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 %!test
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2 %! % Work around MATLAB bug where f(x)(y) is invalid syntax
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
3 %! % (This bug does not apply to Octave)
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4 %! C = @(f,x) f(x);
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 %! C2 = @(f,x,y) f(x,y);
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 %!
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 %! % Church Booleans
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 %! T = @(t,f) t;
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 %! F = @(t,f) f;
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 %!
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 %! % Church Numerals in MATLAB
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12 %! Zero = @(f,x) x;
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 %! One = @(f,x) f(x);
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 %! Two = @(f,x) f(f(x));
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 %! Three = @(f,x) f(f(f(x)));
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 %! Four = @(f,x) f(f(f(f(x))));
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 %!
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18 %! % Arithmetic Operations
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 %! Inc = @(a) @(f,x) f(a(f,x)); % Increment
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20 %! Add = @(a,b) @(f,x) a(f,b(f,x));
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 %! Mult = @(a,b) @(f,x) a(@(x) b(f,x),x);
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 %! Dec = @(a) @(f,x) C(a(@(g) @(h) h(g(f)), @(u) x), @(u) u); % Decrement
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23 %! Sub = @(a,b) b(Dec, a);
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24 %!
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25 %! % Renderer - Convert church numeral to "real" number
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 %! render = @(n) n(@(n) n+1,0);
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 %!
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 %! % Predicates
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 %! iszero = @(n) n(@(x) F, T);
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30 %!
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 %! % Y combinator implements recursion
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 %! Y = @(f) C(@(g) f(@(x) C(g(g), x)), ...
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 %! @(g) f(@(x) C(g(g), x)));
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34 %!
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 %! Factorial = Y(@(f) @(n) C(C2(iszero(n), ...
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 %! @(d) One, @(d) Mult(n, f(Dec(n)))),0));
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 %!
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 %! assert (render(Factorial(Two), 2))
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 %! assert (render(Factorial(Three), 3))
9a276049f18b maint: add file omitted from last commit
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 %! assert (render(Factorial(Four), 12))