Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] static variables in a function
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Stefano Zacchiroli <zack@c...>
Subject: Re: [Caml-list] static variables in a function
On Fri, Jun 14, 2002 at 10:08:30AM -0700, Shannon --jj Behrens wrote:
> Hi,
> 
> What is the correct OCAML idiom for achieving the
> following psuedocode?
> 
> let get_chunk () =
>   static chunks_list;
> 
>   if List.is_empty chunks_list
>   then chunks_list = get_more_chunks ();
>   shift chunks_list
> ;;

an approximation (not tested) may be:

   let chunks_list = ref [] in
   let get_chunk () =
     if List.is_empty !chunks_list then
        chunks_list := get_more_chunks ();
     shift !chunks_list
   in
   (* code where you use get_chunk *)

Cheers.

-- 
Stefano Zacchiroli - undergraduate student of CS @ Univ. Bologna, Italy
zack@cs.unibo.it | ICQ# 33538863 | http://www.cs.unibo.it/~zacchiro
"I know you believe you understood what you think I said, but I am not
sure you realize that what you heard is not what I meant!" -- G.Romney
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners