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.

ivaliy (3) [Avatar] Offline
#1
Something is definetely wrong here and it is related not only to the function readAndAdd2 but to the explanation of option.map and option.bind as well.

// Listing 6.11: Adding two options using bind and map

let readAndAdd2() =
readInput() |> Option.bind (fun num -> // Process first input using 'bind'
readInput() |> Option.map ((+) num) ) // Process second input using 'map'


>>>>error FS0001: Type mismatch. Expecting a
Option<int> -> 'a
but given a
'b option -> 'c option
The type 'Option<int>' does not match the type ''a option'
Tomas Petricek (160) [Avatar] Offline
#2
Re: Ch6(p154 - p156 listing 6.11)-readAndAdd2 function is definitely incorrect
Hi,
I don't see any error in this piece of code, but I may be missing something!
Does the 'readInput' function in your environment have the right type? It should print:

> readInput;;
val it : (unit -> int option) = <fun:it@3>

Also, I see the 'Option<int>' type in the output, which is confusing as F# usually prints 'int option' when printing the type of standard F# option type. If you tried running your own defintion of the option type in FSI (e.g. the one on page 126), then the "Some" and "None" constructors of your type would hide the pre-defined F# constructors. As a result, your value 'Some 10' would be a value of your 'Option<int>' type (which is different to standard F# 'int option').

Hope this helps!
T.
ivaliy (3) [Avatar] Offline
#3
Re: Ch6(p154 - p156 listing 6.11)-readAndAdd2 function is definitely incorrect
Thanks Tomas, you're right, i've tried to run it in fsi and it worked, so i restarted the interactive mode and now it's OK.
Tomas Petricek (160) [Avatar] Offline
#4
Re: Ch6(p154 - p156 listing 6.11)-readAndAdd2 function is definitely incorrect
Great smilie, I'm glad I don't need to add anything to Errata, but if we'll going to do a second edition, we should definitely add some warning about this.
ivaliy (3) [Avatar] Offline
#5
Re: Ch6(p154 - p156 listing 6.11)-readAndAdd2 function is definitely incorrect
actually i have some anyway ) though they're minor.

p155 - missing closing ')' in the listing with option.map
p101 - listing 4.11 drawLabel
line: let size = gr.MeasureString(lbl, fnt) there is no declaration of lbl, so i believe there shoud be title instead.
Tomas Petricek (160) [Avatar] Offline
#6
Re: Ch6(p154 - p156 listing 6.11)-readAndAdd2 function is definitely incorrect
Yes, these two are actual bugs. Thanks for reporting them!

Tomas