Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary search tree cannot be a monoid #59

Closed
sjakobi opened this issue Jan 5, 2015 · 1 comment
Closed

Binary search tree cannot be a monoid #59

sjakobi opened this issue Jan 5, 2015 · 1 comment

Comments

@sjakobi
Copy link
Contributor

sjakobi commented Jan 5, 2015

I was surprised that the binary-search-tree exercise doesn't allow solutions where the BST can be empty.

The problem is that the bstValue function is of type BST a -> a instead of of BST a -> Maybe a.
This also means that the fromList function must have a special case where it returns an error for the empty list.

In the example solution the BST is defined as:

data BST a = Node { bstValue :: a
                  , bstLeft :: Maybe (BST a)
                  , bstRight :: Maybe (BST a) }

Wouldn't a definition like the following be nicer?

data BST a = Tip
           | Node (BST a) a (BST a)
@etrepum
Copy link
Contributor

etrepum commented Jan 6, 2015

That would be reasonable if bstValue was BST a -> Maybe a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants