how to check if a path is well formed with FsCheck in FSharp

278 views Asked by At

I'm struggling getting an answer to this:

Define a function functionWF and functionPath that takes an FsTree and returns a boolean that check whether the given tree is well-formed as a filesystem and whether the path (represented as a list of strings) is well-formed.

A well-formed filesystem cannot have identical paths leading to different nodes in the tree.

A well-formed path cannot contain nodes with empty names.

bellow a the type FsTree = Node of (string * FsTree) list

and bellow is an example of a FsTree :

fsT = [Node ("f1", [Node ("f2", [])]); [Node ("f3", [])]]
1

There are 1 answers

0
jaderberg On

A Node is ill-formed if it contains more than one element with the same name, or if that is the case for any of the subtrees that it contains. Specifically, Node [] is well-formed. Those concepts can be the cases the recursive function functionWF:

let rec functionWF (tree : FsTree) : bool =
    match tree with 
    | Node [] -> true
    | Node list -> 
        let strings = List.map fst list
        let trees = List.map snd list
        let namesOk = allElementsUnique strings
        let subtreeOk state tree = state && (functionWF tree)
        List.fold subtreeOk namesOk trees

where allElementsUnique is a function that ensures that there are no duplicate elements in a list.

I don't understand what you mean that functionPath should do.

PS. Your example of an FsTree is not valid, it should have Node outside of the list:

let fsT = Node [("f1", Node [("f2", Node [])]); ("f3", Node [])]