I try to put a branching diagram into lists and sublists
layout is
- msg01
- msg02
- msg03
- msg04
- msg05
And i want to walk the branching diagram top -> bottom going into each branch
but it fails with
Error Type mismatch. Expecting a
('a * 'b list) listbut given a'b listThe resulting type would be infinite when unifying ‘'a‘ and ‘'b * 'a list‘
Can anybody gives me some ideas what could fix this?
open System
let msgDiagram1 = [ ("msg03",[]); ("msg04",[]) ]
let msgDiagram = [ ("msg01", []); ("msg02", msgDiagram1); ("msg05",[]) ]
let listToString lst =
let rec loop acc = function
| [] -> acc
| x::xs -> let node = x
let sublst = snd(node)
if not ( sublst = List.empty ) then
loop "" sublst
else
loop (acc + (string x)) xs
loop "" lst
printfn "%A" (listToString msgDiagram)
Since
msgDiagram1andmsgDiagramhave different types, you cannot use the sameloopfunction on them. One quick fix is to process differently onmsgDiagram1:You chose a wrong data structure for the job; therefore, you cannot represent more than 2 levels of diagrams and these levels are forced to have different types. The right tool to use here is recursive tree data structure:
Now above values can be defined using the same type
Diagram list:and
listToStringcan be rewritten to processDiagram listrecursively in one batch.