Make multiple replacements in a list by index - prolog

I have this program that makes multiple replacements in a list given by
another list with pairs in the format (Index, Elem).
Example:
replace_multiple([A, B, C, D, E], [(2, b), (1, a), (3, c), (4, d)]).
###
should result:
####
Lst = [a, b, c, d, E],
false
####
However this is my output:
A = a,
B = b,
C = c,
D = d
false
####
What am i doing wrong?

What am i doing wrong?
Here are a few things:
You may be unsure about the fact that the list you want to replace contains elements starting with uppercase letters, i.e. logical variables.
In replace_multiple/2, the new element is described as Elem in the head, but as Letter in the body.
In replace_multiple/2, the call to replace/4 has Lst on first and second position. The meaning of a logical variable like Lst is the same as the name of a shared global variable, which, however, cannot be modified unless you replace more logical variables in that logical variable with other stuff. So you have the same structure as "input" and "output". This is probably not what you want.
Same for replace_multiple/2 per se, which should take Lst and build ("return") a modified LstMod given as third argument.
There is fat syntax error at the end of replace/4.
It is better to use [x,y] or x-y than (x,y) as a notation for a pair.

Related

Prolog Sorting List By Rule Position

I am new to Prolog and for the following program:
place(Store,2,a).
place(Store,1,b).
place(Store,3,d).
place(Store,4,c).
placeSort(S,List):- findall(L,place(S,N,L),List).
output: List = [a, b, d, c].
By using placeSort(S,List) , I can find all the elements(a,b,c,d) that contains S (Store).
However what I want to achieve here is to sort the Position of a,b,c,d by using N, however I dont know how to do so as using sort will just sort it out by alphabetical order
placeSort(S,NewList):- findall(L,place(S,N,L),List),sort(List,NewList).
output: List = [a, b, c, d].
what I want to achieve : List = [b,a,d,c]
**I know by using placeSort(S,NewList):- findall([N,L],place(S,N,L),List),sort(List,NewList).
it will return a list of lists sorted by numbers.
output : List = [[1, b], [2, a], [3, d], [4, c]].
but im not sure how to take away the numbers and just take the alphabets instead.
Any help would be greatly appreciated.
SWI-Prolog offers the interesting builtin order_by/2, filling the gap traditional Prolog suffers when compared to SQL, with library(solutionsequences):
?- order_by([asc(X)],place(P,X,W)).
X = 1,
W = b ;
X = 2,
W = a ;
...
So you can avoid full list construction.
The easiest way to do this is to use setof/3 (which sorts by term) and pick a term form that works for you on your sort. In this case, you can collect terms of the form N-X where they satisfy, place(_, N, X):
setof(N-X, place(S,N,X), OrderedList). % Assuming `S` is bound
This will result in:
OrderedList = [1-b, 2-a, 3-d, 4-c]
Then you can use maplist/3 to get your list by defining a simple mapping:
foo(_-X, X).
maplist(foo, OrderedList, List).
This will then give you just the elements you want.
Your complete predicate would look like:
foo(_-X, X).
placeSort(S, List) :-
setof(N-X, place(S,N,X), OrderedList),
maplist(foo, OrderedList, List).
Obviously, you'd choose sensible names for your facts, predicates, and variables. My names (foo, List, OrderedList, S, N, X) are not sufficient, in my opinion, for an application but I am not familiar with your actual problem domain, so this is just for illustration purposes.
As an aside, note that in your facts Store is a variable, so that's not particularly meaningful in the facts. I kept your use of S in your predicate, but it's unclear to me how you really intend to use it.

Sorting tuples based on elements in Erlang

Having read current answer THIS I did the following:
A = {1, 99}, B = {5, 15}.
F = fun({key_X, val_X},{key_Y, val_Y}) ->
{val_X, key_X} =< {val_Y, key_Y}
end.
And then, put it to lists:sort/2 function.
As follows:
lists:sort(F, [A, B]).
But got the error exception :
exception error: no function clause matching erl_eval:'-inside-an-interpreted-fun-'({1,99},{5,15})
What is a mistake here? Can you guide me through?
You have to note that Erlang differentiates atoms and identifiers using their case.
Eg:
[a, b, bla, key_1, val_X] is a list of atoms
[A, B, Bla, Key_1, Val_X] is a list of variables
In your code you defined F so that it behaves a certain way for specific atoms as input.
What you should have done (and what they did in your link) is use variable identifiers:
F = fun({Key_X, Val_X},{Key_Y, Val_Y}) -> {Val_X, Key_X} =< {Val_Y, Key_Y} end.
See?

Take elements from one list and append to other

