Brian,<br><br>&gt; implementing the text buffer for an edit control<br><br>Just a few weeks ago I was thinking about a functional way to implement an edit control.&nbsp; I ended up with the code below.&nbsp; The function 'text' takes a stream of user input and returns the text to be displayed.
<br><br>Rather than using a mutable buffer, I chose to implement this using a stream of user input and two stacks where each stack contains the characters on opposite sides of the cursor.&nbsp; I'm certainly no expert, but I believe all operations are constant time, except that returning the final string at the end of the input stream is O(n).&nbsp; Of course, if you have a huge amount of text to display and need to display it after each key is pressed, then this is pretty worthless.
<br><br>
text s = mySort s [] []<br>

<br>

mySort&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; []&nbsp;&nbsp;&nbsp;&nbsp; stack&nbsp;&nbsp;&nbsp;&nbsp; sorted&nbsp; = (reverse sorted) ++ stack<br>

mySort ('R':xs)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; []&nbsp;&nbsp;&nbsp;&nbsp; sorted&nbsp; = mySort xs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; []&nbsp;&nbsp;&nbsp;&nbsp; sorted<br>

mySort ('R':xs) (s:stack)&nbsp;&nbsp;&nbsp; sorted&nbsp; = mySort xs&nbsp;&nbsp;&nbsp; stack&nbsp; (s:sorted)<br>

mySort ('L':xs)&nbsp;&nbsp;&nbsp; stack&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; []&nbsp; = mySort xs&nbsp;&nbsp;&nbsp; stack&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; []<br>

mySort ('L':xs)&nbsp;&nbsp;&nbsp; stack&nbsp; (s:sorted) = mySort xs (s:stack)&nbsp;&nbsp;&nbsp; sorted<br>

mySort&nbsp;&nbsp; (x:xs)&nbsp;&nbsp;&nbsp; stack&nbsp;&nbsp;&nbsp;&nbsp; sorted&nbsp; = mySort xs&nbsp;&nbsp;&nbsp; stack&nbsp; (x:sorted)<br><br>Here's how it works:<br>The string 's' in the 'text' function is a string of numbers (sorry, my app only needed to handle numbers) and
two special characters 'L' and 'R' which translate to
MoveCursorOnePositionRight and MoveCursorOnePositionLeft<br><br>In 'mySort', numeric characters in the input stream are pushed onto the 'sorted' stack.&nbsp;&nbsp;&nbsp; A 'Left' movement causes the head of the 'sorted' stack to be transferred to 'stack'.&nbsp; A 'Right' movement causes the head of the 'stack' to be transferred to 'sorted'.&nbsp; At the end of the input stream, the characters to the right of the cursor (the characters in 'stack') are appended to the characters to the left of the cursor (the reverse of 'sorted').
<br><br>I'm new to Haskell so maybe Ropes are better, but if the problem you need to solve is as simple as mine, it's hard to read research papers when you can get the job finished with 7 lines of Prelude code.<br><br>Thanks,
<br>Greg<br>