如何在 Haskell 中编写 Deque 数据类型

发布于 2024-11-03 01:32:24 字数 1453 浏览 6 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把回忆走一遍 2024-11-10 01:32:24

使用列表来实现双端队列效率不是很高,但它可以工作。一些注释

除了类型错误之外,您似乎正在以其他语言的风格编写函数应用程序

front(reverse(a))

在 Haskell 中,括号只是为了分组。编写该行的更 Haskell 方式可能是

front (reverse a)

或者甚至

front $ reverse a

另一个注意事项:在 Haskell 中向列表前面添加内容是非常典型的

[x] ++ xs -- The weird way
x : xs -- The canonical way

,但是向列表后面添加内容很难看。

xs ++ [x] -- No better way for normal lists. This is inefficient

您已经有了一个相当好的开始,但是您应该尝试熟悉 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

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.

|煩躁 2024-11-10 01:32:24

实际上这里是我的 Final Deque 实现,效果很好

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文