In this blog I talk about some of the personal work I do in programming. I mainly do Java and JavaScript, but I'm learning functional programming in Haskell too...

Monday, November 26, 2007

Structure of a functional Java, er, method

A method in FLOJ (remember, the functional Java based language I'm toying with) has a simple structure:- a method definition 100% similar to a normal Java method: visibility generic return-type type parameters.There are no exceptions, of course, since exceptions are things Best Done in Monads, once I've figured outout I'll implement monads.- a series of assignments of the form x=e where x is a variable name, and e an expression- a return expression

Since the return expression is always the last expression, there is no need for a return keyword, but it's accepted by the parser. In the exemple above, the method returns the list of String TOTO and TITI.Note also that the assignments do not allow nor require the type definition of the left hand side of the assignment. The type is inferred from thereturn type of the right hand side expression (in the exemple above, a is a list of String). It's not as powerful as the full blown Haskell type inference but at least it saves some typing.

Of course, the generated Java code will add a sneaky final keyword to all the assignments, to enforce the fact that there's no variables in a functional language.

All in all, that structure makes the code easy to read and to understand: the sequence of calculations and the final expression yielding the method result.

I don't think my FLOJ language will ever be a full blown industrial-strength language, but I know that by implementing my own functional language I will learn a lotmore about functional languages than by just using one. Just implementing method pointers, type inference, combinator parsers and such, I understand more the why and how of Haskell.