Skip to content

Instantly share code, notes, and snippets.

@Idorobots
Created April 28, 2014 23:51
Show Gist options
  • Save Idorobots/11387381 to your computer and use it in GitHub Desktop.
Save Idorobots/11387381 to your computer and use it in GitHub Desktop.
Half-assed ID3 attempt.
fun name (n, v) = n;
fun vals (n, v) = v;
fun get([], a) = 0
| get((b, v) :: tail, a) = if a = b then v else get(tail, a);
fun ig(a, S, C) =
(* TODO :( *)
C(S, name a);
fun pickBest(A, S, C) =
foldl(fn ((n1, ig1), (n2, ig2)) => if ig1 >= ig2
then (n1, ig1)
else (n2, ig2))
(hd A, 0)
(map (fn a => (a, ig(a, S, C)))
A);
fun filterS([], _, _) = []
| filterS(s :: S, v, a) =
if get(s a) = v
then s :: filterS(S, v, a)
else filterS(S, v, a);
fun removeA([], _) = []
| removeA((b, v) :: A, a) =
if b = a
then removeA(A, a)
else (b, v) :: removeA(A, a);
datatype Tree = Leaf of int | Branch of string * (int * Tree) list;
fun id3(S, C, []) = Leaf (C(S, "all"))
| id3(S, C, A) = let val (a, _) = pickBest (A, S, C)
in Branch (name a,
map (fn v =>
(v, id3(filterS(S, v, name a), C, removeA(A, name a))))
(vals a))
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment