recursion - Why does this Prolog code not run? -
i have written following code replace occurrences of given element in list, specified value. here's code:
substitute(x, y, [], []). substitute(x, y, [x|t], [y|r]) :- substitute(x, y, t, r). substitute(x, y, [h|t], [h|r]) :- h\= x, substitute(x, y, t, r).
i trying call in gnu prolog:
substitute(2, 3, [2, 2, 2], []).
which results in:
no
what wrong here? don't see wrong. comparing head of given list see if matches x. if matches, replace in output list y , recurse on tail.
in second case, if head doesn't match x, recurse on tail , check if element of tail same x , recursion replaces per 2nd definition. @ end of tail, when empty list encountered, recursion moves base case , terminates. not sure, why not able run code. please.
Comments
Post a Comment