# Need help with a homework

**URL:** <https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942>\
**Category:** Help!\
**Created:** [November 16, 2022, 2:39am UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942 "2022-11-16T02:39:41Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodry7](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/rodry7/32/4970_2.png) [@rodry7](https://swi-prolog.discourse.group/u/rodry7)\
**Post date:** [November 16, 2022, 2:39am UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/1 "2022-11-16T02:39:41Z")

</div>

I want the code to:

```prolog
deleteCntg([a,a,b,b,b,c,c,c],[a,b,c]).
true.
deleteCntg([a,a,b,b,b,c,c,c],[a,b,b,c].
false.
deleteCntg([a,b,c,a,b,c],[a,b,c]).
false.
deleteCntg([a,b,c,a,b,c],[a,b,c,a,b,c]).
true.
deleteCntg([[a],[b],[b],[a],[a],[b]], [[a],[b],[a],[b]]).
true.
deleteCntg([[a],b,[a,b,[c]],[a,b,[c]],[a,b,[a]],c],X).
X = [[a],b,[a,b,[c]],[a,b,[a]],c] .

```

What is the best way to do this?

Also I have another problem

```prolog
?- subset_add([10,7,3,4,2,1],7,L).
L = [7] ;
L = [3, 4] ;
L = [4, 2, 1] ;
false.
?- subset_add([1,2,3],0,L).
L = [] ;
false.

```

What is the best way to do this?

---

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [November 16, 2022, 5:31am UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/2 "2022-11-16T05:31:43Z")

</div>

> [@rodry7](#):
>
> What is the best way to do this?

As for `subset_add/3`, how about to use `select/3` for successive selection of elements  
until the sum is obtained ?

As for `deleteCntg/2`, I could not guess its specification; what does it ?

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [November 16, 2022, 5:49am UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/3 "2022-11-16T05:49:55Z")

</div>

> [@kuniaki.mukai](#):
>
> As for `deleteCntg/2`, I could not guess its specification; what does it ?

It seems to do what the `uniq` command does on \*nix: filter adjacent repeats. @rodry7 you could use clumped/2 and drop the counts:

```prolog
uniq(L, U) :-
    clumped(L, C),
    pairs_keys(C, U).

```

---

<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:** [November 16, 2022, 9:18am UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/4 "2022-11-16T09:18:10Z")

</div>

This works (using a better name than `deleteCntg`):

```prolog
remove_same_successors([], []).
remove_same_successors([H|T], [H|LstRemoved]) :-
    remove_same_successors_([H|T], H, LstRemoved).

remove_same_successors_([], _, []).
remove_same_successors_([H|T], H, L) :-
    !,
    remove_same_successors_(T, H, L).
remove_same_successors_([H|T], _Prev, [H|L]) :-
    remove_same_successors_(T, H, L).

```

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [November 16, 2022, 10:42am UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/5 "2022-11-16T10:42:11Z")

</div>

Great, now you did OP’s homework.

---

<div class="post-metadata">

**Author:** ![rodry7](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/rodry7/32/4970_2.png) [@rodry7](https://swi-prolog.discourse.group/u/rodry7)\
**Post date:** [November 16, 2022, 3:31pm UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/6 "2022-11-16T15:31:46Z")

</div>

Hey brebs thank you so much this is so helpful, I appreciate it

---

<div class="post-metadata">

**Author:** ![rodry7](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/rodry7/32/4970_2.png) [@rodry7](https://swi-prolog.discourse.group/u/rodry7)\
**Post date:** [November 16, 2022, 7:29pm UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/7 "2022-11-16T19:29:25Z")

</div>

Hi, Im trying to understand the select/3 functionality but how can I applied it to my second problem?

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [November 16, 2022, 7:52pm UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/8 "2022-11-16T19:52:03Z")

</div>

Just select from the list until you reach the sum or overshoot it. Although maybe it is better to use “subsequence” instead of subset. Depends on whoever gave you your homework and what they expect.

Are you going to attribute our help in your homework submission?

---

<div class="post-metadata">

**Author:** ![rodry7](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/rodry7/32/4970_2.png) [@rodry7](https://swi-prolog.discourse.group/u/rodry7)\
**Post date:** [November 16, 2022, 9:33pm UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/9 "2022-11-16T21:33:54Z")

</div>

Oh ok, thanks.

And yes of course, I always like to mention if I ever got help and this is very helpful forum.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [November 17, 2022, 2:00am UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/10 "2022-11-17T02:00:37Z")

</div>

> [@rodry7](#):
>
> Hi, Im trying to understand the [select/3](https://www.swi-prolog.org/pldoc/doc_for?object=select/3) functionality but how can I applied it to my second problem?

Why not just try using select/3 ?

```prolog
?- select(X, [1,2,3,4], Rest).
X = 1,
Rest = [2,3,4] ;
X = 2,
Rest = [1,3,4] ;
X = 3,
Rest = [1,2,4] ;
X = 4,
Rest = [1,2,3].

```

As you can see, it selects one item from the list and also gives you the list without that item. You can also select two items:

```prolog
?- forall((select(X, [1,2,3], Rest1), select(Y, Rest1, Rest2)), writeln(X-Y-Rest2)).
1-2-[3]
1-3-[2]
2-1-[3]
2-3-[1]
3-1-[2]
3-2-[1]

```

(Yes, this does look rather like permuting the list, doesn’t it?)

---

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [November 17, 2022, 5:57am UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/11 "2022-11-17T05:57:39Z")

</div>

I think subset\_add is a good homework for you to skill up you in Prolog programming, at least because it looks like to tell you some need of “accumulator” to “store” intermediate values which is always used in procedural language like C. Sorry, if this comment makes you confuse. This comment is from my experience at the beginning of my prolog programming. Anyway, I hope you solve it finally.

---

<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:** [November 19, 2022, 2:28pm UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/12 "2022-11-19T14:28:38Z")

</div>

Are you wanting working code, or hints? Is easier if you show your nearly-working code.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 19, 2022, 2:46pm UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/13 "2022-11-19T14:46:44Z")

</div>

Are you aware of [Expandable text sections](https://swi-prolog.discourse.group/t/expandable-text-sections/1326) for hiding the working code so that the OP can read the hints and then only see the code if they choose.

---

<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:** [November 19, 2022, 4:29pm UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/14 "2022-11-19T16:29:29Z")

</div>

I can picture the scene:

_But Sir, I only looked at the hint and didn’t peek at the provided code, honest. It’s pure coincidence that my own high-quality answer is completely identical including the variable names!_

---

<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:** [November 19, 2022, 6:43pm UTC](https://swi-prolog.discourse.group/t/need-help-with-a-homework/5942/15 "2022-11-19T18:43:09Z")

</div>

It’s not actually using select/3:

> **Code is hiding here, waiting to be clicked...**
>
> ```prolog
> subset_sum(0, _, []).
> subset_sum(Sum, SourceLst, [Elem|SumLst]) :-
> select_after(Elem, SourceLst, After),
> Sum0 is Sum - Elem,
> compare(Comp, Sum0, 0),
> subset_sum_(Comp, Sum0, After, SumLst).
> 
> subset_sum_(=, 0, _, []).
> subset_sum_(>, Sum1, SourceLst, SumLst) :-
> subset_sum(Sum1, SourceLst, SumLst).
> 
> select_after(Elem, [H|T], After) :-
> select_after_(T, H, Elem, After).
>     
> select_after_(T, H, H, T).
> select_after_([H|T], _, E, R) :-
> select_after_(T, H, E, R).
> 
> ```

Results:

```prolog
?- subset_sum(7, [10,7,3,4,2,1], L).
L = [7] ;
L = [3, 4] ;
L = [4, 2, 1] ;
false.

?- subset_sum(0, [10,7,3,4,2,1], L).
L = [] ;
false.

```

Note the tweaked [argument order](https://swi-prolog.discourse.group/t/split-list/4836/8).
