For some reason the parser breaks with that factorial definition, but adding spaces in between `a-1` seem to do the trick:
let fact a =
if a == 0 then
1
else
(fact (a - 1)) * a
console.log (fact 5)
Now, it's true that the Haskell syntax does not make that much sense if there is no partial application. I think that the variadic-arguments-are-always-allowed nature of JavaScript is not very compatible with partial application though.
> I think that the variadic-arguments-are-always-allowed nature of JavaScript is not very compatible with partial application though.
It could be implemented by compiling Roy functions to chains of 1-arg functions, but the amount of recursion and funcalling would probably be expensive (JS is not very good at these).
A SmartEnoughCompiler® you could also see partial application points and craft a partial application right there:
let add x y = x + y
console.log (add 1 32)
let add2 = add 2
console.log (add2 5)
var add = function (x, y) { return x + y; };
console.log(add(1, 32));
var add2 = function (y) { return add(2, y); };
console.log(add2(5));
As long as the number of arguments can be understood at compile time (which it can), it's not that hard: if the number of arguments provided make the function's arity reach 0 you partially apply it (and decrease the arity of the result), otherwise you fully apply it.
Comments
For some reason the parser breaks with that factorial definition, but adding spaces in between `a-1` seem to do the trick:
Now, it's true that the Haskell syntax does not make that much sense if there is no partial application. I think that the variadic-arguments-are-always-allowed nature of JavaScript is not very compatible with partial application though.> I think that the variadic-arguments-are-always-allowed nature of JavaScript is not very compatible with partial application though.
It could be implemented by compiling Roy functions to chains of 1-arg functions, but the amount of recursion and funcalling would probably be expensive (JS is not very good at these).
A SmartEnoughCompiler® you could also see partial application points and craft a partial application right there:
As long as the number of arguments can be understood at compile time (which it can), it's not that hard: if the number of arguments provided make the function's arity reach 0 you partially apply it (and decrease the arity of the result), otherwise you fully apply it.I decided against outputting like that a while ago but after seeing that example, I can't think of a great reason against it.
I'll have a go and see how well it works. Thanks!