List prolog中相同列表上的两个基于条件的替换函数?

List prolog中相同列表上的两个基于条件的替换函数?,list,prolog,List,Prolog,我需要根据其值替换列表中的元素,对于某些类型的值,我需要执行一种替换,对于第二种,我有一个辅助函数,返回我要用作替换的元素,因此: %receives a character (a 'code') and returns a list with other characters (the 'value' of that code) auxiliarFunction(Char, Response) 我需要做的一个例子: 我需要一个函数来接收数字和字母列表。如果字母“a”出现,我想用字符*替换它,

我需要根据其值替换列表中的元素,对于某些类型的值,我需要执行一种替换,对于第二种,我有一个辅助函数,返回我要用作替换的元素,因此:

%receives a character (a 'code') and returns a list with other characters (the 'value' of that code)
auxiliarFunction(Char, Response)
我需要做的一个例子:

我需要一个函数来接收数字和字母列表。如果字母“a”出现,我想用字符
*
替换它,并将其转换为
Res
变量,但如果它不是“a”,我需要调用
辅助函数
和concat
响应
Res
(假设它是一个字母列表中的数字,这只是一个例子,因为真正的辅助函数很复杂,但输出很简单)

伪代码:

replaceChars([], [])
replaceChars([X|Xs], Res)
% if x == a then Res will have [*] (concat '*' to Res)
% else auxiliarFunction(X, Response) and concat Response to Res (Response will be a list)
% replaceChars(Xs, Res) make recursion call
例如,有效的输入可以是
[2,a,2,a]
,输出(Res)应该是
[t,w,o,*,t,w,o,*]


如何在同一个列表上生成这两个条件替换函数?

如果A then B else C
在prolog中写成
A->B;C

replaceChars([], []).
replaceChars([X|Xs], Res) :-
    replaceChars(Xs, Res1),
    (  X = a
    -> Res = [* | Res1]
    ;  auxillaryFunction(X, Response),
       append(Response, Res1, Res)
    ).

我认为应该分别检查输入的有效性,
isValid(Xs),replaceChars(Xs,Ys)。


我编辑了问题,不需要验证,但您的解决方案非常有效
isValid([_]).
isValid([a, X | Xs]) :- dif(a, X), isValid([X|Xs]).
isValid([X, a | Xs]) :- dif(a, X), isValid([a|Xs]).