I don't understand what feature of the language is behind this code to work. Specifically, how are the values from [a,b,c].sort automatically populated into the variables ?
def isTriangle(a,b,c)
a, b, c = [a, b, c].sort
a + b > c
end
As mentioned by Cary Swoveland an spickermann this works because of array decomposition.
The sort method gives back an array, its content is then assigned to the variables. In the link given above you can see that in the documentation it is written as:
(a, b) = [1, 2]
I assume that by only writing
a, b = [1, 2]
some kind of syntactic sugar is used to make this work.
I tried it in IRB and the two variants behave the same.
Related
so i am trying to solve kenken using prolog but i ran into several problems from the start, first of all lets say i run it like kenken([X1,X2,X3,.....X16]). and i want to solve for this x's with rules i defined before. so lets say the first cell has 3 values X1,X2,and X3 and i want to get 2 by using multiplication meaning that X1*X2*X3=2, now how do i set up a rule to see all posible solutions if i had something like that.
also how would i tell my x's to only use a range of values 1-4.
i tried to do something like
:- use_module(library(clpr)).
solve([X1,X2,X3]):-
{X1*X2*X3=2}.
but its gives me a really weird output.
Since you reason over integers, not floats, consider using library(clpfd) instead of CLP(R). In SICStus, SWI and YAP, you can constrain a finite domain variable X to the integer range 1-4 with:
X in 1..4
You can use the built-in predicate label/1 to search for concrete solutions. Example with SWI-Prolog:
?- Vars = [A,B,C], A*B*C #= 2, Vars ins 1..4, label(Vars).
yielding:
Vars = [1, 1, 2], A = B, B = 1, C = 2 ;
Vars = [1, 2, 1], A = C, C = 1, B = 2 ;
Vars = [2, 1, 1], A = 2, B = C, C = 1.
You need the is operator to do arithmetic:
2 is X1*X2*X3
Note that this will not work unless the X's are all bound to numbers.
I am c++ guy and i am completely new to prolog.
I am using sicstus prolog.
i came across a need as below:
lets say i have a variable
A={0,1,2,3}
B={-2,-1,0,1,2,3,4,5}
and i have hash kind of thing like
0-{3}
1-{4}
now i need to filter the values of A and B using this hash so that after the operation:
A={0,1}
B={3,4}
logic is values from A will be matched with keys of the hash
if the key exists then check for the value.if the value exists in B then the value in A remains.
otherwise value should be deleted.
in the same way it should be done for B with the values in the hash shoul dbe searched in A and if not present then it should be deleted in B.
means the exactly opposite way for B.
Could anybody please help?
I'd suggest you use lists to hold the keys and values of A and B, and a list of Key-Value pairs to hold your hashmap. That way you can use builtin helper predicates include/3 and memberchk/2 to suit your needs.
Then you can write a procedure that filters the items of A and B:
filter(A, B, Hash, FA, FB):-
include(filterkey(B, Hash), A, FA),
include(filtervalue(A, Hash), B, FB).
filterkey(B, Hash, Item):-
memberchk(Item-Value, Hash),
memberchk(Value, B).
filtervalue(A, Hash, Value):-
memberchk(Item-Value, Hash),
memberchk(Item, A).
Say if you have
A=[0,1,2,3]
B=[-2,-1,0,1,2,3,4,5]
Hash=[0-3, 1-4]
then:
?- A=[0,1,2,3], B=[-2,-1,0,1,2,3,4,5], Hash=[0-3, 1-4], filter(A, B,Hash, FA, FB).
Hash = [0-3, 1-4],
FA = [0, 1],
FB = [3, 4].
I'm sorry I don't have Sicstus available to test, and then I could be completely out-of-track, but you are handling a very peculiar kind of variables. Consider
?- write_canonical({1,2,3,4}).
{}(','(1,','(2,','(3,4))))
?- {1,2,3,4}={A}.
A = (1, 2, 3, 4).
Braces are really just a peculiar name for a tuple, and AFAIK are used just as syntactic device to introduce readable data in a DSL (Domain Specific Language), like for instance constraints in library(clpqr).
What I mean is that or
you are using the wrong representation for the task (gusbro addressed this problem, +1)
you are searching for a clp constraint extension (filtering?) not available in Sicstus. But then the question should be reformulated in better terms.
Anyway you could adapt gusbro' answer without changing your program if you add, for instance,
member_set(E, {','(E,_)}).
member_set(E, {','(_,T)}) :- member_set(E, {T}).
member_set(E, {E}).
to replace memberchk. include/3 must be rewritten also, but it's not so easy.
Otherwise, a conversion predicate
set_list({','(A,B)}, [A|R]) :- set_list({B}, R), !.
set_list({E}, [E]).
set_list({}, []).
could be handy:
?- set_list(S,[1,2,3]).
S = {1, 2, 3}.
?- set_list({1,2,3},L).
L = [1, 2, 3].
L = [a, b, c|_]. - an example of an incomplete list. how do I append two of these? How do I reverse an incomplete list? Can someone give me tips how to deal with these in general?
append([x|t],y,[x|r]):-append(t,y,r). This is how two lists are appended.
for instance
?- A=[1,2,3|X], B=[a,b,c|Y], X=B.
A = [1, 2, 3, a, b, c|Y],
X = B, B = [a, b, c|Y].
These patterns are of little utility.
As #Daniel Lyons suggests, you can as well use append/3
?- A=[1,2,3|X], B=[a,b,c|Y], append(A,B,C).
A = [1, 2, 3],
X = [],
B = [a, b, c|Y],
C = [1, 2, 3, a, b, c|Y]
You can see the difference between those patterns: first directly binds X to B (1 inference), while append requires a number of inferences equal to first list length, to reach the tail before binding.
You can read more about incomplete data structures here. The most useful pattern is difference lists, the base for dcg.
I'm a beginner in Mathematica programming. My code is not running as expected. I wonder if anyone could examine what goes wrong? Here is part of the code.
F[{k_, n_, x_}] =
Which[k == 0, f[a, b, x],
k == 1, g[a, b, n, x],
k == 2, h[c, d, n, x]]
G[x_] = F[{0, 0, x}]
While[Extract[G[x], 1] != 3, G[x_] = F[G[x]]]
The functions f, g and h are defined by Which as is F, and they are all vector-valued so that it makes sense to iterate F. What I want to achieve is: given initial value {0,0,x}, keep iterating F until the first component of F becomes 3. Is there anything, e.g. syntax, wrong in the above code?
Thanks!
You need to use SetDelayed (:=) for function definitions like: F[x_]:=x. When you use Set (=) like F[x_]=x, the it is essentially the same as F[_]=x since the definition isn't delayed until evaluation, so there is no way to transfer the matched pattern on the left hand side into the evaluation of the right hand side.
As jVincent mentioned, I would use := instead of = while defining F.
I would also use the built in NestWhile instead of manually iterating.
NestWhile[F, {0, 0, x}, Function[e, Extract[e, 1] != 3]]
I can't quite comment on how to correct the code as written because I'm not entirely sure how reassigning G in the While works.
In[1]:= SameQ[Dot[1, 2], 1.2]
TrueQ[Dot[1, 2] == 1.2]
a = 1; b = 2;
SameQ[Dot[a, b], a.b]
TrueQ[Dot[a, b] == a.b]
Out[1]= False
Out[2]= False
Out[4]= True
Out[5]= True
I know this uses Dot command wrong. Anybody can give me a clear reson for the above different results?
thanks!
a.b is interpreted as Dot[a,b] and then variables a and b are substituted, meaning Dot[1,2] and thus equality holds. This is not the same as 1.2 where the dot stands for the decimal separator and not for the inline operator of Dot.
When you write 1.2, Mma understands a number (aka 6/5), but if you write {1, 1}.{2, 2} or a.b Mma understands a scalar product, as usual in any book using vectors.
HTH!
It can be informative to view an expression under Hold and FullForm:
a = 1; b = 2;
SameQ[Dot[a, b], a.b]] //Hold //FullForm
Hold[SameQ[Dot[a, b], Dot[a, b]]]
With this combination of commands, Mathematica parses but does not evaluate the expression (Hold), and then shows the long pseudo-internal form of the expression (FullForm).
In this case, you can see that the second term a.b is parsed as Dot[a, b] before any evaluation happens.
When . appears with numerals as in 1.2 it is interpreted specially as a decimal point. This is similar to other numeric entry formats such as: 1*^6 which is recognized directly as 1000000:
1*^6 //Hold //FullForm
Compare trying to enter:
a = 1;
a*^6