5 ms·
Neither language have proper tail call elimination, which, is absolutely insane to me. Yall really just write procedural code for everything?
by adenozine 5y ago
Neither language have proper tail call elimination, which, is absolutely insane to me.
Yall really just write procedural code for everything?
- adgjlsfhk1 5y agoif you want tail calls in Julia, there is a 3 line macro that gives it to you.
- tbenst 5y agoCould you point to it? Thanks
- ted_dunning 5y agoHere is one: https://github.com/TakekazuKATO/TailRec.jl https://github.com/TakekazuKATO/TailRec.jl It works by inspecting the code and rewriting a function to turn tail calls into loops. The interesting bit is that it was very easy to write because of the strong macros in Julia.
- adenozine 5y agoThis is eye opening. I might take a look at this in depth and try Julia again.
- nick__m 5y agoThis repo should interest you https://github.com/TakekazuKATO/TailRec.jl https://github.com/TakekazuKATO/TailRec.jl
- doliveira 5y agoYou must be in quite the functional bubble (I envy you for that, though)
- adenozine 5y agoJust Ruby... I use Python plenty, just not in large enough doses that I have to actually make peace with it.
- yakubin 5y agoActually, I've recently watched a talk by Guy Steele where he makes a case for tail call elimination being essential for object orientation: <https://www.youtube.com/watch?v=0hlBkQ5DjaY https://www.youtube.com/watch?v=0hlBkQ5DjaY>. He demonstrates how tail call elimination enables better separation of concerns, allowing you to write code where objects don't need to "peek" into one another. Another non-functional application of tail call elimination is finite state machines. Writing them as functions calling the next state in tail call position is very elegant, legible and efficient.
- nine_k 5y agoES6 used to require tail call elimination, and it was even shipped by Safari and Chrome back in 2016. Were it not for Firefox and Edge teams who torpedoed that feature, it would be a part of the major language of today. Maybe it still will be.
- deleted 5y ago[deleted]
- eximius 5y agoA true scotsman rolls their own callstack.
- freemint 5y agoI've never seen recursive looping over a thing get turned into efficient SIMD code in any language. Starting with loops has no good reason to be better able to achieve that but for practical compilers it makes a huge difference. Julia code might also uses a lot of in place operations which would be hard for a compiler to infer as safe.
- Der_Einzige 5y agoThis is why I find it so annoying that CS programs seem to worship functional programming (at least MY program did!). No, I AM going to write procedural code, and it WILL be faster than your "high IQ" 1 line recursive solution. Also funny to see how little recursion gets used in CUDA/Pytorch/GPU programming - which is what we are seeing to be more and more important over time.
- Vetch 5y agoFunctional programming is a lot more than just recursion and there's nothing about GPUs that precludes an immutable first array based language with functional programming as core. Google Research's Dex is an example.
- cookieater 5y agoSpeaking of functional programming and GPU's - check out Futhark :D. Very on the fringe but very interesting.
- ummonk 5y agoAnd functional paradigms have a way to express looping over a thing that's much better than recursion: filter / map / reduce.
- jhgb 5y ago> I've never seen recursive looping over a thing get turned into efficient SIMD code in any language. Starting with loops has no good reason to be better able to achieve that but for practical compilers it makes a huge difference. Well, for example at the very least in Common Lisp you'll have much more joy with higher-order functions than with loops. The simple reason for that is the existence of compiler macros (http://clhs.lisp.se/Body/03_bba.htm http://clhs.lisp.se/Body/03_bba.htm) which can replace function compositions with arbitrary code. And it's much easier to figure out what the function composition does than to write a loop vectorizer.
- hjtkfkfmr 5y agoReal world code rarely uses recursion, and if it does it's the kind that doesn't allow tail call optimization.
- creata 5y agoTail calls and while loops are essentially equivalent, so why care whether a language prefers one or the other?
- gleenn 5y agoBecause if you like functional programming, then avoiding loops and imperative code is desirable. Some of us hate seeing `for` and `while` because they also imply mutation of variables. Recursion is beautiful.
- Vetch 5y agoIt's like, why use one synonym and not the other? Sometimes it's because writing things in an immutable manner makes things clearer. Other code that can benefit are certain coroutines and generators.
- creata 5y ago> It's like, why use one synonym and not the other? Sometimes it's because writing things in an immutable manner makes things clearer. That's true in many situations, but in this case the equivalence between the two is so straightforward that I can't see recursion gaining you any clarity. Perhaps you have a concrete example?
- Vetch 5y agoI can't think of a specific example but I do know that there have been times where I have chosen to write a loop as tail recursion and times where I have used mutable variables and a while loop. Why one or the other might be recursion better capturing intent, preferring immutability, efficiency or just a matter of taste. Sometimes it makes more sense and is more flexible to package functionality as a recursive function, then mutation and variables feel like clutter. Other sometimes using recursion instead of a loop gains nothing expressively. Or sometimes structural recursion + pattern matching is the much more elegant approach in terms of communicating and capturing the essence of the algorithm.
- jhgb 5y ago
- fault1 5y agoit's fairly easy to just write a macro to do tailrec in julia at least; https://github.com/TakekazuKATO/TailRec.jl https://github.com/TakekazuKATO/TailRec.jl
- gleenn 5y agoLack of TCO is a JVM problem. All JVM languages are incapable for this reason. It's a very hard problem to fix apparently, I remember even asking one of the Sun JVM developers at a talk at my university and he said one of the issues was the Java security model and stack ownership IIRC. This was a long time ago though so I may be incorrect. The problem remains hard though.
- jolux 5y agoNeither PyTorch nor Julia run on the JVM.
- deleted 5y ago[deleted]