Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Andrew Conway <arc@labri.u-bordeaux.fr>
To: caml-list@margaux.inria.fr
Subject: Re: Looking for an elegant coding idiom.
Date: Mon, 28 Aug 1995 14:57:11 +0200	[thread overview]
Message-ID: <9508281257.AA25962@arazek> (raw)
In-Reply-To: Your message of "Wed, 02 Aug 1995 16:37:00 PDT." <m0sdnMC-00026dC@owl.uucp>


>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.





      reply	other threads:[~1995-08-28 14:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-08-02 23:37 John Gerard Malecki
1995-08-28 12:57 ` Andrew Conway [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9508281257.AA25962@arazek \
    --to=arc@labri.u-bordeaux.fr \
    --cc=caml-list@margaux.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox