English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2002-06-19 (04:39)
From: Shannon --jj Behrens <jjinux@y...>
Subject: Re: [Caml-list] static variables in a function
For any of you that are interested, here's the code
(based on a combination of your comments).  This code
has the following benefits:

1) The calling functions don't need to deal with
passing in a ref [].

2) Multiple "instances" of this function can be used

3) Only one match clause is needed, so there is no
repeated code (in my case, an exception will be thrown
for EOF).

 * Return a function that has the interface of a 
 * lexer and can act as a token buffer.
let get_token_buffer () = 
  let token_list = ref [] in
  let rec get_token token_list () =
    match !token_list with
      head :: tail -> token_list := tail;
    | [] -> token_list := [1; 2; 3]; (* replace me *)
      get_token token_list () in 
  get_token token_list;;

(* Try it out. *)
let token_buffer = get_token_buffer () in
while true do
  print_int (token_buffer ())

Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
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