From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA06094; Wed, 4 Dec 2002 19:27:10 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id TAA05981 for ; Wed, 4 Dec 2002 19:27:10 +0100 (MET) Received: from swan.mail.pas.earthlink.net (swan.mail.pas.earthlink.net [207.217.120.123]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id gB4IR9X09334 for ; Wed, 4 Dec 2002 19:27:09 +0100 (MET) Received: from user-0cev2t6.cable.mindspring.com ([24.239.139.166] helo=minsky-primus.homeip.net) by swan.mail.pas.earthlink.net with smtp (Exim 3.33 #1) id 18JeEj-0006g6-00 for caml-list@inria.fr; Wed, 04 Dec 2002 10:27:05 -0800 Received: from 141.155.88.179 (SquirrelMail authenticated user yminsky) by minsky-primus.homeip.net with HTTP; Wed, 4 Dec 2002 13:27:05 -0500 (EST) Message-ID: <32290.141.155.88.179.1039026425.squirrel@minsky-primus.homeip.net> Date: Wed, 4 Dec 2002 13:27:05 -0500 (EST) Subject: [Caml-list] python-style array access From: "Yaron M. Minsky" To: "Caml List " X-Priority: 3 Importance: Normal X-Mailer: SquirrelMail (version 1.2.8) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk One of the things I miss from Python is the small syntactical touches that simplify common operations. one of my favorites is the way python does array indexing. You can use negative numbers to count from the end of the array, and you can easily take subranges of arrays or strings. It's actually quite simple to get the same functionality in ocaml, and here's some simple code for doing it. It adds a bit of overhead (an extra function call), but where that small overhead isn't an issue, I think this is a nice solution. If you include the following code in the beginning of your module, you'll be able to do the following kind of tricks: # "foobar".[-1];; - : char = 'r' # "foobar".[-2];; - : char = 'a' # String.rsub "foobar" 0 (-1);; - : string = "fooba" # String.rsub "foobar" 1 0;; - : string = "oobar" # String.rsub "foobar" 1 3;; - : string = "oo" # String.rsub "foobar" 1 (-1);; - : string = "ooba" The same kind of things can be done with arrays. rsub stans for "range sub", by the way. Note that a 0 in the second position of rsub means the very last as opposed to the very first index position. ------ Snip Here ------- open StdLabels open MoreLabels module Array = struct include Array let normalize ar i = if i < 0 then length ar + i else i let get ar i = get ar (normalize ar i) let rsub ar start stop = let stop = if stop = 0 then length ar else stop in let pos = normalize ar start in let len = (normalize ar stop) - pos in sub ar ~pos ~len end module String = struct include String let normalize str i = if i < 0 then length str + i else i let get str i = get str (normalize str i) let rsub str start stop = let stop = if stop = 0 then length str else stop in let pos = normalize str start in let len = (normalize str stop) - pos in sub str ~pos ~len end -- |--------/ Yaron M. Minsky \--------| |--------\ http://www.cs.cornell.edu/home/yminsky/ /--------| Open PGP --- KeyID B1FFD916 (new key as of Dec 4th) Fingerprint: 5BF6 83E1 0CE3 1043 95D8 F8D5 9F12 B3A9 B1FF D916 ------------------- 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