1

I am completely new to Prolog, but I have to do this for a homework. I have tried something like this

delete(_,[],[]).  
delete([X,Y,Z],[X,Y,Z|List],Temp) :-  
   reverse(Temp,List).

But can't figure out, how to implement second delete from reversed list. Maybe I'm doing it all wrong, I'm lost, thanks for answers.

false
  • 10,264
  • 13
  • 101
  • 209
kristofyk
  • 41
  • 1
  • 1
  • 4
  • You are not only new to Prolog but also new to SO. Before asking questions, get informed by reading [the tour](http://stackoverflow.com/tour). – false Nov 29 '14 at 14:48
  • What is expected behavior if 2 < number of elements < 6, say there are 5 or 3 elements in the original list? – Sergii Dymchenko Nov 29 '14 at 19:03
  • @SergeyDymchenko It's quite interesting question, i haven't thought about it, because it is not in my task. I think, it would be ok, if it threw "false" or something like that. Please understand, i don't understand prolog at all, this is my first attempt to do something in it. – kristofyk Nov 30 '14 at 20:08

1 Answers1

0

Using append:

delete([A, B, C | End], Middle, [A, B, C, X, Y, Z]) :-
    append(Middle, [X, Y, Z], End).

Test run:

?- delete([1,2,3,4,5,6,7,8], L1, L2).
L1 = [4, 5],
L2 = [1, 2, 3, 6, 7, 8] 

?- delete([1,2,3], L1, L2).
false.
Sergii Dymchenko
  • 6,890
  • 1
  • 21
  • 46
  • 1
    What about `[a, b, c]`: I delete the first 3 (a,b,c) and the last 3 (a,b,c) so I get `[]`. Is it not? – false Nov 29 '14 at 18:54
  • @false I agree. I'll ask the OP if he cares about cases. – Sergii Dymchenko Nov 29 '14 at 18:58
  • @false As I answered above, i didn't think about this. But it seems your code works for me. Meanwhile i came with my solution with using only delete, but I think i'll use your code. Anyway thank you for answer. – kristofyk Nov 30 '14 at 20:21