3 ms·
" Erlang doesn’t have loops — it uses recursion for everything " Doesn't this make the language a bit of a brain tease! What harm can a For Loop do? The best
by systems 18y ago
" Erlang doesn’t have loops — it uses recursion for everything "
Doesn't this make the language a bit of a brain tease! What harm can a For Loop do?
The best argument I heard against static typing, that for many tasks the type system forces you to write solutions that cater for the type system and forces you to create boilerplate code.
Isn't this "no loop" statement thing kind of the same, you have to reforumulate your solutions to cater for and use tail recusion, similarly to how you might have to reformulate your solutions to support a type system constraints
I prefer how OCaml allows you to program in different paradigms, in general thought OCaml does enforce many constraints, but its hybrid nature is a step in the right direction
I also like how Groovy (and Perl6) support optional typing.
- hassy 18y ago`map` covers the majority of what for-loops are used in imperative languages. There's also `foreach`, which is a `map` that doesn't give you a new list back. You've also got list comprehensions if you prefer them over `map`. Then there are also `filter`, `all`, `any` and others. The rest of your iteration needs can be covered by the `fold` functions. Sure, these functions use tail-recursion internally, but you never have to care about that.
- iamwil 18y agoI also had a reaction when I started using Ruby and being introduced to functional programming concepts. I read blog posts http://notes-on-haskell.blogspot.com/2007/02/whats-wrong-with-for-loop.html http://notes-on-haskell.blogspot.com/2007/02/whats-wrong-wit... about people saying you don't really need for loops. I thought...well, how do you get anything done? Ends up that most things you care about can be done with iterators like each, map, and inject (like a fold). On occasion, you'll need to break out the indicies, but for most things you don't need them. Raganwald's talked about unfolding, which is pretty interesting as well. I suggest you go and check it out, if not him making a comment about it here.
- daleharvey 18y agoErlang also has an optional type system, you can put type hints at function heads (these can also be built using typer, which is built into erlang). there is also dialyzer which does static type analysis. It doesnt use loops because it doesnt have variables, once a 'Variable' has been bound to a value, it cant be changed
- KirinDave 18y agoFor loops require that you allow reassignment of named variables or mutability of the underlying objects, neither of which is allowed in Erlang. Erlang variable assignment follows a pattern matching semantic, so locals cannot be reassigned. And all values are immutable in Erlang, meaning that you can't change the value of something "Below" the covers. Therefore, for loops are pretty much impossible unless under the covers you map to function calls. And at that point, if you have tail recursion, why not just write it that way yourself? It's almost always less code to write a tail-recursive iteration vs. an explicit iteration in a language with pattern matching.