0

I am trying to code an SML function that returns an array of results in listViolations(L1, L2). I specifically want to cross reference each element with eachother O(n^2), and check to see if the selection conflicts with one another. To visualize: [[1, 2], [2, 3]] is option 1 and [[3, 2], [2, 1]] is option two. I would call listViolations like this: listViolations([[[1, 2], [2, 3]], [[3, 2], [2, 1]]]).

The plan of action would be to :

fun listViolations(L1, L2) =
    if L1 = [] orelse L2 = []
        then 0
     else totalViolations(transitive_closure(hd(L1)),path(hd(L2)))::[] @ listViolations(L1, tl(L2))::[] @ listViolations(tl(L1), L2)::[];

Here I am checking the head of both lists, and passing on the tails of both recursively, in hopes of creating something like this: [3, 0 , 0].

Although I am getting this error when I declare the function:

stdIn:726.5-728.136 Error: types of if branches do not agree [overload conflict]
then branch: [int ty]
else branch: int list
in expression:
  if (L1 = nil) orelse (L2 = nil)
  then 0
  else totalViolations (transitive_closure <exp>,path <exp>) ::
       nil @ listViolations <exp> :: <exp> @ <exp> 

I provided all my other functions below to show that there's nothing wrong with them, I just want to know if there's something im doing wrong. I know for a fact that

  • totalViolations(transitive_closure(hd(L1)),path(hd(L2)))
  • listViolations(L1, tl(L2))::[]
  • listViolations(tl(L1), L2)::[];

return integers. How do I make a list out of it and return it within this function? Thank you in advance.

//[1, 2] , [1, 2, 3] = 0
//[3, 2] , [1, 2, 3] = 1
fun violation(T, P) =
    if indexOf(hd(T), P) < indexOf(hd(tl(T)), P)  then 0
    else 1;

//[[1, 2], [2, 3]] , [1, 2, 3] = 0
//[[3, 2], [2, 1]] , [1, 2, 3] = 2
fun totalViolations(TS, P) =
    if TS = [] then 0
    else violation(hd(TS), P) + totalViolations(tl(TS), P);

//[[1, 2],[2, 3]] -> [1, 2, 3]
fun path(L) =
    if L = [] orelse L =[[]]
        then []
    else union(hd(L),path(tl(L)));

// [[1, 2],[2, 3]] -> [[1, 2],[2, 3], [1, 3]]
fun transitive_closure(L) = union(L, remove([], closure(L, L)));

Additional Code:

fun len(L) = if (L=nil) then 0 else 1+length(tl(L));

fun remove(x, L) =
    if L = [] then []
    else if x = hd(L) then remove(x, tl(L))
    else hd(L)::remove(x, tl(L));

fun transitive(L1, L2) =
    if len(L1) = 2 andalso len(L2) = 2 andalso tl(L1) = hd(L2)::[]
        then hd(L1)::tl(L2)
    else [];

fun closure(L1, L2) =
    if (L1 = [[]] orelse L2 = [[]] orelse L1 = [] orelse L2 = [])
        then [[]]
    else if len(L1) = 1 andalso len(L2) = 1
        then transitive(hd(L1), hd(L2))::[]
    else
        union( union(closure(tl(L1), L2), closure(L1, tl(L2))), transitive(hd(L1), hd(L2))::[]);
EDU Codes
  • 11
  • 4

1 Answers1

1

The then branch of your if is an int while the else branch is a list of ints. To form a list in the former, write [0] (which is just short for 0::[]). Also, the result of the recursive calls in the other branch is already expected to return a list, so the consing with [] is wrong, because it forms a list of lists.

More tips: never compare to the empty list, that will force the element type to be an equality type. Use the null predicate instead. Even better (and much more readable), avoid null, hd, and tail altogether and use pattern matching.

Andreas Rossberg
  • 34,518
  • 3
  • 61
  • 72
  • - fun listViolations(L1, L2) = = if L1 = [] orelse L2 = [] = then [0] = else totalViolations(transitive_closure(hd(L1)),path(hd(L2)))::[] @ listViolations(L1, tl(L2))::[] @ listViolations(tl(L1), L2)::[] stdIn:649.2-734.136 Error: right-hand-side of clause doesn't agree with function result type [tycon mismatch] expression: int list result type: int in declaration: listViolations = (fn (L1,L2) => if orelse then :: else :: ) What would I need to get the desired output, a list of ints? – EDU Codes Sep 30 '17 at 20:56
  • Remove the `::[]` after the two recursive calls. – Andreas Rossberg Sep 30 '17 at 21:09
  • Im sorry, I tired and Im still getting errors. Can you please show me the code for the working version? – EDU Codes Sep 30 '17 at 21:41