I am looking for a functional data structure which supports the following tasks:
- < Li> add o (1)
- Repetition in order, hey (n)
A normal functional linked list only supports O (en) attachments, while I can use normal LL and then reverse it, the reverse operation will be O (N) which (partially) o (1) rejects the opposition action.
You can use John Hughes's continuous-built append lists, which are now available in
DList is called. There is a function for lists from the representation lists: the empty list is the identity function; The attachment is the structure, and the singletons are opposed (partially applicable). In this calculation you will be charged the allocation of
n in each calculation so that it is not good.
Alternatively, make the same algebra as a data structure:
type 'a seq = empty | Single 'A' of a 'seq *' The insertion of a sek is a walking distance of a tree, which will either spend some stack space or require zipper representation of some kind. Here is a walking distance of a tree that converts to the list, but uses stack space:
let's list the t = let's go with t xs = match t. Empty - & gt; XS | Single X - & gt; X :: xs | Appendix T1 (T2) - & gt; Walking T1 (Walking T2X) Walking T []
Here's the same, but using continuous stack space:
Let's do For the list 't = let's go with the t xs = match. Empty - & gt; Finishes lefts xs | Single X - & gt; Finnish Leaves (X :: XS) | Appendix T1 (T2) - & gt; Walking (T1 :: Lifts) with T2X and Finish Lifts Xs = Match Lifts. [] - & gt; XS | T :: ts - & gt; Walking TS T XS in Walking [] T []
You can write that fold function which visits the same element but does not actually correct the list; Just replace the opposition and zero with just a few more common:
Well fold: ('a *' b -> b) - & gt; 'B - & gt; 'A seq - & gt; 'B' Chalo Ch t t = REC running top t xs = match t Empty - & gt; Finishes lefts xs | Single X - & gt; Finnish Leaves (F (x, xs)). Appendix T1 (T2) - & gt; Walking (T1 :: Lifts) with T2X and Finish Lifts Xs = Match Lifts. [] - & gt; XS | T :: ts - & gt; Walking TS Tx Walking [] TZ
Have fun with your linear-time, constant-stack count!
Comments
Post a Comment