[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Andrew Conway <arc@l...> |
| Subject: | Re: Looking for an elegant coding idiom. |
>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.