How to double everything in a Prolog list (including sublists) - prolog

I have a Prolog function that must take in a list (which may include sublists) and return a list that has everything doubled.
For example,
?- doubleAll([1,2,[3,4],5,[6,7,[8],9],10), L).
L = [2,4,[6,8],10,[12,14,[16],18],20).
I was able to double everything when there were no sublists in the list, but when sublists are included I'm having some problems. Here is my code so far:
%L2 is the result of doubling everything in L1.
doubleEverything([],[]).
doubleEverything([H|T], [H2|T2]) :-
atomic(H), H2 is H*2, doubleEverything(T,T2).
doubleEverything([H|T], [H2|T2]) :-
not(atomic(H)), H2 is H*2, doubleEverything(T,T2), doubleEverything(H,T2).
Does anyone see how I can make this work?
Thanks!

Something like this would work:
double( [] , [] ) . % doubling the empty list is...the empty list.
double( [X|Xs] , [XX|XXs] ) :- % if the head of the list is a list,
is_list(X) , % we double it, then double the tail.
! ,
double(X,XX) ,
double(Xs,XXs)
.
double( [X|Xs] , [XX XXs] ) :- % doubling anything else is easy.
XX is X*2 ,
double(Xs,XXs)
.
is_list( X ) :- var(X) , ! , fail .
is_list( [] ) .
is_list( [_|_] ) .

not(atomic(H)), H2 is H*2, doesn't make sense.
Since atomic(H) (should be number(H), really) is already handled, simply recurse:
doubleEverything([H|T], [H2|T2]) :-
doubleEverything(H,H2), doubleEverything(T,T2).
Keep it simple, but remember to test what will happen if neither a number or a list is input...

Related

implement a prolog predicate that removes all duplicate elements

Implement a Prolog Predicate that removes all duplicate elements from a list given in the first argument and returns the result in the second argument position.
Consider the possibilities. There's just a few cases:
The source list is the empty list ([]). That is a set (∅, the empty set) by definition. That give us this:
list_set( [] , [] ) .
The source list is not empty, and the head of the list is duplicated in the tail of the list. It this case, since the head can be found elsewhere in the list, it's a duplicate and can be discarded. That gives us this:
list_set( [X|Xs] , Ys ) :-
member(X,Xs),
list_set(Xs,Ys)
.
Finally, the source list is not empty, and the head of the list is *not duplicated in the tail of the list. In this case, since the head is unique, we can add the head to the result set. That gives us this:
list_set( [X|Xs] , [X|Ys] ) :-
\+ member(X,Xs),
list_set(Xs,Ys)
.
That's all there is to it.
Putting it all together you get:
list_set( [] , [] ) .
list_set( [X|Xs] , Ys ) :- member(X,Xs), list_set(Xs,Ys) .
list_set( [X|Xs] , [X|Ys] ) :- \+ member(X,Xs), list_set(Xs,Ys) .
You might notice the two tests using member/2. That can be optimized away by introducing a cut (!/0) to eliminate the choice point between clauses 2 and 3 of the above predicate. Having introduced that, we can eliminate the second member/2, because the only way to get to the third clause is if X is unique:
list_set( [] , [] ) .
list_set( [X|Xs] , Ys ) :- member(X,Xs), !, list_set(Xs,Ys) .
list_set( [X|Xs] , [X|Ys] ) :- list_set(Xs,Ys) .

Find substrings of certain length

