Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras » Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/Apply

Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/Apply

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras
Filename: Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/Apply
Read OK:
File size: 015D bytes
Load address: 0000
Exec address: 0000
File contents
/* Example Apply */
/* functions and vectors can be arguments or components */

apply(pair)
{
 return pair[function](pair[argument]);
}

f(x)               // f takes functions as arguments
{ return x(0); }  

g(x)
{ return (100+x); }

main()
{
 enum { function, argument };
 x = vector { f; g; };  // a vector of functions
 print(apply(x),"\n");
}
00000000  2f 2a 20 45 78 61 6d 70  6c 65 20 41 70 70 6c 79  |/* Example Apply|
00000010  20 2a 2f 0a 2f 2a 20 66  75 6e 63 74 69 6f 6e 73  | */./* functions|
00000020  20 61 6e 64 20 76 65 63  74 6f 72 73 20 63 61 6e  | and vectors can|
00000030  20 62 65 20 61 72 67 75  6d 65 6e 74 73 20 6f 72  | be arguments or|
00000040  20 63 6f 6d 70 6f 6e 65  6e 74 73 20 2a 2f 0a 0a  | components */..|
00000050  61 70 70 6c 79 28 70 61  69 72 29 0a 7b 0a 20 72  |apply(pair).{. r|
00000060  65 74 75 72 6e 20 70 61  69 72 5b 66 75 6e 63 74  |eturn pair[funct|
00000070  69 6f 6e 5d 28 70 61 69  72 5b 61 72 67 75 6d 65  |ion](pair[argume|
00000080  6e 74 5d 29 3b 0a 7d 0a  0a 66 28 78 29 20 20 20  |nt]);.}..f(x)   |
00000090  20 20 20 20 20 20 20 20  20 20 20 20 2f 2f 20 66  |            // f|
000000a0  20 74 61 6b 65 73 20 66  75 6e 63 74 69 6f 6e 73  | takes functions|
000000b0  20 61 73 20 61 72 67 75  6d 65 6e 74 73 0a 7b 20  | as arguments.{ |
000000c0  72 65 74 75 72 6e 20 78  28 30 29 3b 20 7d 20 20  |return x(0); }  |
000000d0  0a 0a 67 28 78 29 0a 7b  20 72 65 74 75 72 6e 20  |..g(x).{ return |
000000e0  28 31 30 30 2b 78 29 3b  20 7d 0a 0a 6d 61 69 6e  |(100+x); }..main|
000000f0  28 29 0a 7b 0a 20 65 6e  75 6d 20 7b 20 66 75 6e  |().{. enum { fun|
00000100  63 74 69 6f 6e 2c 20 61  72 67 75 6d 65 6e 74 20  |ction, argument |
00000110  7d 3b 0a 20 78 20 3d 20  76 65 63 74 6f 72 20 7b  |};. x = vector {|
00000120  20 66 3b 20 67 3b 20 7d  3b 20 20 2f 2f 20 61 20  | f; g; };  // a |
00000130  76 65 63 74 6f 72 20 6f  66 20 66 75 6e 63 74 69  |vector of functi|
00000140  6f 6e 73 0a 20 70 72 69  6e 74 28 61 70 70 6c 79  |ons. print(apply|
00000150  28 78 29 2c 22 5c 6e 22  29 3b 0a 7d 0a           |(x),"\n");.}.|
0000015d