Skip to content

Instantly share code, notes, and snippets.

@daniel-trinh
Created April 29, 2014 16:27
Show Gist options
  • Save daniel-trinh/11405333 to your computer and use it in GitHub Desktop.
Save daniel-trinh/11405333 to your computer and use it in GitHub Desktop.
defmodule BST do
defstruct data: nil, left: nil, right: nil
@spec insert(BST.t, any) :: BST.t
def insert(nil, value) do
%BST{data: value}
end
def insert(%BST{data: d, left: l, right: _r} = bst, value) when value > d do
%BST{data: d, left: l, right: insert(bst.right, value)}
end
def insert(%BST{data: d, left: _l, right: r} = bst, value) when value <= d do
%BST{data: d, left: insert(bst.left, value), right: r}
end
end
# Dialyzer output:
# BST.ex:9: The call _@1:'right'() requires that _@1 is of type atom() | tuple() not #{}
# BST.ex:12: The call _@1:'left'() requires that _@1 is of type atom() | tuple() not #{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment