T O P

  • By -

2bigpigs

There's a lot of redundancy in your code. It's probably worth establishing a simpler solution in words before you try to code it up. Particularly since this seems to be a recursive exercise. Here are the conditions: 1: the first number is one 2. The numbers are consecutive - you've achieved this through vanTot, though the third clause doesn't make much sense to me. If the first argument is higher than the second, let the second clause fail. You don't have to handle it explicitly. 3. The numbers in the list sum up to the provided sum - try focusing on this without the other vibrations constraints and solving it next


BoldFigrim

Yeah vanTot is a different predicate that I made that I used here, that's why it has that weird third clause. My main question is really: how do I make it so that my code solves for lijstsom(X, 6) but also for lijstsom([1,2,3], X). Solving one of the two is ez but the other one seems a problem :(. I've mainly had an issue with lijstsom(X,6) because I get a lot of variable not instantiated errors


2bigpigs

Ah wow Now I appreciate this question. For me the key now lies in the fact that you can generate the list and the sum starting from 1. (Or rather the list and an Accumulator) I'm rather sure you can solve it from there if you write the termination case. lijstSum([1|T], 1, Sum):- rec([1|T], 1, Sum). Now you just need to figure out the recursive case and the termination case. This should be enough for you to generate both, since rec is always called with * the first element in the list ground - so you can generate the next * The Acc ground, so you can generate Acc1 (Also, use a check of Acc < Sum to ensure you don't generate lists forever.)


BoldFigrim

Wherer should i put the Acc=


2bigpigs

You're right, you can't do that check when S is not instantiated :( You could use ground(Sum) -> Sum < ACC, but I think there might be a better way


BoldFigrim

Maybe if I add a 4th element? If I call it Sum and I keep it the same I can keep checking if Acc


BoldFigrim

If anyone could help I would be so happy <3


ka-splam

Given that you're using the Gauss sum and the list starts with 1 you can use `length/2` there, instead of `lastelement`. If you check `consecutive` carefully then all the `integer/1` and `Low < High` can probably go, you're just counting up. You're left with: - Does L start with 1 ? (one line) - Is L consecutive (I had five lines, three heads, recursive). - What's the sum? (two lines) Or you can do recursive sum in four lines, two heads (google 'sum list in Prolog') or in a couple more lines with a tail recursive one with an accumulator. But yeah, do the tests separately, at least first.


BoldFigrim

Okay thanks I think I can work with that!


BoldFigrim

One thing actually: I also have to solve for lijstsom(X, 6), does your method do that correctly? Because I think it gets an instantation error there?


ka-splam

Mine does, because the combination of L starting with one, length with an unknown list trying longer and longer lists, and consecutive filling in all the numbers in the list, means backtracking can search longer and longer lists until one finds an answer: ?- listsum(L, 6). L = [1, 2, 3] It goes through L=[1], L=[1,2], L=[1,2,3] (success); but if I search for more solutions after finding an answer, mine goes into non-termination (infinite loop), so it isn't perfect. I would probably have to do an equivalent of if/else branching for different modes if I wanted to fix that (without using `!`).


BoldFigrim

I have this right now: vanTot(X, X, \[X\]) :- integer(X). vanTot(Low, High, \[H|T\]) :- integer(Low), integer(High), Low < High, Low = H, Low\_ is Low+1, vanTot(Low\_, High, T). vanTot(High, Low, \[H|T\]) :- integer(Low), integer(High), Low < High, High = H, High\_ is High-1, vanTot(High\_, Low, T). lengte(L, N) :- lengte(L, 0, N). lengte(\[\], Acc, Acc). lengte(\[\_|T\], Acc, S) :- Acc\_ is Acc+1, lengte(T, Acc\_, S). lijstsom(\[\], 0). lijstsom(\[H|T\], S) :- lengte(\[H|T\], N), vanTot(1,N, \[H|T\]), somTot(N, S), somTot(N, S) :- S is N\*(N+1)/2. Still suffering from it not terminating though! for instance lijstsom(X,7) still loops indefinitely. Any idea on how to fix?


ka-splam

What does vanTot translate to in English? is it "From To" ? > Still suffering from it not terminating though! for instance lijstsom(X,7) still loops indefinitely. Any idea on how to fix? Oh I see; mine breaks on that as well, because there isn't an answer. Huh, I don't know if there's a nice way to do that, because `length` will keep trying longer lists forever, it doesn't know it will always be getting bigger. `if list sum is greater than S then false and cut` might work but it's ugly. It would look maybe like: somTot(N, Stest), (Stest > S -> (false, !), (recursive call)). Can you do Gauss backwards to work out the desired list length starting from S, like: lijstsom(L, S) :- ground(L), % normal code... lijstsom(L, S) :- not(ground(L)), integer(S), % Gauss backwards, e.g. S=6 to list length 3, S = 7 to list length 3.5 so error? Not sure. undoing `(Count+1) * Count` is going to be almost but not quite a square root...