I can't find anything about this through Google so I have to ask here. I want to do something like this (very pseudo code):
y = first_value
x={op_1 = >, op_2 = <, c = some_value}
if first_value x.op_1 x.c then
...
end
What that code says to me is that if first_value if greater than x's c value then do something. Now, I know I could set op_1 and op_2 to some value to differentiate between them and then compare values using separate if statements, but I would like to minimize the number of if statements used.
I was just wondering if something like this is possible, maybe even in a different form. Thanks in advance!
Not this way, an operator is a specific symbol that is part of the syntax. However, you can represent an operation using a function:
y = first_value
x={op_1 = function(a,b)return a>b end, op_2 = function(a,b)return a<b end, c = some_value}
if x.op1(first_value, x.c) then
...
end
Related
Simple example: I have some functions and I need to call them all, to modify a structure, only in one function. With these simple functions the task can be made in ways that don't use void, but in others tasks you have to use void. So what can you do?
type player = { mutable name : string; mutable points : int } ;;
let putname brad = match brad with
{ name = x; points = y } -> { name = brad; points = y } ;;
let putpoint guy score = match guy with
{ name = x; points = y } -> { name = x; points = score } ;;
let loosers listplayer guy = guy :: listplayer ;;
Here is the problem - How can I do the next function?
let someoneloses guy = void
guy = putpoint guy 0 ;;
listplayer = loosers (listplayer guy) ;;
Given you are using the name "void" I'm assuming you are more familiar with C (or C++). In OCaml the equivalent of "void" (the name of the type for no value) is "unit". There is another difference though: while in C the syntax is complex enough that it have constructs for no values (for instance, you can either "return a_value;" or "return;", two differents yet syntactically valid use cases for the keyword "return"), in OCaml the syntax is simpler and always require a value. So we have a notation for "nothing", which is, astutely but maybe also confusedly, is written "()".
So, the OCaml equivalent of the C:
void do_nothing(void) { return; }
is written:
let do_nothing () = ()
(notice how OCaml syntax is simpler and easier to grok once you got the "()" trick).
Now that this is hopefully clearer, back to your question.
A function that returns nothing is a function that return "()", either explicitly (as "do_nothing" above) or because it ends with an expression that has "()" as its value. For instance, an assignment (something tell me you'll love assignments), such as:
let putpoint guy score = guy.points <- score
Now back to your problem. You seem to be doing some kind of game with players represented as mutable records and some functions modifying those records as the game develop. You need not use pattern matching for that. Actually, functions such as "putpoint" above is probably what you want. But then you need some more state in your program: the list of loosers for instance is probably going to be a reference to a list that you modify etc.
This is the "imperative" side of OCaml but there is another one, which is usually regarded as more elegant although often slower in general (but not in functional languages which are optimised for this technique), consisting of refraining from altering state (changing values of things) but instead using functions merely taking values and returning values. Implemented like this, a player would be represented as an immutable record and each function acting a user would take an "old user" and return a "new user", and the same goes with the list of loosers, and so on. Actually, the whole game state would be represented as a big value that the "main loop" of your program would, given the previous value, and possible also the time and user inputs, would compute the "new state" and return it.
Have fun!
Also, your question has nothing to do with ocaml-batteries.
since you are using mutable data, you just have to assigned the value directly.
let p = {name = "me";points=0};;
let update x = x.name <- "you";
x.points <- 3;;
update p ;;
I want to overwrite some elements of a vector x with a scalar a based on a 0/1 vector cond. In pseudocode: x[cond]=a.
x[cond] is not the right way of subsetting in Mata. I should use select(x,cond). Unfortunately, this latter object cannot be assigned to.
x[selectindex(cond)] = a fails because such an assignment requires the same dimensions on both sides of the =.
I could modify the latter approach to
x[selectindex(cond)] = J(sum(cond),1,a)
Is that the idiom in Mata? I was expecting something more straightforward because Stata has nice replace x = a if cond syntax.
In the general case, I think that's about as good as you're going to get. sum(cond) is safe if cond is 0 or 1, but a more general alternative is:
select = selectindex(cond)
x[select] = J(length(select), 1, a)
I agree that this is not the simplest syntax. An additional assignment colon operator := would be nice here.
If x and cond are views, st_store() is another option:
st_store(., st_viewvars(x), st_viewvars(cond), J(sum(cond), 1, a))
If you already know the variable names/indices and don't have to call st_viewvars(), all the better.
I am using th z3 C++ API. if I create this simple false expression:
z3::expr x = C->int_const("x");
z3::expr p = z3::forall(x, x==0);
and try to solve, I get an unknown outcome. I am not an expert of strategies and tactics, but I am sure that z3 can solve this, if I use the right tactic.
I also tried
z3::expr p = !z3::forall(x, x==0);
with, of course, the same runknown esult.
I'm not familiar with z3, but from a general C++ perspective, wouldn't x==0 evaluate first, i.e. wouldn't your call be equivalent to implies(x, 1)? From a quick search it seems you may have to construct each piece of the statement as a z3 object, for example:
Z3_ast consequent = Z3_mk_eq(ctx, x, 0);
Z3_ast theorem = Z3_mk_implies(ctx, x, consequent);
But the above is not correct, either. I believe the parameters x and 0 themselves have to be instances of Z3_ast that encapsulate the statement you mean (as opposed to their interpolated values or references).
I am doing this a lot:
auto f_conj = f.conjugate(); //f is a MatrixXcf, so is C;
for(n=0;n<X.cols();++n)
C.col(n) = X.col(n).cwiseProduct(f_conj);
Am I not supposed to be able to do something like
C.colwise() = X.colwise().cwiseProduct(f_conj)
instead?
What you are really doing is a diagonal product, so I'd recommend you the following expression:
C = f.conjugate().asDiagonal() * X;
If you want to use a colwise() expression, then do not put it on the left hand side:
C = X.colwise().cwiseProduct(f.conjugate());
Moreover, let me warn you about the use of the auto keyword. Here, let me emphasize that f_conj is not a VectorXcf, but an expression of the conjugate of a VectorXcf. So using f_conj or f.conjugate() is exactly the same. Since multiplying two complexes or one complex and one conjugate complex amount to the same cost, in this precise case it's ok to use the auto keyword. However, if f_conj would be for instance: auto f_conj = (f+g).conjugate(), then f+g would be recomputed many times in your for loop. Doing (f+g).conjugate().asDiagonal() * X is perfectly fine though, because Eigen knows what to do.
I just ran into this piece of code that does this :
delete a, a = 0;
It compiles and runs just fine. But isn't this supposed to be :
delete a;
a = 0;
Why is separating statements using , allowed in this case ?
Thanks :)
In C and C++, most "statements" are actually expressions. The semicolon added to an expression makes it into a statement. Alternatively, it is allowed (but almost always bad style) to separate side-effectful expressions with the comma operator: the left-hand-side expression is evaluated for its side-effects (and its value is discarded), and the right-hand-side expression is evaluated for its value.
This is the comma-operator. It evaluates both it's arguments and returns the second one.
This is the comma operator. It can be used to separate expressions, but not declarations.
That is comma operator. MSDN article is here. And have a look at this question to understand how it works.
While it is possible to write code like that, it may be somewhat weird. A slightly more realistic usecase would be if you have a struct T as follows:
struct T {
bool check() const;
void fix();
};
Now you want to iterate through everything in the struct and run check on it, and then call fix if check returns false. The simple way to do this would be
for (list<T>::iterator it = mylist.begin(); it < mylist.end(); ++it)
if (!it->check())
it->fix();
Let's pretend you want to write it in as short a way as possible. fix() returning void means you can't just put it in the condition. However, using the comma operator you can get around this:
for (auto it = mylist.begin(); it != mylist.end() && (it->check() || (it->fix(), true)); ++it);
I wouldn't use it without a particularly good reason, but it does allow you to call any function from a condition, which can be convenient.