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.
beginner haskell queue
New contributor
add a comment |
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.
beginner haskell queue
New contributor
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
add a comment |
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.
beginner haskell queue
New contributor
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
beginner haskell queue
New contributor
New contributor
edited yesterday
200_success
127k15148411
127k15148411
New contributor
asked Nov 18 at 13:54
Michel Vorwieger
212
212
New contributor
New contributor
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
add a comment |
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
add a comment |
active
oldest
votes
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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