Verified Commit d0547244 authored by Camil Staps's avatar Camil Staps 🚀

Make Data.Queue tail-strict

parent 2ab3f602
Pipeline #43813 passed with stage
in 2 minutes and 18 seconds
...@@ -6,10 +6,10 @@ definition module Data.Queue ...@@ -6,10 +6,10 @@ definition module Data.Queue
* Functional Data Structures. * Functional Data Structures.
*/ */
from Data.Maybe import :: Maybe from StdMaybe import :: Maybe
from StdOverloaded import class length from StdOverloaded import class length
:: Queue a = Queue ![a] ![a] :: Queue a = Queue ![a!] ![a!]
/** /**
* Create an empty queue * Create an empty queue
...@@ -21,8 +21,8 @@ newQueue :: Queue a ...@@ -21,8 +21,8 @@ newQueue :: Queue a
* @type (Queue a) -> Bool * @type (Queue a) -> Bool
*/ */
empty q :== case q of empty q :== case q of
Queue [] [] -> True Queue [ !] [ !] -> True
_ -> False _ -> False
instance length Queue instance length Queue
......
implementation module Data.Queue implementation module Data.Queue
import Data.Maybe
import StdList, StdOverloaded import StdEnv
import StdMaybe
import StdOverloadedList
/** /**
* This module provides a straightforward FIFO queue. * This module provides a straightforward FIFO queue.
* It is implemented using two list based on Chris Okasaki's example in Purely Functional Data Structures. * It is implemented using two list based on Chris Okasaki's example in Purely Functional Data Structures.
*/ */
newQueue :: Queue a newQueue :: Queue a
newQueue = Queue [] [] newQueue = Queue [|] [|]
instance length Queue where length (Queue front rear) = length front + length rear instance length Queue where length (Queue front rear) = Length front + Length rear
/** /**
* Add an element to the queue * Add an element to the queue
*/ */
enqueue :: a !(Queue a) -> Queue a enqueue :: a !(Queue a) -> Queue a
enqueue x (Queue front rear) = Queue front [x:rear] enqueue x (Queue front rear) = Queue front [|x:rear]
/** /**
* Take an element from the queue (if the queue is not empty) * Take an element from the queue (if the queue is not empty)
*/ */
dequeue :: !(Queue a) -> (!Maybe a, !Queue a) dequeue :: !(Queue a) -> (!Maybe a, !Queue a)
dequeue (Queue [] [] ) = (Nothing, Queue [] []) dequeue (Queue [|x:xs] rear) = (Just x, Queue xs rear)
dequeue (Queue [x:xs] rear) = (Just x, Queue xs rear) dequeue q=:(Queue [|] [|]) = (Nothing, q)
dequeue (Queue [] rear) = let [x:xs] = reverse rear in (Just x, Queue xs []) dequeue (Queue [|] rear) = let [|x:xs] = Reverse rear in (Just x, Queue xs [|])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment