Browse thread
practical functional programming
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2000-11-09 (18:06) |
From: | Stephan Houben <stephan@p...> |
Subject: | Re: practical functional programming |
On Tue, 07 Nov 2000, Chris Hecker wrote: > How does the compiler "know" the datastructure is purely functional? It doesn't. Why would it have to? > In other words, is there any way for me to get a type into a tree that has a > mutable member (perhaps through an abstract type), Yes there is. You can make a map that contains, for example, values of type `int ref' or `int array'. > and if so, what happens in > that case? Well, as you already might know, O'Caml *never* copies anything unless you explicitely tell it to. For example, the following session: # let ar1 = [|1|];; val ar1 : int arr# ar1.(0) <- # ar2;; - : int array = [|2|]2;; - : unit = () ay = [|1|] # let ar2 = ar1;; val ar2 : int array = [|1|] # ar2;; - : int array = [|1|] # ar1.(0) <- 2;; - : unit = () # ar2;; - : int array = [|2|] So I mutated ar1, but the update is reflected in ar2. In C++ speech, all variables are implicitely "references" to the underlying object. ar1 and ar2 refer to the same object. If you want ar2 to be a new array, then you have to use Array.copy to explicitely tell O'Caml that you want a new array. So yes, if I have a map map1, and I get an "updated" map2 from that, then mutations to the objects held in map1 will be reflected in map2. Stephan -- ir. Stephan H.M.J. Houben tel. +31-40-2474358 / +31-40-2743497 e-mail: stephanh@win.tue.nl