Types and effects in FJ
Advertisement
One of the guiding principle for this language to have it be a lightweight language to learn functional programming in (I am learning alot from implementing it too =). So it was a difficult debate with myself whether or not to allow effects. In a later post I will talk about why I chose to implement effects into the language and go over the language. Nonetheless I chose to make the syntax such that it was clear that you were not manipulating variables but reference cells. The syntax for them is verbose compared to the rest of the language to discourage their use.
Some things Ive done is to add types - there is proper type checking - matching if branches, assignemnt, operations, while statements, functions etc. Which has actually aided in developing it. Also added let bindings. The guessing game in full:
The pi calculator: Two versions. The top one is slower than the bottom one. One thing that I like about how I implemented the while statement is that it is like a function, it returns the value of the last term that was evaluated. So this:
New targets
Add variants. and basic pattern matching on variants.
I suggest that the next program to try should be a Rock Paper Scissors game.
Accessing .net objects. Im thinking of the following syntax: handle = dotNetLoad "assembly"; then dotNetInvoke handle "methodnameasstring". As this is an interpreted language there will not be getting around using reflection to access libraries. when actually using the language as library I suspect having a way also to add objects to a heap that the script can access may be better. Will have to think about about the syntax and semantics of that. will probably need to support objects.
Some things Ive done is to add types - there is proper type checking - matching if branches, assignemnt, operations, while statements, functions etc. Which has actually aided in developing it. Also added let bindings. The guessing game in full:
guess = NextRandom 200.0;Print "Guess a number between 0 and 200\n\n";letrec game : float -> float -> unit = function r : float | tries : float. if r == guess then ( Print "Got It. Took: " ; Print tries; Print " tries.") else ( if r > guess then Print "Too High, Try again...\n" else Print "Too Low, Try again...\n" ; game (Convert ReadInput To float) (tries + 1.0) ) in game (Convert ReadInput To float) 0.0;The pi calculator: Two versions. The top one is slower than the bottom one. One thing that I like about how I implemented the while statement is that it is like a function, it returns the value of the last term that was evaluated. So this:
z = while (@k) < 2.0 do r <- @k * @r ; @k; is whatever value k was at the end of the loop. The evaluation takes on average .5 seconds which is quite a bit slower than the other but alot more is going on as I will explain in a future post. However I am also counting the file loading and pretty printing which probable adds quite a bit.val = mutable_cell 0.0;den = mutable_cell 1.0;isplus = mutable_cell true; one = mutable_cell 1.0;while (@den) < 10000.0 do ( val <- (@val) + (@one) * (4.0 / (@den)); den <- (@den) + -2.0; one <- neg (@one) ); //////////////////////////////// - = function a : float | b : float . a + -b;not b = fun a : bool . if a then false else true; pi = while (@den) < 10000.0 do ( if (@isplus) then val <- (@val) + (4.0 / (@den)) else val <- (@val) [-] (4.0 / (@den)) ; den <- (@den) + 2.0; isplus <- not (@isplus) ; @val ); Print pi; New targets
Add variants. and basic pattern matching on variants.
I suggest that the next program to try should be a Rock Paper Scissors game.
Accessing .net objects. Im thinking of the following syntax: handle = dotNetLoad "assembly"; then dotNetInvoke handle "methodnameasstring". As this is an interpreted language there will not be getting around using reflection to access libraries. when actually using the language as library I suspect having a way also to add objects to a heap that the script can access may be better. Will have to think about about the syntax and semantics of that. will probably need to support objects.
Advertisement
Advertisement
Advertisement
Discussion