I have a list of strings and numbers like
let stringsAndNums = [("aa-",20); ("b1",20); ("aa",10); ("b12",10); ("+aa-",30)]
I need to break the list into groups of strings that are included one in the other.
For each of the above groups, I need to find the minimum and maximum value.
This is what I tried to do: it works, but I don’t think it is idiomatic F #, and I think I should avoid for loops.
for tup in stringsAndNums do
let subject, value = tup
let related =
stringsAndNums |> List.filter(
fun o ->
let osubject, ovalue = o;
osubject.Contains(subject) || subject.Contains(osubject);
)
let relvalues = related |> List.map(fun o ->
let osubject, ovalue = o;
ovalue
)
let min = (relvalues |> List.min)
let max = (relvalues |> List.max)
printfn "%A" (subject, value, min, max, (max - min))
Also, how can I define a function that returns a list of set results, instead of printing them?
The required conclusion.
The results I get look great
("aa-", 20, 10, 30, 20)
("b1", 20, 10, 20, 10)
("aa", 10, 10, 30, 20)
("b12", 10, 10, 20, 10)
("+aa-", 30, 10, 30, 20)
In fact, two groups in this case are formed from
+aa-with a value of 30, aawith a value of 10, aa-with a value of 20, therefore max is 30 and min is 10b1with a value of 20, b12with a value of 10
My decision
: for-loop, ?
let results =
stringsAndNums |> List.map(fun tup ->
//for tup in stringsAndNums do
let subject, value = tup
let related =
stringsAndNums |> List.filter(
fun o ->
let osubject, ovalue = o;
osubject.Contains(subject) || subject.Contains(osubject);
)
//for reltup in related do
let relvalues = related |> List.map(fun o ->
let osubject, ovalue = o;
ovalue
)
let min = (relvalues |> List.min)
let max = (relvalues |> List.max)
printfn "%A" (subject, value, min, max, (max - min))
)
for result in results do
printf "%A" result