I want a function that will take two lists A and B and return lists Aout and Bout, such that elements from the beginning of A up to a given element (say the atom 'a') have been removed and appended to the end of B, discarding the character. My attempt below:
% usage: take_while(A, Aout, B, Bout)
take_while([], [], B, B).
take_while(['a'|As], As, B, B).
take_while([A|As], As, B, Bout) :-
append(B, [A], Bout),
%take_while(???
The last clause might be the wrong approach. How do I do this?
Looks like you need to simply add the call to take_while to that last clause: (Actually, I am not sure why the second parameter is needed so I'm removing it from this answer).
take_while([], [], B, B).
take_while(['a'|As], As, B, B).
take_while([A|As], ARem, B, Bout) :-
append(B, [A], BTemp), take_while(As, ARem, BTemp, Bout).

Prolog - get the first list from a list of lists

I've got a list consisting of smaller lists inside of it, each list consisting of 2 items:
[[a,1],[b,2],[c,3]]
I'm using a function called take(1,L,R) to take the first item from list L and return the item R. The code for the take function is here:
take(0,X,X).
take(N,[H|T],[H|R]):-
N>0, M is N-1,
take(M,T,R).
At the moment a run may look like this:
1 ?- take(1,[[a],[b],[c]],Taken).
Taken = [[a], [b], [c]]
Which is the same as the input! This is the same for a "regular" 1-level-depth list:
2 ?- take(1,[a,b,c],Taken).
Taken = [a, b, c]
Question:
The question for you is how can I make the result look like:
1 ?- take(1,[[a],[b],[c]],Taken).
Taken = [a]
I want to return the first N items of the list I send it.
Your base case take(0, X, X). is doing exactly what it says -- given any value X, the result is X. What I think you were trying to say is take(1, [H|T], H). (which yields the first element of a list).
What I think you're actually after is take(0, _, [ ]). which yields an empty list when "taking" 0 items from any list. This works well with your existing recursive case.
You say that you want to get the "first N items of the list" -- such a result must be stored in a list of N items. It follows that take(1, [a, b, c], Taken) would yield Taken = [a], not Taken = a. Similarly, take(1, [[a], [b], [c]], Taken). would yield Taken = [[a]].. To special-case the take(1, ...) form to return the first item only (without it being wrapped in a list) would break your recursion.

Prolog - return value from base case

Ok, here's the deal:
I've got two piles of shirts
I want to take a random shirt from each pile and put them in a new pile
Then get the new pile out
And here is the code:
mix([],[],_).
mix(P1,P2, Pile):-
takeshirt(P1,1,Taken1,Rem1), takeshirt(P2,1,Taken2,Rem2), #Take one
append(Pile,Taken1,New), append(New,Taken2,NewPile), #Put both of them
mix(Remain1,Remain2,NewPile).
This is what the result look like:
1 ?- mix([a,b],[c,d],NewPile).
NewPile = [] .
I want it to look like:
1 ?- mix([a,b],[c,d],NewPile).
NewPile = [b, d, a, c] .
Or whatever the result is. I checked via the graphical debugger and found out that when the final call to mix happens, the bindings are:
P1 = Taken1 = [b]
P2 = Taken2 = [c]
Pile = [a, d]
Rem1 = Rem2 = []
New = [a, d, b]
NewPile = [a, d, b, c] #<--- Interresting
So the wanted value is in NewPile when the final call to:
mix([],[],_).
happens. After this is it collapses like a house of cards.
So the question is:
mix([],[],_).
I'd like to return the _ value from the base case, this rule mix is actually used from a higher instance where I send in two piles and get the new pile out.
Update:
To clarify some comments about the takeshirt rule, here it is:
takeshirt(_,0,[],_).
takeshirt(List,Number,[Element|Taken],Remain) :- N > 0,
length(List,Len),
Index is random(Len) + 1,
removeshirt_at(Element,List,Index,Remain),
Number1 is Number - 1,
takeshirt(Remain,Number1,Taken,Remain).
Consider the following modifications to your code:
mix([], [], []) :- !.
mix(P1, P2, Pile) :-
takeshirt(P1, 1, Taken1, Rem1),
takeshirt(P2, 1, Taken2, Rem2),
append(Taken1, Taken2, Pile0),
mix(Rem1, Rem2, Pile1),
append(Pile0, Pile1, Pile).
It seems you need to accumulate the 'shirts' (as list atoms). Here, we are recursively appending them onto the third argument of mix/3 (Pile), until the base case (the first clause) is hit when both input lists are empty lists (note that the cut ! is necessary here as the binding pattern for the second clause matches the first, so we want to exclude it). The behaviour of the second clause, which takes a shirt from each input list for every step, requires that they must have been of equal length to start with.
To test this, I used the following definition of takeshirt/4:
takeshirt(Ps, _, [P], Rem) :-
select(P, Ps, Rem).
Note that the second argument here is unused, as select/3 is being used to take a single element (a shirt) from the list, and return the remainder. The absence of a cut (!) after the select allows this predicate to backtrack in selecting all other elements (shirts) from the list. If we now execute your example query with this definition, we can get:
1 ?- mix([a,b],[c,d],NewPile).
NewPile = [a, c, b, d] ;
NewPile = [a, d, b, c] ;
NewPile = [b, c, a, d] ;
NewPile = [b, d, a, c] ;
false.
...we can see that mix/3 enumerates all possible 'piles' on backtracking by taking a shirt from the first pile (first input list), then a shirt from the second pile (second input list), and so on, until both input lists are empty. If your definition of takeshirt/4 doesn't leave choice-points, (is non-backtracking), then you could only get one solution, if any.

Resources