Hello!

Not a stupid question at all. There are several ways to approach what you are asking. One way is to represent the Rectangle type as a record containing two fields, max and min, each as a float array.

1
2
type Rectangle = { min : float array; max : float array }

Next, we can create your function:

1
2
3
4
5
6
let edge_overlaps r1 r2 = 

    Array.exists2 (=) r1.min r2.min ||

    Array.exists2 (=) r1.max r2.max

Instead of looping through the array, we use the function "exists2". This takes two array, and returns true or false, depending on if any of the items meet the criteria. Our critera is simple, equals. (Passing = inside parens lets you use it as a normal function.)

If you want to make it an OO-like member, you can use this syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
type Rectangle = { min : float array; max : float array }

with

    member this.edgeOverlaps that =

        Array.exists2 (=) this.min that.min ||

        Array.exists2 (=) this.max that.max

// FSI will show the ful type:

type Rectangle =

  {min: float array;

   max: float array;}

  with

    member edgeOverlaps : that:Rectangle -> bool

  end

You can then use it like this:

1
2
3
4
5
6
let rec1 = { min = [|1.0; 2.0|]; max = [|3.0; 4.0|] }

let rec2 = { min = [|1.1; 2.0|]; max = [|3.1; 4.0|] }

printfn "%b" (rec1.edgeOverlaps rec2)

Note that rectangle is still a record type, not a class type. If you really want Rectangle as a class (a "constructed type") with private fields and properties and so on, do it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
type OORectangle(min : float array, max : float array) =

    member x.Min = min

    member x.Max = max

    member x.Overlaps (y:OORectangle) =

        Array.exists2 (=) x.Min y.Min ||

        Array.exists2 (=) x.Max y.Max

let oorec1 = OORectangle([|1.0; 2.0|], [|3.0; 4.0|])

let oorec2 = OORectangle([|1.0; 2.1|], [|3.0; 4.1|])

printfn "%b" (oorec1.Overlaps oorec2)

FSI will print:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
type OORectangle =

  class

    new : min:float array * max:float array -> OORectangle

    member Overlaps : y:OORectangle -> bool

    member Max : float array

    member Min : float array

  end

val oorec1 : OORectangle

val oorec2 : OORectangle

P.S. To see how to do code snippets, hit quote on my post. (Although, I haven't figured out indentation yet...)

By on 3/8/2009 1:20 PM ()

Hi!
Wow thanks, that was quite usefull, now i can play a little bit with this.. really really big thanks!
Just for completeness
- if i have to use for-loops, is it possible to break loops in order to return false/true?
Greetings

edit: ok one question was stupid -.-

By on 3/8/2009 1:54 PM ()

For loops: In general, such loops are not recommended since they are error prone. There is no early break out, so if you had to, you might resort to a while loop with a mutable variable.

As to exists2, it's signature is:

> Array.exists2;;
val it : (('a -> 'b -> bool) -> 'a [] -> 'b [] -> bool) = <fun:clo@0_1>

So the first argument needs to be a function to do the predicate for exists2. But I think you figured that out :).

By on 3/8/2009 3:01 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper