views:

113

answers:

2

For a description of the data structure see

An half-edge data structure involves cycles.

  • is it possible to implement it in a functional language like Haskell ?
  • are mutable references (STRef) to way to go ?

Thanks

+1  A: 

Obviously the problem is that a half-edge references the next and the opposite half-edge (the other references are no problem). You can "break the cycle" e.g. by referencing not directly to other half-edges, but to reference just an ID (e.g. simple Ints). In order to look up a half-edge by ID, you can store them in a Data.Map. Of course this approach requires some book-keeping in order to avoid a big hairy mess, but it is the easiest way I can think of.

Stupid me, I'm not thinking lazy enough. The solution above works for strict functional languages, but is unnecessary for Haskell.

Landei
This shouldn't be necessary, thanks to laziness. For instances, `let e = HalfEdge { ... partner = f ... } ; f = HalfEdge { ... partner = e ... } in ...` will work just fine.
Antal S-Z
A: 

If the task in question allows you to build the half-edge structure once and then query it many times, then lazy tying-the-know approach is the way to go, as was pointed out in the comments and the other answer.

However, if you want to update your structure, then purely-functional interface might prove cumbersome to work with. Also, you need to consider O(..) requirements for update functions. It might turn out that you need mutable internal representation (probably with pure API on top) after all.

ADEpt
Thanks for your answer. In the tie-the-knot approach, wouldn't the updates be done in constant time (provided, of course, the updates are 'local') ?
beb0s