如何在 Haskell 中编写 Deque 数据类型
如何在 Haskell 中编写双端队列(“deque”)。数据结构应该有函数emptyDeque、front、back、removeFront、removeBack、addFront、addBack和isEmpty,然后在->;之间显示双端队列。和<-。
这是相同的,但对于队列:
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))
`
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))
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用列表来实现双端队列效率不是很高,但它可以工作。一些注释
除了类型错误之外,您似乎正在以其他语言的风格编写函数应用程序
在 Haskell 中,括号只是为了分组。编写该行的更 Haskell 方式可能是
或者甚至
另一个注意事项:在 Haskell 中向列表前面添加内容是非常典型的
,但是向列表后面添加内容很难看。
您已经有了一个相当好的开始,但是您应该尝试熟悉 Haskell 独特的范例和风格。 学习 Haskell 的前几章在这方面做得很好。
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
In Haskell, the parens are simply for grouping. The more Haskelly way to write that line would be
or even
Another note: adding something to the front of the list is very typical in Haskell
Adding to the back of a list is ugly, though.
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.
实际上这里是我的 Final Deque 实现,效果很好
Actually here my Final Deque implementation which works fine