开发者

How to write myDel with append in Prolog?

开发者 https://www.devze.com 2023-03-07 14:20 出处:网络
I\'m new to Prolog and st开发者_StackOverflow中文版uck on some programming homework. one of them should work like this:

I'm new to Prolog and st开发者_StackOverflow中文版uck on some programming homework. one of them should work like this:

myDel(1, [1, 2, 1, 3, 1, 4], M).

the result should be:

M = [2, 3, 4].

to solve this, one can only use append recursively and can not use the built-in delete.

Can somebody help?


I must say that it's quite difficult to help without just spoon-feeding you the answer. Prolog is a bit like that. Here's a partial answer that hopefully doesn't give too much away:

myDel(N, [], []).
myDel(N, [N|T], U) :- ...


Sorry! Cut was needed.

myDel(N,L,DelL) :-
   append(L0,[N|R],L),
   myDel(N,R,DelL2),
   append(L0,DelL2,DelL),
   !.
myDel(_,L,L).
0

精彩评论

暂无评论...
验证码 换一张
取 消