Since Perl's sub calls are slow, I decided to try compiling FP def's down to single, heinous Perl functions. As I suspected, this turns out to be much faster than the other implementation, though debugging is much more of a challenge.

Each code generating function should return an expression that will evaluate to its result in list context, and that has enough parens around it to avoid confusing Perl's parser.

The functions should use temporaries where necessary to avoid evaluating any of its arguments more than once. These temporaries cannot be references, since the arguments generally won't be real arrays, but expressions producing them.

NAME

Language::FP -- think like Jonh Backus wants you to

SYNOPSIS

use Language::FP qw/perl2fp/;

# Sum of the first 12 integers:
my $sum = fp_eval '/+ . iota:12'
print perl2fp($result);
# prints '<78>'

# Matrix-vector product:
fp_eval 'def Ax = @(+ . @* . trans) . distr';
my @mv = ([[1, 2], [3, 4]], [5, 6]);
print perl2fp(fp_eval('Ax:' . perl2fp(@mv)));
# prints '<17 39>'

# Cross-language calls:
print join ', ', Ax(@mv);
# prints '17, 39'

sub cubes { map { $_ ** 3 } @_ }
print perl2fp(fp_eval 'cubes:<1 2 3>');
# prints '<1 8 27>'

fp_eval in => \*INPUT, out => \*OUTPUT;

DESCRIPTION

Language::FP is an implementation of John Backus' FP language, a purely functional language remarkable for its lack of named variables -- only functions have names. Note that this is not a deliberately obfuscated language -- it was designed for actual users (probably mathematicians). Since Perl's $calars, @rrays and %ashes advertise themselves so boldly, I thought programming in a language whose author thought that named variables led only to confusion and error would be eye-opening. I now know why every language since has had named variables.

While at some point I should probably include a brief FP tutorial, for the moment please see http://www.cse.sc.edu/~bays/FPlink for more information on the language's history and basic functions. There are a number of subtle syntactic variants of FP described and implemented on the web. This unfortunate state of affairs is due at least in part to the original language's use of non-ASCII characters. This package uses a hybrid chosen to be somewhat: (1) legible, (2) faithful to the original, and (3) predictable to those familiar with Perl.

Functions

The following functions are useful in evaluating FP expressions and handling FP data.

$str = perl2fp @array

Convert a Perl list-of-lists (LoL) to a string represeting it in FP.

@array = fp2perl $str

Convert an FP value to a Perl LoL.

fp_eval in => \*IFH, out => \*OFH

Evaluate the contents of IFH (STDIN by default), writing the results to OFH (STDOUT by default).

$result = fp_eval $string

Evaluate the FP expression $string, returning the result as a Perl scalar or reference to a LoL.

In addition, all FP builtin functions (not combining forms) may be called as Perl functions in list context. For example, to use distl in Perl, one could write

my @result = Language::FP::distl $x, @ys

Debugging

You will experience unexpected behavior when programming in FP. Some of it may even be your fault. When this occurs, setting the global variable $::FP_DEBUG to a string containing one or more of the following characters can help:

'p' -- Trace parsing
'r' -- Trace execution
'b' -- Make FP errors ("bottom") fatal
'C' -- Use the slower, closure-based evaluator.

EXPORTS

Language::FP exports the fp_eval function by default, for command-line convenience.

TODO

Documentation -- a lot more needs to be explained a lot better.

Testing -- getting better, but still needs work.

Maybe make it more "OO" -- not that important.

BUGS

While calling user-defined Perl functions from FP works as expected, it is currently not possible to call Perl builtins.

Argument context is a mess in places.

AUTHOR

Sean O'Rourke, <seano@cpan.org>

Bug reports welcome, patches even more welcome.

COPYRIGHT

Copyright (C) 2002 Sean O'Rourke. All rights reserved, some wrongs reversed. This module is distributed under the same terms as Perl itself. Let me know if you actually find it useful.

APPENDIX

For further study, here is an implementation of Euler's totient function, which computes the number of co-primes less than its argument. This may be the longest FP program ever written.

 def totient = /+ . @((== . [1, `1] -> `1 ; `0) .
	(while (> . [2, `0]) (< -> reverse ; id) . [2, -]))
	. distl . [id, iota]