You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

87 lines
1.7 KiB

module UnbalancedSet exposing (..)
import Html exposing (Html)
type alias Ordered t =
{ eq : t -> t -> Bool
, lt : t -> t -> Bool
, leq : t -> t -> Bool
}
type Tree t
= E
| T (Tree t) t (Tree t)
type alias Set =
Tree
empty : Tree x
empty =
E
member : Ordered x -> x -> Tree x -> Bool
member compare x tree =
case tree of
E ->
False
T a y b ->
if
if compare.lt x y then
member compare x a
else
member compare x b
then
True
else
compare.eq x y
insert : Ordered x -> x -> Tree x -> Tree x
insert compare x tree =
case tree of
E ->
T E x E
T a y b ->
if compare.lt x y then
T (insert compare x a) y b
else if compare.lt y x then
T a y (insert compare x b)
else
tree
main : Html msg
main =
let
normal =
{ eq = \a b -> a == b
, lt = \a b -> a < b
, leq = \a b -> a <= b
}
debug =
{ eq = \a b -> Debug.log "eq" <| a == b
, lt = \a b -> Debug.log "lt" <| a < b
, leq = \a b -> Debug.log "leq" <| a <= b
}
in
empty
|> insert normal 4
|> insert normal 2
|> insert normal 1
|> insert normal 3
|> insert normal 7
|> insert normal 6
|> insert normal 8
|> insert normal 5
|> Debug.log "tree"
|> member debug 3
|> toString
|> Html.text