typeclass - Haskell type class for Queue -


Is any Haskell type class (or a combination of type classes) written, which describes the FIFO queue.

looks very strong, but on the other hand looks very weak (as the order is not defined).

I just did not want to do any other work again.

Really very difficult (and an interesting practice) to roll your FIFA line in Haskell I can appreciate it for the desire to use a standard type class, and this is almost certainly what you should do, but I have just learned about the last week, and I have written a lot about it I'm excited.

Here is a simple queue class, which allows you to check that the queue is empty, from the top of the queue (and the rest of the queue back) to get the first element and a new element in the queue Enter

  square q q where empty: qa - & gt; Get Bull :: QA - & gt; (A, QA) In :: A -> QA - & gt; QA   

The easiest way to create a FIFO queue is using a list:

  line [] where empty = get tap [] = "Error" Do not retrieve the elements from the empty list "(x: xs) = (x, xs) ins xxs = xs ++ [x]   

However, this is very Is only disabled if there are currently n elements in the queue, then taking a new element ( n ) takes time if you m element Want to insert in the empty row, OO ( m 2 ) takes time, can we make a queue which is the (1) time (or at least, o (1) elements in quantized time Inserts and retrieves?)

To store front and back of the queue in the move, separate lists with the back of the queue are being stored in reverse: <

The queue is empty if both are front and back are empty:

  

Code> down (f xs Ys) = null xs & null ys

To insert a new element in the list, we put it only in the previous row, which takes time O (1).

  ins y (f xs ys) = f xs (y: ys)   

Getting an element from the front of the row is easy, when the elements Are waiting (and we put an error queue is empty)

  get (F [] []) = error "Can not retrieve the element from empty queue" (F (X: xs) ys = = (x, f) Finally, if no element is waiting in front of the queue, then we reverse the back of the queue and put it in front. Although it is o ( n ) take time We have to do only once for each element, so for the average O (1) time of our operation received:  
  get (F [] ys) = mill ( F (reverse YS) []) There you have - O (1) refine the fifo queues in a functional language.  

Edit: The F. asked about the performance of amortized O (1) in comments. The logic for Amortized continuous time is very simple.

n Blank blanket Consider the sequence of r summation, then n retrieval can take time in the inclusions n on the first recovery, is empty in front of the queue, so behind the queue In the end, n takes time to retrieve the element, at the end n - 1 retrieval time takes 1 each, then the total time is

n + n + 1 + n - 1 = 3 n < P> We made 2 n calls in total Education is, therefore refine times 3 is n / 2 n = 3/2, which is O (1). The same logic does not matter that there is nothing to call ins and meet - in two calls, each element is cons'ed once, one Bar took and de-cons'ed once.

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -