ijrussell / essential-fsharp

Markdown source of Essential F# ebook

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question about code: Chapter 5, Page 71

bytesource opened this issue · comments

Hi Ian,

I've just finished chapter 5, and it's been a great journey so far!

I've been using F# for some time, mainly to automate tasks, so I have a bit of experience with the language. Your teaching approach is helping me connect the dots and get a better understanding of the concepts.

For example, re-using a class instance, such as HttpClient wasn't clear to me, but thanks to your rnd() example I finally understood how it's done and why it's working.

The reason I'm opening this issue is that the example using reduce on page 71 returns a different result from the other implementations:

// Reduce is a partial function
match nums with
| [] -> 0
| items -> items |> List.reduce (fun acc v -> acc + if v % 2 = 1 then (v * v) else 0)

On the first iteration, acc holds the first element of the list. However, acc is not tested for "oddness" and not squared, just added to the sum.

Here is a gist with two tests: https://gist.github.com/bytesource/f53125a7fef451e333098c454b9d8179

Again, thanks for writing this book!

Stefan

Thanks for the kind words.
I've updated the book to resolve your issues.