Sorry but I need your help again! I need a function which takes as input a
list of (string*string)
and returns as output a
list of (int*int)
The function should manipulate the list such that, every element that is represented by the same string has to be replaced by the same number. For example if I insert:
changState([("s0","l0"),("l0","s1"),("s1","l1"),("l1","s0")]);
the output should be: val it = [(0,1),(1,2),(2,3),(3,0)]
Is there someone who has an idea to solve this problem? I would really appreciate that. Thanks a lot!
Here is one way:
Here
lp
takes the list of string pairs, a map which relates strings to integers, and a variablemax
, which keeps track of the next unused number. In each iteration, we lookup the two strings in the map. If they are found, return that integer, otherwise, use the next available integer. The last case, where neither string exists in the map, we need to check if the strings are equal, in case your input was[("s0", "s0")]
, we would expect[(0, 0)]
. If they are equal, we map them to the same number, otherwise create distinct ones.If you are not familiar with functors, the first 5 lines are creating a structure that satisfies the
ORD_KEY
signature. You can find more details in the documentation atBy applying the
ReBlackMapFn
functor to theStringKey
structure, it creates a new structure of a map (implemented as a red black tree) that maps strings to things of type'a