The Redis documentation on transactions gives an example of how to implement ZPOP. How do I implement ZMOVE for Redis sorted sets (analagous to SMOVE)?
Is this right? .. to move an element, ele, from sorted set z1 to z2 and give it SCORE 1, i.e.:
ZMOVE z1 z2 1 ele
WATCH z1 z2
MULTI
ZREM z1 ele
ZADD z2 1 ele
EXEC
I will set the SCORE to be the current time, but I just used 1 here for simplicity.
Related
A logic circuit is given two 2-bit binary numbers A and Bas its inputs. The circuit consists of two outputs Y1 and Y2. The output values of Y1 and Y2 are obtained as follows:
If A<B, then Y1 and Y2 will be equal to A-B. Else Y1 and Y2 will be equal to A.
How To Determinate truth table for this
There are two inputs A[0:1] and B[0:1], 4 inputs in total. Your truth table will have 16 inputs(rows).Are Y1 and Y2 2-bit outputs and is it magnitude of A-B? If yes, The left two columns can be A and next two will be B. For 6 of these rows from 16 cases, A<B in 6 cases ([A,B] = {[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]}). these six rows will have Y1 = Y2 = [B-A]. All other rows will have Y1 = Y1 = A input. Seems straightforward, but I may be missing something here.
I am writing a Checkers game in Prolog, and I want to write a predicate to print all possible moves.
I have a predicate who checks all the legal moves than can be made -
is_move_legal(Board,p(X1,Y1),p(X2,Y2),DoEat, Player):-
Player = w, % making sure the "right" player is playing here - white
get(Board,p(X1,Y1),w),
(
get(Board,p(X2,Y2),1); % make sure no player is in the dest cell
get(Board,p(X2,Y2),0) % make sure no player is in the dest cell
),
between(1,8,X1),between(1,8,X2),between(1,8,Y1),between(1,8,Y2),
(
(DoEat = 0, X2 is X1-1,Y2 is Y1-1); % we don't eat
(DoEat = 0, X2 is X1-1,Y2 is Y1+1);
(DoEat = 1, X2 is X1-2, Y2 is Y1-2, X3 is X1-1, Y3 is Y1-1, (get(Board,p(X3,Y3),b);get(Board,p(X3,Y3),bk)),remove(Board,p(X3,Y3))); % eat the black soldier
(DoEat = 1, X2 is X1-2, Y2 is Y1+2, X3 is X1-1, Y3 is Y1+1, (get(Board,p(X3,Y3),b);get(Board,p(X3,Y3),bk)),remove(Board,p(X3,Y3))) % eat the black soldier
).
I have similair predicates for the black soldiers and for "kings" soldiers.
This is the findall predicate -
% find all possible moves
moves(Board,Moves):-
findall((X->Y),is_move_legal(Board,P1,P2,_,b),Moves).
It seems that it does find the moves, however this it the output I get -
[(_8090->_8092),(_8078->_8080),(_8066->_8068),(_8054->_8056),(_8042->_8044),(_8030->_8032),(_8018->_8020),(_8006->_8008)]
What I am trying to do, is to satisfy the p(X1,Y1), p(X2,Y2) arguments in the is_move_legal predicate.
EDIT:
From a comment here I realized the mistake -Rather then (X->Y), write -
findall((P1->P2),is_move_legal(Board,P1,P2,_,b),Moves).
Your help is much appreciated.
Thank you!
I am using Haskell to make a Verlet integrator to model gravity. The integrator uses the first two positions of the object as seeds and generates the rest after this.
I thought a nice way of making this in Haskell would be to use an infinite list. However, when implemented I find that it runs very slowly for large times (Haskell 1700 time steps: 12 seconds, Python 1700 time steps: < 1 second)
Here is the relevant code for a 1d integrator that has similar performance:
verletStep dt acc xn xn1 = 2*xn1 - xn + (acc xn1)*dt*dt
verlet dt acc x0 x1 = x0 : x1 : next (verlet dt acc x0 x1)
where
next (xn : xs#(xn1:_)) = (verletStep dt acc xn xn1) : next xs
I also tried using zipWith to generate the infinite list but it has similar performance.
Why does this take so long? The garbage collection itself is around 5 seconds. Is there a nice way to make this run faster?
This definition...
verlet dt acc x0 x1 = x0 : x1 : next (verlet dt acc x0 x1)
where
next (xn : xs#(xn1:_)) = (verletStep dt acc xn xn1) : next xs
... leads to verlet dt acc x0 x1 being calculated many times unnecessarily, thus building a lot of unneeded lists. That can be seen by working out a time step by hand:
verlet dt acc x0 x1
x0 : x1 : next (verlet dt acc x0 x1)
x0 : x1 : next (x0 : x1 : next (verlet dt acc x0 x1))
x0 : x1 : (verletStep dt acc x0 x1) : next (x1 : next (verlet dt acc x0 x1))
The solution is to eliminate the unnecessary list-building:
verlet dt acc x0 x1 = x0 : x1 : x2 : drop 2 (verlet dt acc x1 x2)
where
x2 = verletStep dt acc x0 x1
drop 2 removes the first two elements of a list (in this case, x1 and x2, which we have already prepended). verlet is called recursively with the second position, x1, and the newly calculated third one, x2. (Compare with the original definition, in which verlet is called recursively with the same arguments. That should raise suspicion.)
Example:
?-lastN([1,2,3,4],3,T).
T = [2,3,4]
this is whay i'm write:
lastN(L,N,R):- length(L,X), X1 is X-N, lastT(L, N,R).
lastT(L,0,L).
lastT(X,[H|T],L):- X2 is X-1, lastT(T,X2,L).
I assume the question was 'predicate to get last N elements in a list' and this is what you meant to do. It's a simple discarding of the first element after counting how many elements have to be discarded, right? It also does not deal with improper input at all
lastN(L,N,R):- length(L,X), X1 is X-N, lastT(L,X1,R).
lastT(L,0,L).
lastT([H|T],X,L):- X2 is X-1, lastT(T,X2,L).
I have a some predicates inside the recursion verif(X1,Y1,F). The first predicate is gen(X1,Y1,X2,Y2) which receives X1 and Y1 and generates the numbers X2 and Y2. The other predicates are the ones that I want to verify. If one of this predicates returns F=1 the loop should be restarted with verif(X2,Y2,F), but I dont know how to do this. If all the predicates return F=0 the recursion ends.
Here is the example:
verif(X1,Y1,0).
verif(X1,Y1,F):-
gen(X1,Y1,X2,Y2),
pred1(X2,Y2,A,B,F),
pred2(X2,Y2,C,D,F),
pred3(X2,Y2,E,G,F),
verif(X2,Y2,F).
The problem is when the 3 predicates return diferent values for F it will fail.
One way would be to use the predicate repeat until none of the predicates fails, but this way the predicate gen(X1, Y1, X2, Y2) would always generate the same X2 and Y2 because it would allways receive the same X1 and Y1.
I think you can use different variables, and then test the values
verif(X1,Y1,F):-
gen(X1,Y1,X2,Y2),
pred1(X2,Y2,A,B,F1),
pred2(X2,Y2,C,D,F2),
pred3(X2,Y2,E,G,F3),
(( F1 == 0, F2 == 0, F3 == 0 ) -> true ; verif(X2,Y2,F)).
or simpler
verif(X1,Y1,F):-
gen(X1,Y1,X2,Y2),
pred1(X2,Y2,A,B,F1),
pred2(X2,Y2,C,D,F2),
pred3(X2,Y2,E,G,F3),
(F1 + F3 + F3 =:= 0 -> true ; verif(X2,Y2,F)).