I'm a beginner at prolog and I'm having trouble getting started with the following problem:
Define the predicate partstr/3, where the first argument is a list, that generates a list A of length L that you find consecutive in the first list.
You should be able to present all answers with backtracking.
E.g.:
?- partstr([1, 2 , 3], L, A).
If L = 2 then A = [1,2] and [2,3],
or if L = 2 then F=[1,2] and [2,3].
and so on...
I feel like you would use recursion to solve it, but I'm not sure where to start. I would really appreciate some tips on how to solve this because I feel like I'm getting nowhere.
The core of this problem is that you need a way to pull all the sublist of length N from a list, correct?
So...
Consider that append/3 can concatenate two lists: append( [a,b,c], [1,2,3], L) returns L as [a,b,c,1,2,3]. But it can also decompose a list into a prefix and a suffix, so
append( Pfx, Sfx, [a,b,c])
will, on backtracking, successively yield:
Pfx
Sfx
[]
[a,b,c]
[a]
[b,c]
[a,b]
[c]
[a,b,c]
[]
...and... length/2 can not only tell you the length of a list, but
can generate lists of a specified length populated with unique,
unbound variables, so length(L,3) returns [V1,V2,V3].
You can combine those to get the behavior you want:
partstr( Xs, N, SL ) :- % To get all the contiguous sublists of length N from Xs...
append(_,Sfx,Xs) , % - repeatedly get all possible suffixes of Xs, and...
length(SL,N) , % - construct an empty, unbound list of the desired length (N), and...
append(SL,_,Sfx) % - pull that prefix off the suffix
. % Easy!
That's one approach. I imagine that this is coursework and that your instructor likely would like you to roll your own solution from scratch.
To do that, we first need a predicate that will yield the source list, and on backtracking remove the head of the list. Something like:
suffix( Xs , Xs ) .
suffix( [_|Xs] , Sfx ) :- suffix(Xs,Sfx).
Then we need a way to grab the 1st n elements from a list, something like this:
take( _ , 0 , [] ) :- ! .
take( [X|Xs] , N , [X|Sfx] ) :- N1 is N-1 , take(Xs,N1,Sfx) .
Given those two...
partstr( Xs, N , SL ) :-
suffix(Xs,Sfx),
take(Sfx,N, SL )
.
You can even dispense with the suffix/2 predicate, thus, rolling its functionality into partstr/3 itself:
partstr( Xs , N , SL ) :- take(Xs,N,SL).
partstr( [_|Xs] , N , SL ) :- partstr(Xs,N,SL).
And that, I think, is the sweet spot: it is hard to beat 4 lines of code —
partstr( Xs , N , SL ) :- take(Xs,N,SL) .
partstr( [_|Xs] , N , SL ) :- partstr(Xs,N,SL) .
take( _ , 0 , [] ) :- ! .
take( [X|Xs] , N , [X|Sfx] ) :- N > 0 , N1 is N-1 , take(Xs,N1,Sfx) .\

Prolog Split list into list of lists

i want to make one list split into list of lists, and skip the 'solid' and separate the list into sublist.
the input and output is below
split_s([A,B,C,solid,D,E,F],X).
X = [[A,B,C],[D,E,F]].
Can anyone help me?
Try something along the following lines. It helps if you decompose your problem. In this case, the heart of the problem is this:
find the longest prefix of a list that doesn't contain the atom solid.
Which you can do like this with a predicate like take_until( List , Separator , Prefix , Remainder ):
take_until( [] , _ , [] , [] ) . % if we hit the end of the source list, we're done.
take_until( [X|Xs] , X , [] , Xs ) . % if we hit the separator, we're done
take_until( [X|Xs] , S , [X|Ps] , Rs ) :- % otherwise...
X \= S , % - when the head of the list is NOT the separator
take_until( Xs , S , Ps , Rs ) % - we take it on to the sublist and keep going.
. %
Once you have that down, the rest is easy:
use the above predicate to extract the first such prefix, then
recurse down on what's left over.
Like this:
split( [] , [] ) . % splitting the empty list results in the empty list.
split( [X|Xs] , [Y|Ys] ) :- % splitting a non-empty list...
take_until( [X|Xs] , solid , Y , R ) , % - get the desired prefix
split(R, , Ys ) % - recurse down on what's left
. % Easy!
The following works for me:
split_s([],[[]]).
split_s([H|T],[[H|XH]|XR]) :- var(H),!,split_s(T,[XH|XR]).
split_s([solid|T],[[]|X]) :- !,split_s(T,X).
split_s([H|T],[[H|XH]|XR]) :- split_s(T,[XH|XR]).
EDIT: moved the cut in the 3rd clause in front of the split.
If you don't want empty lists, then try the following:
split_s([],[]).
split_s([H|T],[[H|XT]|XXT]) :- var(H),!,split_s([[H]|T],[[[H]|XT]|XXT]).
split_s([solid|T],X) :- !,split_s(T,X).
split_s([H],[[H]]) :- !.
split_s([H,M|T],[[H,M|XT]|XXT]) :- var(M),!,split_s([[M]|T],[[[M]|XT]|XXT]).
split_s([H,solid|T],[[H]|XT]) :- !,split_s(T,XT).
split_s([H|T],[[H|XH]|XR]) :- split_s(T,[XH|XR]).

