hubFS: THE place for F#

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

type with Array, Array2 and member: pure functional code or not?

Last post 09-07-2008, 16:04 by jonaas. 2 replies.
Sort Posts: Previous Next
  •  09-07-2008, 11:08 6951

    type with Array, Array2 and member: pure functional code or not?

    If a have no good idea What is exactly «pure functionnal» programming

    If I write F# code:
    ========================================================
        type nc_data_int32 =
           {data: int32 []; dims: int list; status: int;}
           with
              member x.get ii     = let nvalues = List.fold_left (fun prod n -> prod * n) 1 x.dims
                                    if (ii <= nvalues) then
                                        x.data.[ii-1]  //based array similar to Fortra, Matlab
                                    else
                                        failwith "nc_data_int32: Bad dimension with get"
             
              member x.getm kk ll =  let mm = List.nth x.dims 0
                                     let nn = List.nth x.dims 1
                                     if ((kk <= mm) && (ll <= nn)) then
                                        x.data.[kk + (ll-1)*mm - 1]
                                     else
                                        failwith "nc_data_int32: Bad dimension with getm"
                                       
              member x.array2     =  if ((List.length x.dims) = 2) then
                                        reshape2 x.data (List.nth x.dims 0) (List.nth x.dims 1)
                                     else
                                        failwith "nc_data_int32: Impossible to reshape in Array2 type"
           end
    ======================================================

    This declaration of type nc_data_int32 is «pure functionnal code» or not?

  •  09-07-2008, 13:43 6953 in reply to 6951

    Re: type with Array, Array2 and member: pure functional code or not?

    Under certain assumptions, this may be part of a pure functional program. However by itself (e.g. as a library) it is not, because you have no guarantee that the input array stored in the "data" field is not being mutated by other code. In your actual code you probrably do have this guarantee. 

    One way to think about purity is in terms of "effects", e.g.

    • reading from a mutable memory location
    • writing to a mutable memory location
    • writing to a file
    • reading from a file
    • reading the system time
    • generating a random number
    • failing to terminate

    and so forth. If your program has no effects, then it is fair to call it pure. You can imagine annotating each function with the effects it causes, relative to the file handles and mutable objects it has access to.(Note,  the Haskell IO monad provides one way to do this at a coarse granularity).

    The use of effects doesn't automatically mean a program is impure. For example, if you can safely assume that no other code mutates the particular array stored in the "data" field then the above program is pure, despite the fact that it has "read" effects. Likewise, if you made a private copy of the array in your data structure then this particualr type is pure. The key thing is that the use of effects can't be effectively observed (forgive the pun)

    [ Why do I say "effectively observed"? This is because there are ways of observing effects that we ignore in practice, e.g. counting CPU cycles, or clock time, or CPU temperature, or looking under-specified values such as object hash codes. ]

    Thanks

    Don

     

  •  09-07-2008, 16:04 6959 in reply to 6953

    Re: type with Array, Array2 and member: pure functional code or not?

    Thanks - I am happy with your answer 


        My objective is read data from a file. Normally data in file never change.
        And array stored in «data» is never affected.
        I use «data» only for calculatation: sum, mean, rmse etc.

        Also, I think my code is pure.

     

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