how to write a Deque data type in Haskell
how to write in Haskell a double-ended queue ("deque"). The data structure should have functions emptyDeque, front, back, removeFront, removeBack, addFront, addBack and isEmpty, and then display the double-ended queue between -> and <-.
this is the same but for a Queue:
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
I came up with is itt correct?
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))
`
Using lists to implement a Deque is not very efficient, but it can work. A few notes
Type errors aside, you seem to be writing function application in the style of other languages
front(reverse(a))
In Haskell, the parens are simply for grouping. The more Haskelly way to write that line would be
front (reverse a)
or even
front $ reverse a
Another note: adding something to the front of the list is very typical in Haskell
[x] ++ xs -- The weird way
x : xs -- The canonical way
Adding to the back of a list is ugly, though.
xs ++ [x] -- No better way for normal lists. This is inefficient
You're off to a fairly good start, but you should try to familiarize yourself with Haskell's unique paradigm and style. The first few chapters of Learn You a Haskell do a good job of this.
Actually here my Final Deque implementation which works fine
module Deques (Deque, emptyDeque, front, back, removeFront, removeBack, addFront, addBack, isEmpty) where
newtype Deque a = Deque [a]
backwards (Deque []) = Deque []
backwards (Deque a) = Deque( reverse a )
emptyDeque = Deque []
front (Deque (x:xs)) = x
front (Deque []) = error("No front of empty Deque")
back (Deque a) = front( backwards (Deque 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) = backwards( removeFront( backwards (Deque a) ))
removeBack (Deque []) = error("Nothing on Deque to remove")
isEmpty (Deque []) = True
isEmpty (Deque (x:xs)) = False
showElems [] = ""
showElems (x:xs) = " " ++ (Prelude.show x) ++ (showElems xs)
showDeque (Deque a) = "<" ++ (showElems a) ++ " >"
instance (Show a) => Show (Deque a) where show = showDeque
精彩评论