как написать в Haskell двустороннюю очередь («deque»).Структура данных должна иметь функции emptyDeque, front, back, removeFront, removeBack, addFront, addBack и isEmpty, а затем отображать двустороннюю очередь между -> и <-. </p>
это то же самое, но дляОчередь:
module Queues (Queue, emptyQueue, front, remove, add, isEmpty)
newtype Queue a = Queue [a]
emptyQueue = Queue []
front (Queue (x:xs)) = x
front (Queue []) = error("No front of empty queue")
add (Queue xs) x = Queue (xs ++ [x])
remove (Queue (x:xs)) = Queue xs
remove (Queue []) = error("Nothing on queue to remove")
isEmpty (Queue []) = True
isEmpty (Queue (x:xs)) = False
showElems [] = ""
showElems (x:xs) = " " ++ (Prelude.show x) ++ (showElems xs)
showQueue (Queue a) = "<" ++ (showElems a) ++ " >"
instance (Show a) => Show (Queue a) where show = showQueue
Я придумал, это правильно?
module Deques (Deque, emptyDeque, front, back, removeFront, removeBack , addFront, addBack, isEmpty)
newtype Deque a = Deque [a]
emptyQueue = Queue []
reverses (x:xs) = (reverses xs) ++ [x]
front (Deque (x:xs)) = x
front (Deque []) = error("No front of empty Deque")
back (Deque a) = front(reverse(a))
back (Deque []) = error("No back of empty Deque")
addBack (Deque xs) x = Deque (xs ++ [x])
addFront (Deque xs) x = Deque ([x] ++ xs)
removeFront (Deque (x:xs)) = Deque xs
removeFront (Deque []) = error("Nothing on Deque to remove")
removeBack (Deque a) = reverses(removeFront(reverses(a))
`