From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.6.10/8.6.6) id QAA17083 for caml-redistribution; Mon, 28 Aug 1995 16:10:17 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.6.10/8.6.6) with ESMTP id PAA15918 for ; Mon, 28 Aug 1995 15:02:28 +0200 Received: from margaux.inria.fr (margaux.inria.fr [128.93.8.2]) by concorde.inria.fr (8.6.10/8.6.9) with ESMTP id PAA11530 for ; Mon, 28 Aug 1995 15:02:27 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by margaux.inria.fr (8.6.10/8.6.6) with ESMTP id PAA08818 for ; Mon, 28 Aug 1995 15:02:27 +0200 Received: from victor.labri.u-bordeaux.fr (root@victor.labri.u-bordeaux.fr [147.210.8.168]) by concorde.inria.fr (8.6.10/8.6.9) with SMTP id PAA11526 for ; Mon, 28 Aug 1995 15:02:26 +0200 Received: from arazek (arazek.labri.u-bordeaux.fr) by victor.labri.u-bordeaux.fr (4.1/SM-mailhost-BORDEAUX-1.0) id AA07671; Mon, 28 Aug 95 15:08:18 +0200 Received: from localhost by arazek (4.1/SM-BORDEAUX0.1) id AA25962; Mon, 28 Aug 95 14:57:13 +0200 Message-Id: <9508281257.AA25962@arazek> From: Andrew Conway Reply-To: arc@labri.u-bordeaux.fr To: caml-list@margaux.inria.fr Subject: Re: Looking for an elegant coding idiom. In-Reply-To: Your message of "Wed, 02 Aug 1995 16:37:00 PDT." Date: Mon, 28 Aug 1995 14:57:11 +0200 Sender: weis >CAML is a very elegant language and I am curious to hear of any >elegant coding idioms. I am especially curious to hear of any elegant >solutions to deal with some nuisance code of mine. I once solved this >problem in C. This may be holding me back from an elegant CAML >solution. It may be that there is no "best" style or solution but any >suggestions you have are appreciated. > >I am placing rectangular objects in the plane. There are 8 orthogonal >orientations and they are: > >>type orient = >> R0 >> | R90 >> | R180 >> | R270 >> | MY (* mirrored about the y axis *) >> | MX >> | MXR90 (* mirrored about the x axis and then rotated *) >> | MYR90 >> ;; >Given an orientation it is useful to be able to compose it with >another orientation. A transliteration from C looks something like > >> [ rotation -> ordinal -> look up table (messy) ] Since you have a group consisting of a rotation r, r^4=I, and a reflection, f^2=I satisfying rf = fr^{-1}, one could use the following type definition type orient = Unreflected of int (* The integer is a number 0-3 or rotations *) | Reflected of int (* The integer is a number 0-3 or rotations *) ;; let mod4 x = (x+4) mod 4;; (* works for -4<=x<= big number *) let composeorient = fun | (Unreflected n) (Unreflected m) -> Unreflected (mod4 (n+m)) | (Reflected n) (Unreflected m) -> Reflected (mod4 (n+m)) | (Unreflected n) (Reflected m) -> Reflected (mod4 (m-n)) | (Reflected n) (Reflected m) -> Unreflected (mod4 (m-n)) ;; It dies have the disadvantage of requiring more memory, but there are lots of other nice things that one can do with such a representation. Andrew.