4 ms·
I programmed in Lisp back in 1985 and liked it a lot. Today I consider it too imperative for my tastes, and also too heavyweight. So lately I've been embeddin
by fexl 16y ago
I programmed in Lisp back in 1985 and liked it a lot. Today I consider it too imperative for my tastes, and also too heavyweight. So lately I've been embedding a little interpreter for my Fexl language ( http://fexl.com http://fexl.com ) into my systems written in Perl. That way I can opportunistically use a pure functional language for script-like purposes such as dynamic web sites and report generation, without committing to rewriting my entire system in another language. The Fexl interpreter is tiny and only has 7 grammar rules, so it's really easy to deploy in very focused areas of the system.
It's going well so far in testing. I suspect that over time I will migrate an increasing amount of system logic out of Perl and into Fexl, so eventually all that remains in Perl is the Fexl interpreter itself. Then I'll swap that out for my interpreter written in C and be done with Perl.
Don't get me wrong -- I've been using Perl for 12 years and I know how to write disciplined code in it, so I am grateful to Perl. But I find the pure functional approach to be liberating, making many tasks seem almost effortless.
In fact, I've even been applying the functional concepts to my Perl code itself, with great reward. For example, when I'm parsing text, I don't use the usual destructive position pointer. Instead, I use a functional representation of the text, with head, tail, prefix, suffix, and such. It's liberating.
Another example: when I use my key-value maps with automatic hierarchical structuring by disjoint prefix, I no longer use a destructive "put" operation to change entries. Instead, the "put" operation in effect returns a brand new key-value map with just that one entry changed. It's amazing how efficient this is because of structure sharing, and it also adds much clarity to the code and avoids a lot of thorny implementation issues (especially because I actually need a super-efficient "virtual copy" of large key-value maps for what I'm doing.)
So my main points here are:
1. You can deploy a small embedded interpreter for a purely functional language, inside a system written in any language (yes, even Perl). You don't have to rewrite your entire system in a "pure" language all in one fell swoop, or ever. You can just systematically migrate more logic into the functional side, completely at your leisure, in an evolutionary fashion.
2. You can apply functional concepts and techniques to code written in any language (yes, even Perl). That works even if you don't go as far as using an embedded interpreter.
I am finding both of these techniques to be highly rewarding, and the most exciting thing I've done since I started programming in 1975.
- Imbue 16y agoIs Fexl open-source, or even available for download? It looks pretty neat.
- fexl 16y agoFunny, I was just thinking about that very thing when I noticed your post. Problem is, I'm a little "ashamed" to release the code right now because I'm in the middle of some major revisions: 1. Use DeBruin notation for referring to bound lambda values positionally, instead of my current approach of actually using the lambda symbol in an associative list. (Don't laugh, it works.) 2. Change the C implementation so it just uses an array of longs for the whole working space, with integer offsets, instead of node structs with pointers. At that point the whole implementation will be "nothin' but int", and avoid malloc altogether. 3. Change both the Perl and C implementations so that I supply external function definitions as function pointers. Right now when a high-order function is reduced to a normal form (lambda form), I actually look at the name of the external function being called and do a big if-else on that. (Don't laugh, it works.) This will no longer be an option when I go with DeBruin notation. Besides, the function pointer approach will be a lot more solid, serious, and extensible. Thanks for expressing an interest. When I'm a little more proud of my code and have something available for download, I'll announce it on HN.
- Imbue 16y agoThis is probably a stupid question. If you have Fexl written in Perl now, what are the chances of just throwing a sand-boxed interpreter online for people to play with? I'm thinking just a form with an input field and a submit button which runs the script and displays any printed values.
- fexl 16y agoIt's a brilliant question, and it's now on my TODO list. I'll aim to get this done by the end of August. You're right about the sandboxing too -- the Fexl interpreter runs with two limits: the maximum number of cycles, and the maximum amount of memory. If it reaches either of those limits, it halts. So it should be perfectly safe to run it on the public web.