hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

Basic Let question

Last post 08-01-2008, 7:53 by Nair. 4 replies.
Sort Posts: Previous Next
  •  07-31-2008, 8:00 6487

    Basic Let question

    I was reading 'Expert F#' by DonSyme book and when exprimenting one of the sameple code and I ran into a problem need help to
    procede. The code I am talking about is on 11, it is mentioned that with the use
    #light will take care of alignment and use of 'in'. But using fsi
    1.4.9.19 when I start typing in the code, the code liiks like the
    following

    let poweroffourplus2 n =
    let n = n * n
    let n = n* n
    When I entered the second line the fsi failed. I changed it and included 'in' in seond let to make sure may be it is because of #light still failed. I am bit confused. Could someone help me on this?
    Thanks
  •  07-31-2008, 8:30 6488 in reply to 6487

    Re: Basic Let question

    Nair:
    let poweroffourplus2 n =
    let n = n * n
    let n = n* n
    The #light syntax is very similar to Python in that whitespace is significant.  Therefore, the code must be indented - using spaces not tabs - to show code relationships.  The function also needs to return a value in order to be valid.  Finally, when using FSI the last line needs a double semcolon ";;" to end input:
     
    </DIV>
    <DIV>let poweroffourplus2 n =</DIV>
    <DIV>    let n = n * n</DIV>
    <DIV>    let n = n* n</DIV>
    <DIV>    n;;</DIV>
    <DIV>

    "It seems that perfection is reached, not when there is nothing left to add, but rather when nothing more can be taken away." -- Antoine de St. Exupéry
  •  07-31-2008, 9:13 6489 in reply to 6488

    Re: Basic Let question

    Thank you very much and yes Don also mentioned the same in the same paragraph and I was eager to try to code without reading all the meat. I have one another question, this is realted to funcation thinking what exactly happening to n in the above example

    when I use the first let (on the funcation definition) it is coming in as argument value. In the second let, the value of n is pass and creats a new n value (even though Don did mentioned the value of n doesn't change) here it is changing... now the next let uses n, it is using modified n instead of original n........

    so how the value 'n' is not changed.??

     

    Thanks again.

  •  07-31-2008, 15:07 6493 in reply to 6489

    Re: Basic Let question

    This is 'shadowing' - each new 'let n = ...' creates a new variable which hides the old one.  So

    let n = n * n

    let n = n * n

    is like

    let n_2 = n_1 * n_1

    let n_3 = n_2 * n_2

    only they all have the same name.  None of the objects referred to by the name n_i is being mutated.  Instead there are three different objects, and at different points in the code, the name 'n' refers to different ones of those objects.

  •  08-01-2008, 7:53 6500 in reply to 6493

    Re: Basic Let question

    Thank you very much. I have one more question, I realised if I don't understand this correctly I am going to assume too many things so here is one more

    in the following example

    let nbynplus5 n =

         let n = n * n

         let n = n + 5

         n;;

     

    In the above example, the let n = n * n will be let n_1 = n * n since it is the first time n is referenced and in the second let, it will be let n_2 = n_1 + 5. Is this correct so if I have make the code it would be

    let nbynplus5 n =

         let n_1 = n * n

         let n_2 = n_1 + 5

         n_2;;

     

    Is this correct? also why would be doing instead of using this modified code itself? Is it a style or coding standard?

    Thanks again for helping me started.

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems