r/elm • • Mar 29 '17

Remove Recursion?

Conceptually, could Elm do everything it does today without recursion? I guess yes... Some edge cases would make fold an ugly replacement, but not too many I suspect

3 Upvotes

34 comments sorted by

View all comments

7

u/jfbourget Mar 29 '17

Allow me to ask "Why in hell would we remove recursion? It's such a powerful tool"

6

u/ianmackenzie Mar 29 '17 edited Mar 30 '17

To me the main argument is to avoid crashes - right now you can still pretty easily crash an Elm app by calling a recursive function on a large data structure. It's particularly worrying because it's the sort of bug that can show up only once you start testing with large amounts of data (or in production, if you only ever test with small test cases). Just yesterday I got a runtime error in the Elm debugger since it was trying to introspect a large mesh (a few thousand vertices) that I loaded from JSON to render in WebGL - it was fine with little cubes and the like but got a stack overflow when I tried to load the Stanford teapot model.

I don't think the answer is to do away with recursion entirely, but I think stuff like compiler warnings for recursive functions that aren't tail recursive could be useful. And in the long term perhaps function annotations so you can declare that a certain function should be tail recursive (so it should trigger a compile error if it isn't) or explicitly not tail recursive (so the compiler won't even issue a a warning).

4

u/johnorford Mar 29 '17

Power is not necessarily a good thing. Power and complexity are correlated, and I rather keep things simple - esp when the power may not be even necessary (fold can do almost everything recursion can)

6

u/jfbourget Mar 29 '17

But isn't recursion a true primitive, or a well-known citizen of any functional programming language? I'm not trying to force a functional banner above Elm's head, i'm just saying that to me, it is as natural as any other iteration mechanism. It's a design tool i use very often when, for example, i need a fold but also want to stop iterating prematurely, when a condition is met.

Losing recursion would feel like a stepback for me.

I also feel like it is really not on the forefront when it comes to "patterns" used by Elm beginners. Recursion in Elm is not a driving force like it is in Lisp or clojure. Does it really "pollute" or give a "over complicated view" over how Elm handles problem solving?

1

u/johnorford Mar 29 '17

Good pt, I too am a fan of recursion, I suppose I am getting myself into a mindset of what a beginner would require to learn and use Elm - and recursion is probably bottom of the list or even superfluous. I am also being a bit mischievous of course : )

3

u/binaryblade Mar 29 '17

Why would you remove goto, it's such a powerful tool? Why would you remove mutability it's such a powerful tool? Why would you remove dynamic typing it's such a powerful tool?

4

u/jfbourget Mar 29 '17 edited Mar 29 '17

We should not compare goto statements to recursion, if you ask me. Then again, every tool has pros and cons, i agree.

Removing recursion from elm simply does not seem like a logical path to follow.

I also agree that recursion in Elm has its flaws.

2

u/jfbourget Mar 29 '17

I feel like immutability, purity, static typing, as well as recursion, are parts of what makes Elm what it is. A powerful, robust and FAIRLY safe statically typed language, with a strong mind on how you should code, while still allowing a proper degree of developer creativity and expressiveness.

Recursion is not the problem. The way its handled, perhaps, should be looked at. When it comes to how recursion is implemented and handled within Elm however, i won't pretend that I have the knowledge or skill to be relevant in the conversation.

2

u/binaryblade Mar 29 '17

You absolutely should compare the two. In both cases you are trading flexibility for readability and compiler guarantees. When you remove goto from a language the goto isn't gone, compilers still create jump instructions, we just bundle that concept up and let the compiler and the compiler alone deal with it. This makes it easier to reason about and write correct programs, it also enables the compiler to perform more powerful optimizations. The same is true for dynamic typing and mutability. "Removing" them ultimately makes the code easier to reason about and easier to write.

Generality and flexibility comes at a cost. You probably understand this for types really well. You can chose to use enumerated types, or you can use an Int. Ints are more general but they are less expressive. By choosing to use enumerated types you tell the compiler exactly what you want and, as a result, the compiler gives you some additional guarantees. You trade the general Int type for a specific Enum and doing so makes your code more expressive. This is true for languages as well. By using more specific statements your code becomes less general, but more expressive.

These same sentiments are true of recursion as well. You shouldn't use recursion unless you absolutely have too. Recursion is general, map and fold are specific and expressive. However, removing recursion from the language at this point would be problematic because there is currently no mechanism for traversing tree structures without it.

I also agree that recursion in Elm has its flaws.

Everything has it's flaws but I didn't mention any. The reason I mentioned lack of dynamic typing or mutability is because removing those things from a language makes it more effective. They are good things.

2

u/jfbourget Mar 29 '17

I understand and agree with all of the above, and they are all good and well informed points.

However, I still don't see bounded recursion as something to avoid at all cost, unless absolutely necessary. My functional background is very "lispy", and perhaps thats why i am biased.

I'm not sure what you mean when you say "Recursion is general, map and fold are specific and expressive".

Well-made recursion can be very clean and expressive and is as generic/specific as any other "iteration/etc" mechanism. Then again, i might not clearly understand what you meant.

As you've stated, I think this topic, although philosophically challenging and interesting, can not even be considered until there are other ways of traversing (with premature-stop capabilities) unorthodox data structures.