3 ms·
This is an inspiring project. It would have taken me far more than five weekends to create this, so you can feel smug knowing you're about ten times as efficien
by mkdir 14y ago
This is an inspiring project. It would have taken me far more than five weekends to create this, so you can feel smug knowing you're about ten times as efficient as a random HN member!
I have a couple of criticisms. First of all, strange things happen when a semicolon is omitted from the end of a function definition. In the following example, there is no output. What's going on?
square = { n |
n * n
}
println(square(5));
Second, according to the rules for omitting parentheses, it seems like this should output "25". Instead, it outputs "Function5":
square = { n |
n * n
};
println square(5);
It seems like it's passing "square" and 5 as separate parameters to println, which is unlikely to be the intended behavior in most cases.
Lastly, is recursion possible?
factorial = { n |
if (n < 2) { 1 }
{ n * factorial(n - 1) }
};
println(factorial(5));
That code generates the following error:
Error: function identifier expected ('Exp() :3:5' found) at line 3 char 5
Anyway, I don't mean to nitpick! Do you plan on developing this further?
- Udo 14y agoFirst: thanks for even trying it out! Yeah, if you don't leave off the final semicolon, it doesn't return the last value. In that case you would have to use a normal "return" statement. It's definitely a bug, but I left it in there for some time because I thought it was weirdly interesting. I'm going to get rid of that bug though in the next version. > Second, according to the rules for omitting parentheses Ah, I'm doing a bad job with the tutorial then. Parentheses don't work like they do in C-like languages. It's more like Lisp in that regard. Your last statement prints "square"="Function" because you're not invoking the function but getting the function pointer. The correct way would be (again, somewhat Lisp-like): square = { n | n * n }; println (square 5); Again, this would also be the solution to your last example. I should probably make a general syntax paragraph as part of the tutorial, especially for people coming from C-likes where invokation goes function(a) instead of (function a). Edit: there's a section on the site now to explain this, I hope this will make it easier.
- mkdir 14y agoActually—I think you did explain properly. I must confess that I jumped into the sandbox before reading the documentation, and then I only checked the documentation when ran into behavior I didn't expect. I do think the semicolon behavior is a bit confusing. The documentation explains that expressions aren't auto-returned if they're followed by a semicolon, but I don't think that quite covers the behavior I pointed out. I might be missing something else from the documentation, though... Also, I found one more major issue. Your documentation uses the numeric literal "3.1415" in the explanation for named parameters. That really needs to be "3.1416". :P
- Udo 14y ago> I do think the semicolon behavior is a bit confusing. I agree. Regarding the named params, the example is: f 3.1415 #name:"I'm using a named param!"; where 3.1415 is an unnamed parameter, and the 3rd line contains the named part. I'm probably showing too many things at once here. Also, there are definitely a few bugs still to iron out!
- mkdir 14y agoRegarding "3.1415", I was simply passive-aggressively pointing out that if you're referencing pi, you should probably round up to "3.1416". The example itself made sense!
- Udo 14y agoAh, I'm so dense! Sure, there is also "math.pi" as a built-in for that. (And I fixed this grave rounding error) I also discovered a scope problem with the recursion example you gave. Turns out, there is (of course) a major bug in there, thanks for discovering that. It's about the visibility of the "factorial" symbol itself, so the workaround is for now: factorial = { n | factorial = outer.factorial; if(n == 0) { 1 } { n * (factorial (n - 1)) } }; println (factorial 5); At least, until I restart the service (which I don't want to do while everyone is potentially using the tutorial).
- S4M 14y agoI also am super impressed that it took the OP only 5 week ends to do this. For me it would probably take 1 year if I don't give up on the way...
- Udo 14y agoTo be fair, I'm showing you something extremely half-baked here and I thought about the project a lot before starting to code anything.