sdiehl / write-you-a-haskell

Building a modern functional compiler from first principles. (http://dev.stephendiehl.com/fun/)
MIT License
3.35k stars 257 forks source link

Recursion Discussion #10

Closed dgstevens closed 9 years ago

dgstevens commented 9 years ago

I'll preface this by saying that my knowledge of Haskell is intermediate at best, but the discussion on recursion seems to fall somewhere between misleading and wrong. Many recursive functions are not tail-recursive - the factorial example isn't, for instance. It's possible to transform function to be tail-recursive, but I would be surprised if such transformations are the default behavior of any Haskell compiler at -O0. I especially would hesitate to speculate on the implementation of recursion due to Haskell's laziness.

As a side note, the mutually recursive examples have extraneous semicolons.

sdiehl commented 9 years ago

Entering a function in Haskell does not create a new stack frame, the logic of the function is simply entered with the arguments on the stack and yields result to the register.

There is no mention of tail recursion in this section explicitly for this reason. The only point I wanted to make is that stack frames are not created when jumping to entry code for a function.

dgstevens commented 9 years ago

So I just did some quick reading about Haskell's runtime. What's confusing to me about this section is that it mentions the Haskell stack but doesn't actually discuss that the stack in Haskell is different from stacks in most other languages. Without knowing that fact, the obvious conclusion from the fact that the factorial code can crash with a stack overflow is that the paragraph is wrong in some manner. And I will say that I'm probably a pretty good candidate for this tutorial - I've written a compiler for an imperative language before in Haskell, so I don't think my lack of knowledge about the Haskell runtime is unusual.

sdiehl commented 9 years ago

Well, I implemented a Haskell runtime in Chapter 23 anyways so I'm not going to stress of the exact phrasing of the introduction comment.

dgstevens commented 9 years ago

Just my two cents, but phrasing in the introduction of a tutorial is very important.

The fact that the tutorial discusses the Haskell runtime in Chapter 23 implies that Chapter 2 doesn't assume knowledge of the Haskell runtime in the introduction. And that in turn means that Chapter 2 could very likely be seen as wrong by the targets of the tutorial, which could lead people to lose confidence and abandon the tutorial.

Another example where phrasing is important in the introduction is Chapter 1's section on static typing. Those two examples are very intimidating for anyone that does not have a good grasp of type systems. So even though later chapters go over that material, I'm sure many readers will give up during the introduction because there is no indication that the tutorial does not assume they understand those examples.

sdiehl commented 9 years ago

I'm welcome to changing the phrasing or adding qualifiers. Submit a change for how you'd like to see the sentence changed.

https://github.com/sdiehl/write-you-a-haskell/blob/master/001_basics.md

The same goes for the examples on static typing.