A simple queue implementation in Haskell











up vote
4
down vote

favorite












I'm a member of a discord channel and sometimes people post simple coding exercises for fun. This time the exercise was:



Implement the following operations of a queue using stacks.




  • push(x) -- Push element x to the back of queue.

  • pop() -- Removes the element from in front of queue.

  • peek() -- Get the front element.

  • empty() -- Return whether the queue is empty


Example:



MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false


I didn't implement it using stacks because I don't know of any that are in the standard Haskell library but

I came up with this Solution:



data Queue a = Empty | Value a (Queue a) deriving (Show, Eq, Read)

push :: a -> Queue a -> Queue a
push x Empty = Value x Empty
push x (Value a pointer) = Value a (push x pointer)

pop :: Queue a -> Maybe (a, Queue a)
pop Empty = Nothing
pop (Value a pointer) = Just (a, pointer)

peek :: Queue a -> Maybe a
peek Empty = Nothing
peek (Value a _) = Just a

empty :: Queue a -> Bool
empty Empty = True
empty _ = False


I'm currently trying to understand Haskell better and was wondering if there was a more idiomatic Solution.










share|improve this question









New contributor




Michel Vorwieger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • This queue takes O(n) time to enqueue, unlike the common queue implementation which uses a doubly linked list. You can get an amortized O(1) time for enqueues if you implement it with two stacks (which is probably what you were getting at with the stack comment). A stack can be implemented using a singly linked list, which is what lists in haskell are. You can either make an abstract data type for a stack and use that in your queue ADT or just use lists.
    – cole
    yesterday















up vote
4
down vote

favorite












I'm a member of a discord channel and sometimes people post simple coding exercises for fun. This time the exercise was:



Implement the following operations of a queue using stacks.




  • push(x) -- Push element x to the back of queue.

  • pop() -- Removes the element from in front of queue.

  • peek() -- Get the front element.

  • empty() -- Return whether the queue is empty


Example:



MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false


I didn't implement it using stacks because I don't know of any that are in the standard Haskell library but

I came up with this Solution:



data Queue a = Empty | Value a (Queue a) deriving (Show, Eq, Read)

push :: a -> Queue a -> Queue a
push x Empty = Value x Empty
push x (Value a pointer) = Value a (push x pointer)

pop :: Queue a -> Maybe (a, Queue a)
pop Empty = Nothing
pop (Value a pointer) = Just (a, pointer)

peek :: Queue a -> Maybe a
peek Empty = Nothing
peek (Value a _) = Just a

empty :: Queue a -> Bool
empty Empty = True
empty _ = False


I'm currently trying to understand Haskell better and was wondering if there was a more idiomatic Solution.










share|improve this question









New contributor




Michel Vorwieger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • This queue takes O(n) time to enqueue, unlike the common queue implementation which uses a doubly linked list. You can get an amortized O(1) time for enqueues if you implement it with two stacks (which is probably what you were getting at with the stack comment). A stack can be implemented using a singly linked list, which is what lists in haskell are. You can either make an abstract data type for a stack and use that in your queue ADT or just use lists.
    – cole
    yesterday













up vote
4
down vote

favorite









up vote
4
down vote

favorite











I'm a member of a discord channel and sometimes people post simple coding exercises for fun. This time the exercise was:



Implement the following operations of a queue using stacks.




  • push(x) -- Push element x to the back of queue.

  • pop() -- Removes the element from in front of queue.

  • peek() -- Get the front element.

  • empty() -- Return whether the queue is empty


Example:



MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false


I didn't implement it using stacks because I don't know of any that are in the standard Haskell library but

I came up with this Solution:



data Queue a = Empty | Value a (Queue a) deriving (Show, Eq, Read)

push :: a -> Queue a -> Queue a
push x Empty = Value x Empty
push x (Value a pointer) = Value a (push x pointer)

pop :: Queue a -> Maybe (a, Queue a)
pop Empty = Nothing
pop (Value a pointer) = Just (a, pointer)

peek :: Queue a -> Maybe a
peek Empty = Nothing
peek (Value a _) = Just a

empty :: Queue a -> Bool
empty Empty = True
empty _ = False


I'm currently trying to understand Haskell better and was wondering if there was a more idiomatic Solution.










share|improve this question









New contributor




Michel Vorwieger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm a member of a discord channel and sometimes people post simple coding exercises for fun. This time the exercise was:



Implement the following operations of a queue using stacks.




  • push(x) -- Push element x to the back of queue.

  • pop() -- Removes the element from in front of queue.

  • peek() -- Get the front element.

  • empty() -- Return whether the queue is empty


Example:



MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false


I didn't implement it using stacks because I don't know of any that are in the standard Haskell library but

I came up with this Solution:



data Queue a = Empty | Value a (Queue a) deriving (Show, Eq, Read)

push :: a -> Queue a -> Queue a
push x Empty = Value x Empty
push x (Value a pointer) = Value a (push x pointer)

pop :: Queue a -> Maybe (a, Queue a)
pop Empty = Nothing
pop (Value a pointer) = Just (a, pointer)

peek :: Queue a -> Maybe a
peek Empty = Nothing
peek (Value a _) = Just a

empty :: Queue a -> Bool
empty Empty = True
empty _ = False


I'm currently trying to understand Haskell better and was wondering if there was a more idiomatic Solution.







beginner haskell queue






share|improve this question









New contributor




Michel Vorwieger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Michel Vorwieger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









200_success

127k15148411




127k15148411






New contributor




Michel Vorwieger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 18 at 13:54









Michel Vorwieger

212




212




New contributor




Michel Vorwieger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Michel Vorwieger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Michel Vorwieger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • This queue takes O(n) time to enqueue, unlike the common queue implementation which uses a doubly linked list. You can get an amortized O(1) time for enqueues if you implement it with two stacks (which is probably what you were getting at with the stack comment). A stack can be implemented using a singly linked list, which is what lists in haskell are. You can either make an abstract data type for a stack and use that in your queue ADT or just use lists.
    – cole
    yesterday


















  • This queue takes O(n) time to enqueue, unlike the common queue implementation which uses a doubly linked list. You can get an amortized O(1) time for enqueues if you implement it with two stacks (which is probably what you were getting at with the stack comment). A stack can be implemented using a singly linked list, which is what lists in haskell are. You can either make an abstract data type for a stack and use that in your queue ADT or just use lists.
    – cole
    yesterday
















This queue takes O(n) time to enqueue, unlike the common queue implementation which uses a doubly linked list. You can get an amortized O(1) time for enqueues if you implement it with two stacks (which is probably what you were getting at with the stack comment). A stack can be implemented using a singly linked list, which is what lists in haskell are. You can either make an abstract data type for a stack and use that in your queue ADT or just use lists.
– cole
yesterday




This queue takes O(n) time to enqueue, unlike the common queue implementation which uses a doubly linked list. You can get an amortized O(1) time for enqueues if you implement it with two stacks (which is probably what you were getting at with the stack comment). A stack can be implemented using a singly linked list, which is what lists in haskell are. You can either make an abstract data type for a stack and use that in your queue ADT or just use lists.
– cole
yesterday















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Michel Vorwieger is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207919%2fa-simple-queue-implementation-in-haskell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








Michel Vorwieger is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Michel Vorwieger is a new contributor. Be nice, and check out our Code of Conduct.













Michel Vorwieger is a new contributor. Be nice, and check out our Code of Conduct.












Michel Vorwieger is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207919%2fa-simple-queue-implementation-in-haskell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre