'foldl function in SML/NJ: Right-hand-side of clause doesn't agree with function result type
I have a function named dfs
which produces a list of all visited nodes in a graph represented by a list of tuples such as [(node1,node2,weight),....]
and I get this error :
v.sml:72.7-79.16 Error: right-hand-side of clause doesn't agree with function result type [circularity]
expression: ''Z -> ''Z list
result type: ''Z list -> ''Z list
in declaration:
find_visited = (fn arg => (fn <pat> => <exp>))
uncaught exception Error
raised at: ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
../compiler/TopLevel/interact/evalloop.sml:44.55
../compiler/TopLevel/interact/evalloop.sml:292.17-292.20
-
Code :
fun succesors n e =
List.map (fn (_, v, _) => v) (List.filter (fn (u, _, _) => n = u) e)
fun dfs graph start =
let
fun find_visited visited node =
if not (List.exists (fn x => x = node) visited) then
let
val s = (succesors node graph)
in
foldl (fn (v, n) => (find_visited v n)) (node::visited) s
end
else
visited
in
find_visited [] start
end
Solution 1:[1]
foldl
has the type ('a * 'b -> 'b) -> 'b -> 'a list -> 'b
.
That is, the function to fold is list_element * result -> result
, and (fn (v, n) => ...
should be (fn (n, v) => ...
(This is the opposite order to OCaml's fold_left
.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Chris |