0
votes

I'm struggling with SML because I need it for my master thesis but I've never used before. I need to define a function which takes as input a list of tuple of kind: (string * string * string) list

and return a list of different tuple of kind: ((string * (string * string * string)) * ((string * string * string) * string)) list

The problem is that I'm using the recursive concept to create a dynamic list but I cannot find a way. My code so far is:

fun insertRelationLts ((x,y,z),nil) = 
        let val h=(x,y,z)
        in [((x,h),(h,z))]
        end
| insertRelationLts ((x,y,z),(a,b,c)::(d,e,f)) = 
        let val h=(x,y,z)
            val q=x
            val w=z
        in ((q,h),(h,w))::insertRelationLts((a,b,c),(d,e,f))     
        end

I hope someone can help me. Thanks a lot!

1

1 Answers

2
votes

The cons operator :: expects a list on the right, but the tuple (d,e,f) isn't one. There is no reason to pattern-match it either, so just replace both occurrences of (d,e,f) with a variable and it should type-check. You can also simplify a little:

fun insertRelationLts (h as (x,y,z), nil) = [((x,h),(h,z))]
  | insertRelationLts (h as (x,y,z), t::rest) = 
      ((x,h),(h,z))::insertRelationLts(t, rest)     

I would, however, recommend using records or at least some type abbreviations for more clarity.

Edit: In fact, this function can be expressed even more easily without explicit recursion:

fun insertRelationsLts (h, list) =
    map (fn h as (x,y,z) => ((x,h), (h,z))) (h::list)