Advertisement
Guest User

Untitled

a guest
Oct 9th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. datatype Tree = Branch of Tree * Tree | Node of string;
  2.  
  3. fun collect (nil : a' list list) : a' list = nil
  4.   | collect (x::xs) = x@collect(xs);
  5.  
  6. fun insertVariable variableToInsert : string ([] : string list) : string list list = [[variableToInsert]]
  7.   | insertVariable variableToInsert (x::xs) =
  8.       (variableToInsert::x::xs)::(map(fn (y) => x::y)(insertVariable(variableToInsert)(xs)));
  9.  
  10. fun getPermutations (nil : string list) : string list list = [[]]
  11.   | getPermutations (x::xs) =
  12.       collect ((map (insertVariable (x)))(getPermutations(xs)));
  13.  
  14. fun buildTreePossibilities (nil : Tree list, nil : Tree list) : Tree list = nil
  15.   | buildTreePossibilities (leftTrees, nil) = nil
  16.   | buildTreePossibilities (leftTrees, right::rightTrees) =
  17.       let
  18.         fun addRightPossibilities (nil, currRight) = nil
  19.           | addRightPossibilities (l::lefts, currRight) : Tree list =
  20.           Branch(l, currRight)::addRightPossibilities(lefts, currRight);
  21.       in
  22.         addRightPossibilities(leftTrees, right)@buildTreePossibilities(leftTrees, rightTrees)
  23.       end;
  24.  
  25. fun buildTrees (nil : string list list) : Tree list = nil
  26.   | buildTrees (x::xs) =
  27.     let
  28.       fun treeFromPerm(nil, n) : Tree list = nil
  29.         | treeFromPerm(list, n) =
  30.             if length(list) = 1 then [Node(concat(list))]
  31.             else if n = (length(list)) then nil
  32.             else (buildTreePossibilities(treeFromPerm(List.take(list, n), 1),treeFromPerm(List.drop(list, n), 1)))@treeFromPerm(list, n + 1)
  33.     in
  34.       treeFromPerm(x, 1)@buildTrees(xs)
  35.     end;
  36.  
  37. buildTrees(getPermutations(["a", "b", "c", "d", "e"]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement