目标:
rovarsprak([104, 101, 106], B).
B = [104,111,104,101,106,111,106]
[104,101,106] =" hej"
B =" hohejoj"
目前:
rovarsprak([104, 101, 106], B).
B = [104,111,104,106,111,106]
[104,101,106] =" hej"
B =" hohjoj"
(例如,我失去了中间的,不受影响的元素(101 =" e")。)
代码:
voal(97). % 97 = a
voal(111). % 111 = o
voal(117). % 117 = u
voal(101). % 101 = e
voal(105). % 105 = i
voal(121). % 121 = y
isLowerCase(A) :-
A > 96, % small A is 97
A < 123. % small Z is 122
rovarsprak([], []).
rovarsprak([A|AS], [X,Y,Z|ZS]) :-
A = X, A = Z, Y = 111,
isLowerCase(A),
(\+ voal(A)) % if
-> rovarsprak(AS, ZS) % then
; rovarsprak(AS, [X,Y,Z|ZS]). % else
我觉得我在这里错过了这一点......
答案 0 :(得分:1)
问题在于,在没有实例化X,Y,Z中的任何一个的情况下递归调用谓词的情况。你应该实例化X = A,这样你的列表就不会像现在这样忽略A.因此,您需要的唯一更改是在A=X
之后添加;
并调用rovarsprak(AS, [Y,Z|ZS]).
,因为X已实例化:
rovarsprak([], []).
rovarsprak([A|AS], [X,Y,Z|ZS]) :-
A = X, A = Z, Y = 111,
isLowerCase(A),
(\+ voal(A)) % if
-> rovarsprak(AS, ZS) % then
; A=X,rovarsprak(AS, [Y,Z|ZS]). % else
示例:
?- rovarsprak([104, 101, 106], B).
B = [104, 111, 104, 101, 106, 111, 106].