# Kindly Requesting Guidance and Direction

**URL:** <https://swi-prolog.discourse.group/t/kindly-requesting-guidance-and-direction/6521>\
**Category:** Help!\
**Created:** [May 3, 2023, 6:44pm UTC](https://swi-prolog.discourse.group/t/kindly-requesting-guidance-and-direction/6521 "2023-05-03T18:44:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hcxs](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/hcxs/32/5456_2.png) [@hcxs](https://swi-prolog.discourse.group/u/hcxs)\
**Post date:** [May 3, 2023, 6:44pm UTC](https://swi-prolog.discourse.group/t/kindly-requesting-guidance-and-direction/6521/1 "2023-05-03T18:44:05Z")

</div>

Hello swi-prolog,

Thank you in advance for your time and advice.

I have been tasked to write a single predicate and single rule prolog that takes in a list and find if it have 3 items in decreasing by order of 2. true if it does and false if not.

My single predicate and single rule:

three\_by\_two([A, B, C | T]) :- B is C - 2, A is B - 2, three\_by\_two([B, C | T]).

Tests:  
three\_by\_two([1, 10, 5, 3, 42, 23, 5]). %“false”

three\_by\_two([5, 23, 42, 3, 5, 10, 1]). % “true, for [5, 3,1].”

During trace, I see that the predicate and rule is not performing the rule through all the elements of the list. Kindly requesting guidance and direction as to how to properly solve the problem and understand the solution.

Allowed constructs:  
1.“is”  
2.“-”  
3.“:-”  
4.“,”  
5.“;”  
6. “”  
7.“[H|T]”  
8. “!”

Thank you,  
hcxs

---

<div class="post-metadata">

**Author:** ![hcxs](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/hcxs/32/5456_2.png) [@hcxs](https://swi-prolog.discourse.group/u/hcxs)\
**Post date:** [May 3, 2023, 7:32pm UTC](https://swi-prolog.discourse.group/t/kindly-requesting-guidance-and-direction/6521/3 "2023-05-03T19:32:53Z")

</div>

Thank you for your reply, but “select” wasn’t allowed by the task.

---

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [May 3, 2023, 7:50pm UTC](https://swi-prolog.discourse.group/t/kindly-requesting-guidance-and-direction/6521/4 "2023-05-03T19:50:36Z")

</div>

Well, there’s select/3 and permutation/2 , as hints.

On the swi-prolog pages, you can click on the orange circle, to see the source code.

---

<div class="post-metadata">

**Author:** ![hcxs](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/hcxs/32/5456_2.png) [@hcxs](https://swi-prolog.discourse.group/u/hcxs)\
**Post date:** [May 3, 2023, 8:02pm UTC](https://swi-prolog.discourse.group/t/kindly-requesting-guidance-and-direction/6521/5 "2023-05-03T20:02:42Z")

</div>

Thank you very much for your guidance and for most cases it would work! But for the single predicate, single rule requirement in addition to the constraints the recursive predicate for select/member and permutations would not fit it.

This are the predicates I cant use because of the requirements and constraints:

1. member/select (from: [https://www.tutorialspoint.com/prolog/prolog\_lists.htm](https://www.tutorialspoint.com/prolog/prolog_lists.htm))

list\_member(X,[X|_]).  
list\_member(X,[_|TAIL]) :- list\_member(X,TAIL).

This one is the permutation\_sort from ([LogicLambda: [https://youtu.be/yi30r4gU4uE](https://youtu.be/yi30r4gU4uE?t=1378)

---

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [May 3, 2023, 8:37pm UTC](https://swi-prolog.discourse.group/t/kindly-requesting-guidance-and-direction/6521/6 "2023-05-03T20:37:33Z")

</div>

> [@hcxs](#):
>
> [5, 3,1]

Why is that acceptable - it’s not 10 elements?

---

<div class="post-metadata">

**Author:** ![hcxs](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/hcxs/32/5456_2.png) [@hcxs](https://swi-prolog.discourse.group/u/hcxs)\
**Post date:** [May 3, 2023, 8:39pm UTC](https://swi-prolog.discourse.group/t/kindly-requesting-guidance-and-direction/6521/7 "2023-05-03T20:39:18Z")

</div>

Apologies, typo. 3 instead of 10. Corrected.

---

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [May 4, 2023, 7:20pm UTC](https://swi-prolog.discourse.group/t/kindly-requesting-guidance-and-direction/6521/8 "2023-05-04T19:20:03Z")

</div>

> [@hcxs](#):
>
> This are the predicates I cant use because of the requirements and constraints:
> 
> 1. member/select

Can’t use _why_? These commands are built-in, ready for use.

Presumably your programming teacher thinks it’s a good idea to put you through this frustration, rather than enjoy the power of the language?

I don’t think it’s a good way to teach or learn ☹

---

<div class="post-metadata">

**Author:** ![hcxs](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/hcxs/32/5456_2.png) [@hcxs](https://swi-prolog.discourse.group/u/hcxs)\
**Post date:** [May 4, 2023, 7:53pm UTC](https://swi-prolog.discourse.group/t/kindly-requesting-guidance-and-direction/6521/9 "2023-05-04T19:53:00Z")

</div>

I completely agree. Thank you Brebs for your inputs, it means a lot. Have a great weekend!

---

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [May 4, 2023, 9:06pm UTC](https://swi-prolog.discourse.group/t/kindly-requesting-guidance-and-direction/6521/10 "2023-05-04T21:06:36Z")

</div>

Here’s 3 predicates to show how it can be done (with the first 2 initially hidden, in case you just want a clue):

> **select\_3\_minus\_2**
>
> ```prolog
> select_3_minus_2(Lst, Three) :-
> select_forward(N1, Lst, Lst0),
> select_num_minus_2(Lst0, N1, N2, Lst00),
> select_num_minus_2(Lst00, N2, N3, _),
> Three = [N1, N2, N3]. 
> 
> ```

> **select\_num\_minus\_2**
>
> ```prolog
> select_num_minus_2(Lst, N, Minus2, Lst0) :-
> Minus2 is N - 2,
> select_forward(Minus2, Lst, Lst0).
> 
> ```

```prolog
select_forward(E, [H|T], F) :- 
    select_forward_(T, H, E, F).

select_forward_(T, H, H, T).
select_forward_([H|T], _, E, F) :-
    select_forward_(T, H, E, F).

```

Result:

```prolog
?- select_3_minus_2([5, 23, 42, 3, 5, 10, 1], L).
L = [5, 3, 1] ;
false.

```

Add the cut (i.e. `!`) if desired.

---

<div class="post-metadata">

**Author:** ![hcxs](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/hcxs/32/5456_2.png) [@hcxs](https://swi-prolog.discourse.group/u/hcxs)\
**Post date:** [May 4, 2023, 10:00pm UTC](https://swi-prolog.discourse.group/t/kindly-requesting-guidance-and-direction/6521/11 "2023-05-04T22:00:52Z")

</div>

Thank you Brebs!