Sum and square root list elements

So I am currently trying to compute a formula using Prolog. I currently have part of the formula done, but I am having trouble implementing the next part where I need to add the elements of the list and then square root the sum. Not sure how I would do that.
What I currently have:
formula([], [], []).
formula([], [H2|T2], [L|Ls]) :-
L = H2,
formula([], T2, Ls).
formula([H1|T1], [], [L|Ls]) :-
L = H1,
formula(T1, [], Ls).
formula([H1|T1], [H2|T2], [L|Ls]) :-
L is (H1 - H2)*(H1 - H2),
formula(T1, T2, Ls).
Your original formula
formula([], [], []).
formula([], [H2|T2], [L|Ls]) :-
L = H2,
formula([], T2, Ls).
formula([H1|T1], [], [L|Ls]) :-
L = H1,
formula(T1, [], Ls).
formula([H1|T1], [H2|T2], [L|Ls]) :-
L is (H1 - H2)*(H1 - H2),
formula(T1, T2, Ls).
can be simplified to make the pattern matching more explicit:
formula( [] , [] , [] ) .
formula( [] , [Y|Ys] , [Y|Zs] ) :- formula( [] , Ys , Zs ) .
formula( [X|Xs] , [] , [X|Zs] ) :- formula( Xs , [] , Zs ) .
formula( [X|Xs] , [Y|Ys] , [Z|Zs] ) :-
L is ( X - Y ) * ( X - Y ) ,
formula(Xs,Ys,Zs)
.
I assume your instructor wants you to roll your own here and learn about recursion rather than using a built-in predicate. So, ... You could sum the elements of a list like this (the naive implementation):
sum_of( [] , 0 ) . % the sum of the empty list is zero.
sum_of( [X|Xs] , S ) :- % the sum of an empty list is computed by
sum(Xs,T) , % - computing the sum of the tail of the list
S is T+X % - and adding that to the value of the head of the list.
. %
But that will fail with a stack overflow once the list gets sufficiently long as each recursive call pushes a new frame onto the stack. Prolog has an nifty optimization (tail recursion optimization) that effectively converts recursion into iteration by recognizing when it can reuse the stack frame. To do that, the recurive call must be the very last thing done.
This introduces a common pattern in prolog programming:
a public interface predicate (here, sum_of/2),
that invokes a "private" tail-recursize worker predicate (here, sum_of/3) that uses an accumulator argument to build up its result.
Using that pattern, we get this implementation:
sum_of(Xs,Sum) :- sum_of(Xs,0,Sum) .
sum_of( [] , S , S ) . % the sum of the empty list is 0.
sum_of( [X|Xs] , T , S ) :- % the sum of a non-empty list is computed by
T1 is T+X , % incrementing the accumulator by the value of the head of the list, and
sum_of( Xs , T1 , S ) % recursing down on the tail.
. % Easy!
This will work for lists of any length.
Using the SWI-Prolog library predicate sum_list/2:
list_summed_and_square_rooted(List, Value) :-
sum_list(List, Sum),
Value is sqrt(Sum).
You probably don't need to write a separate predicate for relating a list to the square root of the sum of its elements, unless you'll be needing to use that particular relation often. Your formula/3 makes one list out of two, but ultimately you seem to be after a numerical value, so you probably do want another predicate to describe the relation between two lists and the resultant numerical value.
lists_processed_in_some_way(List1, List2, Value) :-
formula(List1, List2, CombinedList),
sum_list(CombinedList, Sum),
Value is sqrt(Sum).
By the way, you can simplify your formula/3 because you don't need L = H2:
formula([], [H2|T2], [H2|Ls]) :-
formula([], T2, Ls).
Also, it's generally good practice to name your predicates carefully, with something descriptive. It will help you reason about what your predicates do and help you communicate your programs to others.

