Related
I have Erlang function that returns the last element of a list
lastElement([H|[]]) ->H;
lastElement([H|T]) ->lastElement(T).
and a function that returns a list without last element
withoutLastElement([H|[]], Result) ->Result;
withoutLastElement([H|T]) ->withoutLastElement(T, [H|Result]).
so i browse the same list for two times, and for more efficiency i want to do only a function that returns the last element of a list and returns this list without this element in one browse, this is easy in another language but i can't do that in Erlang so any help and thank you all.
You'd usually return the two values in a tuple:
list_and_last(List) ->
list_and_last(List, []).
list_and_last([H], Result) ->
{H, Result};
list_and_last([H|T], Result) ->
list_and_last(T, [H|Result]).
Calling list_and_last with [1,2,3] returns {3,[2,1]}.
In Erlang you can return a tuple, so you simply define a tuple with the last element and the list:
... -> {last_element, list}
You retrieve the value with pattern matching:
{Element, List} = last_element_and_new_list(The_List)
The last element and list will be bound to the respective variables. Note that you can return more than two values in a tuple.
You can simply reverse the list:
List = [1, 2, 3, 4, 5, 6],
[Last | Rest] = lists:reverse(List)
I'm having difficulty with my assignment, I have to write 2 predicates:
remove_all/3 which removes all instances of a given element out of a list and output the list without the given variable, e.g.:
remove_all(a, [b,a,c,a,d], X)
gives
X = [b,c,d])
and
remove_list/3 which removes all elements of a given list out of another list and outputs the resulting list, e.g.:
remove_list([a,b], [b,a,c,a,d], X)
gives
X=[c,d].
This is what I have:
remove_all(Rema,[],[]).
remove_all(Rema,[Rema|X],Res) :-
remove_all(Rema,X,Res).
remove_all(Rema,[L|P],Res) :-
remove_all(Rema,P,NR), Res=[L|NR].
remove_list([],ListB, ListRes).
remove_list([H|Taila],ListB,ListRes) :-
member(H,ListB),
remove_all(H,ListB,ListRes),
remove_list(Taila,ListRes,ListRes) .
remove_list([S|Tailb],ListB,ListRes) :-
remove_list(Tailb, ListB, ListRes).
Now, my remove_all works fine, but not when I use it in my remove_list predicate, it will then only remove all the instances of the first element of the list which specifies which element are to be removed, for example:
?- remove_list([1,2],[1,2,3,2,1],F).
F = [2, 3, 2]
It only removes the 1's.
Anybody have any idea what to do?
I have some trouble when im trying to create a list with multiple sublists. I fail to see why my code is not working as expected.
solves(_, _, 0, _).
solves(Length, List, Stopcond, NList):- length(NL, Length),
append([NL], List, NList),
write(NList), nl,
N is Stopcond-1,
solves(Length, NList, N, NList2).
?- solves(3, [], 3, B).
I want B to be a list with three sublists, each with three intern variables.
When I write Nlist, it shows:
1. [[_G3207,_G3210,_G3213]]
2. [[_G3222,_G3225,_G3228],[_G3207,_G3210,_G3213]]
3. [[_G3237,_G3240,_G3243],[_G3222,_G3225,_G3228],[_G3207,_G3210,_G3213]]
4. B = [[_G3207, _G3210, _G3213]] .
But I don't understand why B becomes only the last element of the list. I want it to be the list on line 3. Can anyone see why and what i'm doing wrong? I'm pretty new to prolog.
I think this is what you intended
solves(_, List, 0, List).
solves(Length, List, Stopcond, NList2):- length(NL, Length),
append([NL], List, NList),
%write(NList), nl,
N is Stopcond-1,
solves(Length, NList, N, NList2).
What does your predicate try to do ( Ignoring the 3 element sublists for now )?
It appends a list of length Stopcond to the 2nd argument passed (List).
How do you recursively construct a list of length TL Given a list of length L?
You append a list of Length L with a list of Length 1 and try to construct a list of length TL-1 from our new list of length L+1.
As a base case, You know that to construct a list of length TL from a list of length TL, you just need to return the list as is.
Your predicate returns a bigger-list consisting of Stopcond smaller-lists of length Length each.
Recursively, A bigger-list of length Stopcond is a smaller-list of Length elements appended to a bigger-list of length Stopcond-1.
Base case: A bigger-list of size 0 is simply an empty list.
If we were just to consider 0's instead of the 3 element sublists,
solves(FinalList, 0, FinalList). % The result of appending a list of length 0 to the given list is the given list itself.
solves(List, StopCond, FinalList):- % Appends A list of length StopCond to List
Length1List = [0], % Create a list of length 1
append( Length1List, List, LengthPlus1List),
N is N-1,
solves( LengthPlus1List, N, FinalList).
Since you're new, It might help to write english comments against each line.
% Base case: stopping condition satisfied. List is the final List we need
solves(_, List, 0, List).
% Recursive case: We need to make one more list of length L and append/prepend it to List
solves(Length, List, Stopcond, NList2):-
length(NL, Length), % Create smaller-list of length Length
append([NL], List, NList), % Append it to the list we're building up
%write(NList), nl,
N is Stopcond-1,
solves(Length, NList, N, NList2). % Build Stopcond-1 more lists
I am trying to get my head wrap around lists in Prolog. To do this I am trying to create a sort of game. You pass in a list of numbers 1-9 that can be repeated, the list can be any length. The rules are that starting from the first element(e) you can only move to e+2 or e+3 until you get to the end. The goal is to "land" on the highest numbers. In essence it is kind of like hopscotch. The problem I am running into is determining all the possible permutation for paths. So far I have the following.
paths([], []). %empty list returns empty list
paths([X], [X]). %list with one element returns that one element
paths([X1, X2], [X1]). %list with 2 elements returns the first element
paths([X1, X2, X3], [X1,X3]). %list with three elements returns the first and third element
paths() :- % the recursive case for a list with 4+ elements
An list to use would be: [1,2,3,4,5,6,8,7,9,3,6,5,7,8,9]
I need to determine all possible paths using the rule mentioned about. I wish lists could be indexed in Prolog :(
Any logic guidance would be appreciated.
The requirements aren't completely clear, but it seems that:
The second argument is required to have the same first element as the
first argument (you "hop" on the first "square" first always, using
your hopscotch metaphore)
You aren't requiring that the last element of the first list be the
last element of the second list (you aren't requiring that you "land
on" the last "square").
An empty list succeeds with an empty list result (rather than just failing on an empty list - which is another valid approach).
Then this could be implemented as follows. You do not need many explicit 2- and 3-element list cases since they are handled by the recursive clause and simpler base cases.
path([], []).
path([X], [X]).
path([X,_|T], [X|R]) :- % hop over 1 element
path(T, R).
path([X,_,_|T], [X|R]) :- % hop over 2 elements
path(T, R).
For a simple example:
| ?- path([1,2,3,4,5,6], R).
R = [1,3,5] ? ;
R = [1,3,6] ? ;
R = [1,4,6] ? ;
R = [1,4]
yes
If I don't have your requirements exactly right, you should be able to adjust this to suit your needs as it shows how to handle a recursive case. It also sounds like you are headed in the direction of trying to optimize the values in your hops, which I shall also leave as an exercise.
This can also be done with a DCG (definite clause grammar)
path([]) --> [].
path([X]) --> [X].
path([X|T]) --> ([X,_] | [X,_,_]), path(T).
Which would be exercised:
| ?- phrase(path(R), [1,2,3,4,5,6]).
R = [1,3,5] ? ;
R = [1,3,6] ? ;
R = [1,4,6] ? ;
R = [1,4] ? ;
(1 ms) no
| ?-
In light of the extra requirement that the last step taken must be one that falls within the list, here is an updated version of the path/2 predicate:
path([], []).
path([X], [X]).
path([X,_], [X]).
path([X,_,Y|T], [X|R]) :- % hop over 1 element
path([Y|T], R).
path([X,_,_,Y|T], [X|R]) :- % hop over 2 elements
path([Y|T], R).
I think that there is a reason to avoid indexing: simplicity. If you decompose your problem, maybe you could start writing a step/3 predicate like
step([_,X|T],X,T).
step([_,_,X|T],X,T).
and then
paths([],[]).
paths(L,[X|Xs]) :- step(L,X,T), paths(T,Xs).
note: I don't understand very well your game, some example of playground and solution would be welcome.
%passing in a list and return all possible paths using K+2 or K+3 with K being the first element of the list.
%empty list returns empty list
%list with one element returns that one element
%list with 2 elements returns the first element
%list with three elements returns the first and third element
%list with four/four+ elements needs to be called recursively, prefix them with the first element and append them together
%RL means ReturnList
%FL means FinalList
%List is the appended list containing all the paths
paths([], []).
paths([X], [[X]]).
paths([X1, X2], [[X1]]).
paths([X1, X2, X3], [[X1,X3]]).
paths([X1, X2, X3, X4 | T], List) :-
paths([X3,X4|T], RL), paths([X4|T], RL2),
prefix_all(X1, RL, FL1), prefix_all(X1, RL2, FL2),
append(FL1, FL2, List).
So if run with the list [1,2,3,4,5] is would produce the following:
| ?- paths([1,2,3,4,5],X).
X = [[1,3,5],[1,4]] ? ;
no
I'm trying to create a predicate in Prolog that takes a list and returns only one copy of the adjacent duplicates of the list.
for example:
?- adj_dups([a,b,a,a,a,c,c],R).
R=[a,c]
I think I need two base cases:
adj_dups([],[]). % if list is empty, return empty list
adj_dups([X],[]). % if list contains only one element, return empty list (no duplicates).
then for the recursive part, I need to compare the head with the head of the tail, and then go recursively on the tail of the list.
This is what I came up with so far, but it doesn't work!
adj_dups([X,X|T],[X|R]):- adj_dups([X|T],R). % if the list starts with duplicates
adj_dups([X,Y|T],R):- X \= Y, adj_dups([X|T],R). % if the list doesn't start wih duplicates
How can I fix it so I can get the right result?
Edit:
I'll list some examples to help you all understand my problem.
How the code supposed to behave:
?- adj_dups([a,c,c,c,b],R).
R = [c]
?- adj_dups([a,b,b,a,a],R).
R = [b,a]
?- adj_dups([a,b,b,a],R).
R = [b]
How my code is behaving:
?- adj_dups([a,c,c,c,b],R).
R = []
?- adj_dups([a,b,b,a,a],R).
R = [a,a]
?- adj_dups([a,b,b,a],R).
R = [a]
Thank you.
I find ambiguous this specification
only one copy of the adjacent duplicates of the list
as it doesn't clarify what happens when we have multiple occurrences of the same duplicate symbol.
adj_dups([],[]).
adj_dups([X,X|T],[X|R]) :-
skip(X,T,S),
adj_dups(S,R),
\+ memberchk(X,R),
!.
adj_dups([_|T],R) :- adj_dups(T,R).
skip(X,[X|T],S) :- !, skip(X,T,S).
skip(_,T,T).
This yields
?- adj_dups([a,a,c,c,a,a],R).
R = [c, a].
Comment the + memberchk line to get instead
?- adj_dups([a,a,c,c,a,a],R).
R = [a, c, a].
Let's look at what happens when you try a simpler case:
adj_dups([a,b,b,a],R).
The first three predicates don't match, so we get:
adj_dups([X,Y|T],R):- X \= Y, adj_dups([X|T],R).
This is the problematic case: X is bound to a, Y is bound to b.
It will then call adj_dups([a,b,a],R), binding T to [b,a], which only has a single b. Effectively, you have now removed the duplicate b from the list before it could be processed.
Let's create a few auxiliary predicates first - especially a predicate that filters an element from a list. Then, in the recursive part, there are two cases:
If the first element occurs in the tail of the list being processed, it is duplicated; we need to return that first element followed by the processed tail. Processing the tail consists of removing that first element from it, then check the tail for duplicates.
The second case is much simpler: if the first element does not occur in the tail, we simply apply adj_dups to the tail and return that. The first element was never duplicated, so we forget about it.
% Filter the element X from a list.
filter(X,[],[]).
filter(X,[X|T],R) :- filter(X,T,R).
filter(X,[Y|T],[Y|R]) :- X \= Y, filter(X,T,R).
% Return "true" if H is NOT a member of the list.
not_member(H,[]).
not_member(H,[H|T]):-fail.
not_member(H,[Y|T]):-H \= Y, not_member(H,T).
% Base cases for finding duplicated values.
adj_dups([],[]). % if list is empty, return empty list
adj_dups([X],[]). % if list contains only one element, return empty list (no duplicates).
% if H1 is in T1 then return the H1 followed by the adj_dups of (T1 minus H1).
% if H1 is not in T1 then return the adj_dups of T1.
adj_dups([H1|T1],[H1|T3]):-member(H1,T1), filter(H1,T1,T2), adj_dups(T2,T3).
adj_dups([H1|T1],T3):-not_member(H1, T1), adj_dups(T1,T3).
This gives R=[a,b] for the input [a,b,b,a], and R=[a,c] for your example input [a,b,a,a,a,c,c].