6

I'm implementing Ukkonen's algorithm, which requires that all leaves of a tree contain a reference to the same integer, and I'm doing it in Haskell to learn more about the language. However, I'm having a hard time writing out a data type that does this.

-- Node has children, indexes of info on the edge
-- to it, and an optional suffix link.

-- Leaf has a beginning index of the info, but the
-- end index is always an incrementing variable index.
data STree = Node [STree] (Int, Int) (Maybe STree)
           | Leaf (Int, ??? )

How can I put the reference in the Leaf type declaration?

dfeuer
  • 48,079
  • 5
  • 63
  • 167
Craig
  • 255
  • 1
  • 6
  • See [`Data.IORef`](http://hackage.haskell.org/package/base-4.6.0.1/docs/Data-IORef.html) – luqui Oct 14 '13 at 22:28
  • Thanks! I was looking at ST ref and couldn't figure out what to do with the s in the type – Craig Oct 14 '13 at 22:38
  • 1
    The `s` remains abstract, you just ignore it and it's consumed by `runST`. The reason it exists is to prevent you from viewing ST-internal computation outside of `runST`. – J. Abrahamson Oct 14 '13 at 22:43
  • I'm sorry, I don't see what to put in the data declaration with STRef. Leaf (Int, STRef ... Int) would be convenient if I could do it. – Craig Oct 14 '13 at 22:54
  • 2
    @user2876621, It would be `data STree s = Node [STree s] (Int, Int) (Maybe (STree s)) | Leaf (Int, STRef s Int)`. This is the more "moral" way to do algorithms with mutable references, but if you are getting lost, there is no harm in using `IORef` when you are beginning. – luqui Oct 14 '13 at 22:56
  • @luqui, That makes a lot of sense! I'll be able to make more progress with this now – Craig Oct 14 '13 at 23:01
  • It might be worth checking out whether instead of an actual reference, this could also be done with multiple "copies" of a lazily computed value, perhaps a list of ints. (Probably not in this case, but such techniques can sometimes work in Haskell to implement stuff you'd think _must_ require explicit mutation, efficiently in a purely functional fashion.) – leftaroundabout Dec 20 '14 at 18:47
  • 1
    @leftaroundabout, http://www.zbh.uni-hamburg.de/pubs/pdf/GieKur1995a.pdf seems to look into purely functional (and other) suffix tree algorithms fairly deeply. They don't seem to think it possible to achieve linear time construction, but apparently their (asymptotically inefficient) algorithm achieves good practical performance due to locality. https://hackage.haskell.org/package/suffixtree-0.2.2.1/docs/Data-SuffixTree.html seems to be based on that. – dfeuer Dec 27 '14 at 19:46
  • @leftaroundabout, I see now that those are already mentioned here: http://stackoverflow.com/questions/11495875/how-to-find-all-substrings-of-a-string-with-start-and-end-indices/11499486#11499486 – dfeuer Dec 27 '14 at 19:56

0 Answers0