The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Jason Kikel (4) [Avatar] Offline
Using a hypothetical language might cause create more trouble than good. If the reader is not a functional programmer, an additional new syntax might turn out to confuse him or her more.

Maybe just provide the F# code and explain how it works.
let rec SumNumbers from to =
  if from > to then 0
    let sumRest = SumNumbers(from + 1, to)
    from + sumRest

Or even, leave the hypothetical example in there and add the F# after it to complete the transition from C# style to F# style.
Jason Kikel (4) [Avatar] Offline
Re: Section 2.2.2
Sorry about the weird formatting. Code brackets seem to be broken.
Tomas Petricek (160) [Avatar] Offline
Re: Section 2.2.2
this has also changed since the last update smilie. The new version shows how a simiar thing would look using standard C# switch and then using pattern matching in F# (after showing a declaration of the discriminated union).

davidmorton (1) [Avatar] Offline
Re: Section 2.2.2
When I was looking over the "fictional language" it did occur to me that escaping C# isn't really necessary at all here.

static int SumNumbers(int x, int y)
return x > y ? 0 : x + SumNumbers(x + 1, y);

Is perfectly legal and compilable in C#, although I'd certainly agree with the switch to F#. F# uses a while loop to handle tail recursion and the example I posted above is very prone to a StackOverflowException.

Message was edited by:
Tomas Petricek (160) [Avatar] Offline
Re: Section 2.2.2
this is really difficult choice - I changed it back to standard C# and then back to pseudo-C# again. I didn't want to start with F#, because I wanted to make the chapter 2 as "light" as possible and the F# syntax could be difficult to read for some readers in the beginning.

Anyway, the reason why I ended up with pseudo C# is that I really want to be able to demonstrate how let-bindings (that are also expressions) work - the fact that you can write (using the pseudo-C#):

return x > y ? 0 : { var rest = SumNumbers(x+1, y) in x + rest };