Hiyya,
I'm currently working on a type of "virtual shop" in Prolog, and I'm trying to work on the functionality to update an aisle. However, seeing my rather limited knowledge of Prolog, I'm facing some difficulties.
The predicate(s) and call:
/* % First parameter: The shop (or "old list") that contains the unmodified data
% Second parameter: Simply a index/counter keeping track of where we are in
the "recursive loop"
% Third parameter: Index of where we want to get. We keep recursively looping
and either incrementing or decrementing until the second and third
parameters are the same.
% Fourth parameter: Since the first parameter is a list of lists, this
parameter will contain the "inner list" which will replace another inner
list.
% Fifth parameter: The shop (or "new list"), the result, whatever you'd like
to call it
*/
update_aisle( [H|T], CurrentAisle, TargetAisle, NewObj, NewShop):-
CurrentAisle < TargetAisle,
Temp is CurrentAisle + 1,
append( NewShop, H),
update_aisle( T, Temp, TargetAisle, NewObj, NewShop).
update_aisle( [H|T], CurrentAisle, TargetAisle, NewObj, NewShop):-
CurrentAisle > TargetAisle,
Temp is CurrentAisle - 1,
append( NewShop, H),
update_aisle( T, Temp, TargetAisle, NewObj, NewShop).
update_aisle( [H|T], CurrentAisle, TargetAisle, NewObj, NewShop):-
CurrentAisle is TargetAisle,
nth0( 0, T, TempT),
append( NewObj, TempT, NewShop).
?- Shop = [ [ "Bread", "Donuts", "Cookies" ],
[ "Beer", "Cider", "Juice" ],
[ "Ham", "Raw Meat", "Sausage" ] ],
write(Shop), nl,
update_aisle(Shop, 0, 1, ["Beer", "Milk", "Juice"], NewShop),
write(NewShop), nl.
Right now, I'm only getting directive (failed), so not getting very far at all (and I'm going to assume that's because append/2 is not cooperating as I'd want it to). However, what I'd want to achieve is this:
Shop = [ [ "Bread", "Donuts", "Cookies" ],
[ "Beer", "Cider", "Juice" ],
[ "Ham", "Raw Meat", "Sausage" ] ].
NewShop = [ [ "Bread", "Donuts", "Cookies" ],
[ "Beer", "Milk", "Juice" ],
[ "Ham", "Raw Meat", "Sausage" ] ].
So in other words, I'm trying to loop through the list, and append the "head" of the list for each recursive loop for as long as we're not where we want to be in the list. Once we've reached that point, we're mostly out after appending NewObj to the list (instead of the head, as that is the one being replaced) and job done.
I feel like I'm missing something glaringly obvious, but any and all help would be appreciated!
append/2is definitely not correclty used. Wherever it appears, it will makeHthe result of merging the sublists ofNewShop. That's not what is wanted. Also, ` CurrentAisle is TargetAisle` is wrong. [is/2] is arithmetic evaluation of the right-hand-side. You want to check for equality, right? Then=("make sure left and right unify") or=:=(make sure arithmetic expressions on the left and right rfesolve the same) - David Tonhoferappend/2isappend(+ListOfLists, ?List)? Unless their documentation does not keep orders in mind? - Xariezappend( [[1,2],[3,4],[5,6]] ,X).givesX = [1, 2, 3, 4, 5, 6].backwards:bagof([A,B], append( [A,B] , [1,2] ), S).givesS = [[[], [1, 2]], [[1], [2]], [[1, 2], []]].- David Tonhoferupdate_aisleis supposed to do? What's its purpose? It's not clear to me why it needs all of those arguments. I know what each one means individually based upon their names, but why you need them or want them is unclear. - lurker