Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is using the base key word causing this error?

Tags:

f#

I am trying to do an example for Programming F# by O'Railey, Chris Smith page 53.

It is working with functions returning functions.

This line straight from the book in the VS2013 IDE Editor, FSI and LinqPad4 are giving an error:

Code:

let generatePowerOfFunc base = (fun exponent -> base ** exponent)

Error:

error FS0010: Unexpected keyword 'base' in pattern

What am I missing or is there something the author did not include that needs to be included.

like image 476
Cubicle.Jockey Avatar asked Feb 24 '26 00:02

Cubicle.Jockey


1 Answers

I strongly suspect it's merely a matter of base not being a keyword when the book was written.

Try a different identifier:

let generatePowerOfFunc b = (fun exponent -> b ** exponent)

Assuming you've got the 2009 edition of Programming F#, that would be before F# 2.0 was released (although after 1.0). I'm trying to find out exactly when it was introduced as a keyword...

EDIT: Actually, looking at this version of the spec which was written in 2009, it looks like base was already a keyword at that point. I wonder whether the original code was written significantly before the book was published.

Either way, I think it's reasonable to treat it basically as an error, and using a valid identifier instead should be fine.

EDIT: It's actually listed in the book's errata:

Example 3-3 does not work as-is in VS 2010. "base" is apparently a keyword, so it should have been escaped or there is some voodoo to make it not a keyword that I've missed in the book. Line 2 of the example should look like this:

   let generatePowerOfFunc ``base`` = (fun exponent -> ``base`` ** exponent);;

Alternatively, a different variable name should be chosen.

Note from the Author or Editor:
Thanks for the feedback, I must have missed the keyword being marked as reserved late in the product cycle.

In a future version of the book I'll have it read:

 let generatePowerOfFunc baseValue = (fun exponent -> baseValue ** exponent);;
like image 158
Jon Skeet Avatar answered Feb 26 '26 09:02

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!