I'm guessing here.

I think it has something to with the fact that you want to be able to define local bindings that have the same name of bindings in some currently open module.
e.g.:

1
2
3
4
5
6
open MyModuleWithXYZ


let myFun x =
    let XYZ = 12
    x + XYZ

The local XYZ hides the definition in the open module, which is good.
I agree the fact that you can redefine bindings within the same scope is objectionable, but maybe we are missing something...

By on 5/29/2010 8:17 AM ()

this is one thing I would say the F# has gone to far.

it actual is this:

1
2
3
4
5
6
7
8
 

let x = 1 in

  let x = 2 in

   let x = 3

Now it is clear that it is not in the same scope.

By on 5/29/2010 9:37 AM ()

Interesting point Gary.

Robert Pickering puts it differently (Foundations of F#, pp. 22-23):

Identifiers within functions behave a little differently from identifiers at the top level,
because they can be redefined using the let keyword. This is useful; it means that the F#
programmer does not have to keep inventing names to hold intermediate values. To demonstrate,
the next example shows a mathematical puzzle implemented as an F# function. Here
you need to calculate lots of intermediate values that you don’t particularly care about; inventing
names for each one these would be an unnecessary burden on the programmer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
let mathsPuzzle() =
    print_string "Enter day of the month on which you were born: "
    let input = read_int ()
    let x = input * 4 // Multiply it by 4
    let x = x + 13 // Add 13
    let x = x * 25 // Multiply the result by 25
    let x = x - 200 // Subtract 200
    print_string "Enter number of the month you were born: "
    let input = read_int ()
    let x = x + input
    let x = x * 2 // Multiply by 2
    let x = x - 40 // Subtract 40
    let x = x * 50 // Multiply the result by 50
    print_string "Enter last two digits of the year of your birth: "
    let input = read_int ()
    let x = x + input
    let x = x - 10500 // Finally, subtract 10,500
    printf "Date of birth (ddmmyy): %i" x

Undoubtedly there are situations where this is useful. I'm sure it was a tough choice for the language team.

By on 5/29/2010 9:45 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper