Tuesday, 27 May 2014

Prolog Program to replace a character with another character in the List.

Prolog Program to replace a character with another character in the List.

Program:

domains
list=symbol*
 
predicates
replace(symbol,symbol,list,list) //first symbol is replaced by second symbol

clauses
replace(_,_,[],[]).
replace(X,Y,[X|T],[Y|NT]):-replace(X,Y,T,NT).
replace(X,Y,[H|T],[H|NT]):-H<>X,replace(X,Y,T,NT).
    

goal with output

goal: replace(a,c,[a,b,c,d,a,b],X)
output: X=["c","b","c","d","c","b"]
           1 solution


BackList Programs | Next

No comments:

Post a Comment