module Dfs = struct
let rec dfslsts g paths final =
let l = PrimePath.removeDuplicates (PrimePath.extendPaths g paths)
in
let f elem =
if (List.mem "%d" (List.flatten final) = false) then (dfslsts g ["%d"] (List.flatten l)::final)
else final
in
List.iter f (Graph.nodes g)
end
Fehler: Dieser Ausdruck hat Typ String, sondern Ausdruck wurde vom Typ int Liste erwartetrekursive Aufruf, wenn expression - ocaml
Dieser Fehler trat auf, wenn ich dfslsts Funktion aufgerufen, die rekursiv ist, in der, wenn die Bedingung . Die Funktion dfslsts gibt eine Liste von Listen zurück. Wenn ich versuche, den komplexen Ausdruck in if-Anweisung zu
if (List.mem "%d" (List.flatten final) = false) then "%d"
else "%d"
dann bekomme ich Fehler zu ersetzen: Dieser Ausdruck hat Typ ‚a -> string sondern Ausdruck wurde vom Typ erwartet‘ ein -> Einheit Typ Der String ist nicht kompatibel mit dem Typ in der List.iter-Zeile.
Wie löse ich dieses Problem und darf eine rekursive Funktion innerhalb des if-Ausdrucks aufrufen.
Dies ist die Definition meiner Diagrammtyp:
module Graph = struct
exception NodeNotFound of int
type graph = {
nodes : int list;
edges : (int * int) list;
}
let makeGraph() =
{
nodes = [];
edges = [];
}
let rec isNodeOf g n = List.mem n g.nodes
let nodes g = g.nodes
let edges g = g.edges
let addNode g n =
let nodes = n::g.nodes and edges = g.edges in
{
nodes;
edges;
}
let addEdge g (n1, n2) =
if ((isNodeOf g n1) = false) then
raise (NodeNotFound n1)
else if ((isNodeOf g n2) = false) then
raise (NodeNotFound n2)
else
let nodes = g.nodes
and edges = (n1, n2) :: g.edges in
{
nodes;
edges;
}
let nextNodes g n =
let rec findSuccessors edges n =
match edges with
[] -> []
| (n1, n2) :: t ->
if n1 = n then n2::findSuccessors t n
else findSuccessors t n
in
findSuccessors g.edges n
let rec lastNode path =
match path with
[] -> raise (NodeNotFound 0)
| n :: [] -> n
| _ :: t -> lastNode t
end
module Paths = struct
let extendPath g path =
let n = (Graph.lastNode path) in
let nextNodes = Graph.nextNodes g n in
let rec loop path nodes =
match nodes with
[] -> []
| h :: t -> (List.append path [h]) :: (loop path t)
in
loop path nextNodes
let rec extendPaths g paths =
match paths with
[] -> []
| h :: t -> List.append (extendPath g h) (extendPaths g t)
(* Given a list lst, return a new list with all duplicate entries removed *)
let rec removeDuplicates lst =
match lst with
[]
| _ :: [] -> lst
| h :: t ->
let trimmed = removeDuplicates t in
if List.mem h trimmed then trimmed
else h :: trimmed
end
(Es war eine Nebenbemerkung. Bitte zeigen Sie uns die Definition Ihres Diagrammtyps.) –
Ich habe meine Frage aktualisiert, die aus einer Diagrammstruktur besteht. – Jab
Hier (List.flatten l) ist eine Liste und final ist eine Liste von Listen. Ist es ein Problem mit :: dieser Notation? – Jab