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 LAA32741; Sat, 28 Sep 2002 11:02:22 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id LAA00075 for caml-list@pauillac.inria.fr; Sat, 28 Sep 2002 11:02:21 +0200 (MET DST) 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 SAA16424 for ; Fri, 27 Sep 2002 18:47:42 +0200 (MET DST) Received: from thecia.net (opcenter.thecia.net [208.218.131.20]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g8RGlf529959 for ; Fri, 27 Sep 2002 18:47:41 +0200 (MET DST) Received: from dfan.thecia.net.thecia.net ([63.214.116.93]) by smtp.thecia.net; Fri, 27 Sep 2002 12:47:38 -0400 To: caml-list@inria.fr Subject: [Caml-list] Design advice From: Dan Schmidt Date: 27 Sep 2002 12:47:35 -0400 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk I've been using ocaml for only a couple of months now, and it's not yet clear to me how to do some things most idiomatically in it. One has to do with making the equivalent of an enum in C. Say I'm implementing a deck of cards for a card game. There are four suits, and none of them have any special properties (this isn't a game like bridge where different suits are treated differently). I could do type suit = Spades | Hearts | Diamonds | Clubs but this seems a little heavyweight; it seems weird to perform pattern matching on values that really have no semantic importance other than the fact that they are different from each other. It's not like I'm going to have any code that does one thing when given the 3 of Spades and another thing when given the 3 of Hearts. The other issue is that it is mildly annoying to, for example, write a compare function to be used inside a struct that implements the OrderedType signature (e.g., if I want to have a Set of cards). The alternative is to just use an int, but then it is theoretically possible that I could pass in ints outside the valid range and get run-time errors. The idea of guaranteeing that a suit is never invalid appeals to me. The same sort of issue comes up with representing the two players of the game. I could use an int and just raise exceptions everywhere if it turns out to be not 0 or 1. Or I could make a type player = Player_one | Player_two type, but now I have to convert this to an integer whenever I want to use the player number as an index. Which sort of approach usually ends up being less of a hassle in the end? Or does it depend on the particular application? Or is there yet a third solution which is better than either of these? I've been vacillating, and right now I am inclined to go with the variants at the expense of having to indirect every once in a while. Finally, is there any type in the library that functions like an immutable array? I would like to have an indexable bunch of values but use it in a purely functional way. I could always just use Arrays and copy them before updating, but if there's already an idiomatic type to use I'd prefer to use that. Thanks, Dan -- http://www.dfan.org ------------------- 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