How can I compare two lists in prolog, returning true if the second list is made of every other element of list one?

spjz

I would solve it by comparing the first index of the first list and adding 2 to the index. But I do not know how to check for indexes in prolog. Also, I would create a counter that ignores what is in the list when the counter is an odd number (if we start to count from 0). Can you help me? Example: everyOther([1,2,3,4,5],[1,3,5]) is true, but everyOther([1,2,3,4,5],[1,2,3]) is not.

repeat

We present three logically-pure definitions even though you only need one—variatio delectat:)

  1. Two mutually recursive predicates list_oddies/2 and skipHead_oddies/2:

    list_oddies([],[]).
    list_oddies([X|Xs],[X|Ys]) :-
       skipHead_oddies(Xs,Ys).
    
    skipHead_oddies([],[]).
    skipHead_oddies([_|Xs],Ys) :-
       list_oddies(Xs,Ys).
    
  2. The recursive list_oddies/2 and the non-recursive list_headless/2:

    list_oddies([],[]).
    list_oddies([X|Xs0],[X|Ys]) :-
       list_headless(Xs0,Xs),
       list_oddies(Xs,Ys).
    
    list_headless([],[]).
    list_headless([_|Xs],Xs).
    
  3. A "one-liner" which uses foldl/4 in combination with Prolog lambdas:

    :- use_module(library(lambda)).
    
    list_oddies(As,Bs) :-
       foldl(\X^(I-L)^(J-R)^(J is -I,( J < 0 -> L = [X|R] ; L = R )),As,1-Bs,_-[]).
    

All three implementations avoid the creation of useless choicepoints, but they do it differently:

  • #1 and #2 use first-argument indexing.
  • #3 uses (->)/2 and (;)/2 in a logically safe way—using (<)/2 as the condition.

Let's have a look at the queries @WouterBeek gave in his answer!

?- list_oddies([],[]),
   list_oddies([a],[a]),
   list_oddies([a,b],[a]),
   list_oddies([a,b,c],[a,c]),
   list_oddies([a,b,c,d],[a,c]),
   list_oddies([a,b,c,d,e],[a,c,e]),
   list_oddies([a,b,c,d,e,f],[a,c,e]),
   list_oddies([a,b,c,d,e,f,g],[a,c,e,g]),
   list_oddies([a,b,c,d,e,f,g,h],[a,c,e,g]).
true.                                           % all succeed deterministically

Thanks to , we get logically sound answers—even with the most general query:

?- list_oddies(Xs,Ys).
  Xs = [],                        Ys = []
; Xs = [_A],                      Ys = [_A]
; Xs = [_A,_B],                   Ys = [_A]
; Xs = [_A,_B,_C],                Ys = [_A,_C]
; Xs = [_A,_B,_C,_D],             Ys = [_A,_C]
; Xs = [_A,_B,_C,_D,_E],          Ys = [_A,_C,_E]
; Xs = [_A,_B,_C,_D,_E,_F],       Ys = [_A,_C,_E]
; Xs = [_A,_B,_C,_D,_E,_F,_G],    Ys = [_A,_C,_E,_G]
; Xs = [_A,_B,_C,_D,_E,_F,_G,_H], Ys = [_A,_C,_E,_G]
...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how can I compare one int element with a list int

From Dev

How can I combine two lists into one long list?

From Dev

Split a list into two - one with every second element, and another with all the rest

From Dev

Split a list into two - one with every second element, and another with all the rest

From Dev

Prolog: Compare 2 Lists and find out if at least one member of the first list exists in the other one

From Dev

How to convert list of lists to a set in python so I can compare to other sets?

From Dev

In Python, how can I randomly chose an element of one list that's NOT in a second list?

From Dev

How to construct a list from two lists in prolog

From Dev

In R, compare two lists of strings, find parts of each element of the first list on the second

From Dev

In R, compare two lists of strings, find parts of each element of the first list on the second

From Dev

compare elements in list to every other element in same list

From Java

How can I create a new list as a result of combing two other lists?

From Dev

How can I create a list that is created using the indexes and items of two other lists?

From Dev

How can I replace an element of a list using an index in PROLOG

From Dev

How can one pull every other value within a list?

From Dev

Prolog - Working with a list of lists and an element inside one of the sublists

From Dev

Prolog, split list into two lists

From Dev

Prolog, split list into two lists

From Dev

How can I remove every other item from a Sass list?

From Dev

I have two lists containing the same objects. How do I change one list without changing the other?

From Dev

split list into 2 lists corresponding to every other element

From Dev

Extract second element from every item in a list and other questions

From Dev

How can I make a Python function examine every element of a list?

From Dev

Given a list, how do I construct a new list such that every element of the new list is the sum of every two numbers in the old list?

From Dev

How can I sort list by second element without using lambda?

From Dev

haskell, map two lists adding each element of one with each of the other list

From Dev

SML How to prepend an element to the beginning of every list in a list of lists?

From Dev

How can I sort a list of lists by one of the items in the sublists?

From Dev

How can I have changes made to one list affect another, different list?

Related Related

  1. 1

    how can I compare one int element with a list int

  2. 2

    How can I combine two lists into one long list?

  3. 3

    Split a list into two - one with every second element, and another with all the rest

  4. 4

    Split a list into two - one with every second element, and another with all the rest

  5. 5

    Prolog: Compare 2 Lists and find out if at least one member of the first list exists in the other one

  6. 6

    How to convert list of lists to a set in python so I can compare to other sets?

  7. 7

    In Python, how can I randomly chose an element of one list that's NOT in a second list?

  8. 8

    How to construct a list from two lists in prolog

  9. 9

    In R, compare two lists of strings, find parts of each element of the first list on the second

  10. 10

    In R, compare two lists of strings, find parts of each element of the first list on the second

  11. 11

    compare elements in list to every other element in same list

  12. 12

    How can I create a new list as a result of combing two other lists?

  13. 13

    How can I create a list that is created using the indexes and items of two other lists?

  14. 14

    How can I replace an element of a list using an index in PROLOG

  15. 15

    How can one pull every other value within a list?

  16. 16

    Prolog - Working with a list of lists and an element inside one of the sublists

  17. 17

    Prolog, split list into two lists

  18. 18

    Prolog, split list into two lists

  19. 19

    How can I remove every other item from a Sass list?

  20. 20

    I have two lists containing the same objects. How do I change one list without changing the other?

  21. 21

    split list into 2 lists corresponding to every other element

  22. 22

    Extract second element from every item in a list and other questions

  23. 23

    How can I make a Python function examine every element of a list?

  24. 24

    Given a list, how do I construct a new list such that every element of the new list is the sum of every two numbers in the old list?

  25. 25

    How can I sort list by second element without using lambda?

  26. 26

    haskell, map two lists adding each element of one with each of the other list

  27. 27

    SML How to prepend an element to the beginning of every list in a list of lists?

  28. 28

    How can I sort a list of lists by one of the items in the sublists?

  29. 29

    How can I have changes made to one list affect another, different list?

HotTag

Archive