1

I am practicing purely functional style in C++. One big question is how can I construct tree data structure efficiently. Especially the back-edges. The connection to super-node. This can be thought as how to make a reference.

In C++, people simply use pointer (or smart pointer) for the back-edges, but AFAIK, purely functional (or referential transparency) means it's completely value based, so pointer or reference doesn't mean anything anymore.

I know this can be solved using huge vertex/edge table approach, but it seems to be very inefficient because it essentially needs lookup for every operation.

Any suggestions?


My question is not limited to C++. If you know another way to do this in another languages, please let me know.

eonil
  • 83,476
  • 81
  • 317
  • 516
  • Where'd you get the idea that pointers/references are off limits? Sure, purely functional languages do away with those concepts, but their "values" behave very much like reference types (consider recursive types), so in C++ where these details must be settled explicitly you have little choice but use pointers/references if you want similar power. Just don't use them for mutability (of course, same goes for value types). How would you write a cons list in C++ without pointers nor references? –  Jul 23 '13 at 11:19
  • @delnan It's just my own concern about referencing. To reference an object, the object must have an identity. Identity means something makes it different with others. But, to be purely functional, objects with same semantics must be evaluated equal. And this is making me suffer. If two values are differentiable ... well I think I need to study more about functional style. – eonil Jul 23 '13 at 13:26
  • c++ is imperative, so code like {A a = new (& b); B b = new (& a) } is impossible. So, purely functional style is impossible for bidirectional references in c++, sorry to disappoint you. – permeakra Jul 23 '13 at 16:14
  • @permeakra If I use proper functional language (e.g. Haskell), would I be able to do this? – eonil May 14 '16 at 06:51
  • @Eonil Definitely yes for Haskell (though it has its own can of worms) . Haskell supports unrestricted mutually recursive variable definitions in language. In computations... you may shoot yourself in a leg, but that's the cost. – permeakra May 14 '16 at 08:12

0 Answers0