SICStus Prolog Lists

Having trouble understanding how Prolog works. I'm tryig to write a rule that takes three lists of integers as input (representing sets) and puts the integers that belong to both the first and second list in the third list.
Example:
?-inter([10,20,30,40],[10,50,40,60], List3 )
List3 = [10, 40]
So far I have this, that can recognize if a list contains a certain letter:
mymember(X,[X|T]).
mymember(X,[H|T]) :- mymember(X,T).
There's actually an inbuilt library to sort that all out for you, known as ordsets.
inter(X, Y, Z) :-
list_to_ord_set(X, L1),
list_to_ord_set(Y, L2),
ord_intersection(L1, L2, Z).
Using your example input you get the following
| ?- inter([10,20,30,40],[10,50,40,60],X).
X = [10,40] ? ;
no
inter(Xs, Ys, Zs) will be true when each element in Zs also is in Xs and in Ys.
But Zs are unknown, then a more constructive approach is required.
Here it is: iterate on Xs and store in Zs each element that is in Ys.
An example of iteration is mymember/2, you can see that it requires a recursive predicate.
The other idiomatic part of the above statement is store in Zs, Prolog has a peculiar way to do such things, using pattern matching.
inter([X|Xs], Ys, [X|Zs]) :-
mymember(X, Ys), inter(Xs, Ys, Zs).
You will need to complete inter/3 with other 2 clauses: base recursion, i.e. when all Xs elements have been processed, and the case where X is not a member of Ys.
Try something like this, using the builtins member/2 and setof\3:
set_intersection( As , Bs , Xs ) :-
set_of( X , ( member(X,As) , member(X,Bs) ) , Xs )
.
One should note that this will fail if the lists As and Bs have no elements in common. An alternative would be use findall/3 rather than set_of/3. findall/3 will hand back and empty list rather than failure if the goal is not satisfied:
set_intersection( As , Bs , Xs ) :-
findall( X , ( member(X,As) , member(X,Bs) ) , Xs )
.
However findall/3 returns a bag (duplicates are allowed) rather than a set (no duplicates allowed), so if your two source lists aren't sets, you won't get a set out.
member/2 is a builtin predicate that unifies its first argument with an element of the list — the equivalent of
member(X,[X|_).
member(X,[_|Xs) :- member(X,Xs) .
And, finally, as #chac noted in his answer, you can recursively traverse the list.
set_intersection( [] , _ , [] ) . % the intersection of the empty set with anything is the empty set.
set_intersection( [A|As] , Bs , [A|Xs] ) :- % if the list is non-empty,
member(A,Bs) , % - and A is a member of the 2nd set
! , % - we cut off alternatives at this point (deterministic)
set_intersection( As , Bs , Xs ) % - and recurse down on the tail of the list.
.
set_intersection( [_|As] , Bs , Xs ) :- % if the list is non-empty, and A is NOT a embmer of the 2nd set
set_intersection( As , Bs , Xs ) % we just recurse down on the tail of the list.
.
#chac's technique builds the result list as he goes, something like:
[a|X]
[a,b|X]
[a,b,c|X]
The final unification, the special case of the empty list unifies the unbound tail of the list with [] making the list complete, so the final [a,b,c|X] becomes
[a,b,c]
A little prolog magic. An alternative that might be easier to understand is to use a worker predicate with an accumulator:
%
% set_intersection/3: the public interface predicate
%
set_intersection( As , Bs , Xs ) :-
set_intersection( As , Bc , [] , T ) % we seed our accumulator with the empty list here
.
%
% set_intersection/4: the private worker bee predicate
%
set_intersection( [] , _ , T , Xs ) :- % since our accumulator is essentially a stack
reverse(T,Xs) % we need to reverse the accumulator to
. % put things in the expected sequence
set_intersection( [A|As] , Bs , T , Xs ) :-
member( A, Bs ) ,
! ,
T1 = [A|T] ,
set_intersection( As , Bs , T1 , Xs )
.
set_intersection( [_|As] , Bs , T , Xs ) :-
set_intersection( As , Bs , T , Xs )
.

Resources