Example of programming language that is left associative? - operators

I was teaching a friend about programming and I had a hard time convincing them that a = b and b = a are two very different things.
I eventually found the correct words to describe it (right associative) which got me thinking.
Are there any programming languages which are left associative? I have never seen a language where:
a = b results in b being set to the value of a.

You misunderstood associativity. An operator op is associative if (a op b) op c is equivalent to a op (b op c). For operators that are not associative it thus becomes relevant whether a op b op c stands for the former or the latter. Thus we distinguish between left-associative operators, where it's (a op b) op c, and right-associative operators, where it's a op (b op c).
Most operators in most languages are left-associative. Take for example -: a - b - c is equivalent to (a - b) - c in most languages, not a - (b - c).
The assignment operator is an exception from that as (a = b) = c is generally not legal (as you can't assign to the result of an assignment). Thus in most languages a = b = c is equivalent to a = (b = c). A notable exception is Python where a = b = c does not associate at all and is simply illegal.
None of this has anything to do with the difference between a = b and b = a. Since this involves only a single use of the = operator, associativity does not factor into this at all. Rather the relevant property is commutativity: An operator op is commutative if a op b is equivalent to b op a. I'm not aware of any language where assignment is commutative, nor do I have an idea of how it could be.
Concepts like left-commutativity or right-commutativity do not exist. There is, to the best of my knowledge, no term for the question "Does a = b assign b to a or vice-versa?" - that's just part of the semantics of the assignment operator.

I don't believe so. Though you can always overload the assignment operator and cause complete confusion inside your c++.

R has both <- and -> assignment operators defined.
> b <- 42
> b -> a
> a
[1] 42

If we talk about Operator associativity we should actually consider two types of associativity: side_associative (e.g. = ) or non-associative (e.g. == ) as it doesn't make any difference for a machine if it get's the line from left to right or from right to left.
There are no such programming languages that are originally left associative but some of them allow it via operator overloading and some (e.g. R) will allow both: -> and <-.
I believe no-one will like it in Europe but it might be found lovely in the Middle East. I can imagine that there are IDEs that switch right-side with left-side but at the end compilers are written in an European way.

Related

Multiple Boolean statements VBA

I am having issues with some Boolean logic.
Essentially I want something to program in VBA a filter such that,
A = True AND (B = True OR B = False)
I just cannot seem to get the coding right to do this in VBA (I am using MS Access).
I have tried:
A = True AND B = True OR B = False
But this obviously fails (looks for either A,B = True OR B = False, essentially).
Am I missing something obvious here?
I chose to left out the actual code, but if requested I can post. My thinking is that I am missing some basic with the Boolean logic.
If A, B, and C are Boolean expressions, then comparing them to a Boolean literal (True, False) is redundant: the expression is already a Boolean expression.
That makes the original logic go like this:
If A And (B Or Not C) Then
The And operator has greater priority than the Or operator, so it gets evaluated first; if the (B Or Not C) part needs to be evaluated as a whole, then the parentheses are required, otherwise A And B takes precedence, making the expression (wrongly) evaluate as:
If (A And B) Or Not C ' redundant parentheses to illustrate operator precedence
Edit: I misread the OP, my brain put that C in there. If A And (B Or Not B) Then is redundant, as BigBen pointed out - the simplified logic would be If A Then. Still worth noting operator precedence and using parentheses when applicable and appropriate though.

Restrain variable to a bounded region (interval) formulation in Mixed Integer Linear Programming

I have 4 non negative real variable that are A, B, C and X. Based on the current problem that I have, I notice that the variable X must belong to the interval of [B,C] and the relation will be a bunch of if-else conditions like this:
If A < B:
x = B
elseif A > C:
x = C
elseif B<=A<=C:
x = A
As you can see, it quite difficult to reformulate as a Mixed Integer Programming problem with corresponding decision variable (d1, d2 and d3). I have try reading some instructions regarding if-then formulation using big M method at this site:
https://www.math.cuhk.edu.hk/course_builder/1415/math3220/L2%20(without%20solution).pdf but it seem that this problem is more challenging than their tutorial.
Could you kindly provide me with a formulation for this situation ?
Thank you very much !

How to determine if a context-free grammar describes a regular language?

Given an arbitrary context-free grammar, how can I check whether it describes a regular language?
I'm not looking for exam "tricks". I'm looking for a foolproof mechanical test that I can code.
If it helps, here's an example of a CFG that I might receive as an input.
Specifically, notice that the answer must be much more complicated than just looking for left- or right-recursion, since the presence of another type of recursion does not automatically imply the grammar is irregular.
S: A B C D X
A: A a
A:
B: b B
B:
C: c C c
C: c
D: D d D
D: d
X: x Y
X:
Y: y X
Y:
There is no such mechanical procedure because the problem of determining whether a CFG defines a regular language is undecidable.
This result is a simple application of Greibach's Thereom.

Which languages have a primitive operation for swapping variables?

In most languages, if you want to swap two variables, it's something like:
var c = b
b = a
a = c
Yes, you can do fancy hacks with XOR if you like but it's generally 3 lines of code for a single operation. Are there any languages that have swapping variables as a primitive in the language?
Lua, Python, Ruby and more support this notation:
a, b = b, a
And javascript sure needs no temporary variable either ;)
a = -(b = (a += b) - b) + a;
For more examples on how to swap variables (in 86 languages), see: http://rosettacode.org/wiki/Generic_swap
In most dynamic languages you can do something like this to swap:
a, b = b, a
Now a have the value of b, and b has the value of a. I am not sure if this is what you meant or not.

Logic operator 'A doesn't imply B' using AND, OR, XOR operators

I have two bit masks (say A and B, and I want to know which bit of A is 1 where the corresponding B bit is 0 (and viceversa).
Of course this is implementable using conditional statements, but I don't want to iterate/shift for testing all bits of the bit mask.
The logic condition which I need is not implemented (or at least, I can't see it). Using a parallel with the logic gates, I need the operator 'A doesn't imply B' (see on Wikipedia).
Is it possible to implement such operator using AND, OR, XOR operators?
According to the linked Wikipedia, the simplest expression is A and (not B)
You can use:
A xor (not B)
A xor B
Analysis:
A B A xor B
0 0 0
0 1 1
1 0 1
1 1 0
Edit:
Changed it to a simple XOR, that gives the result that the question asks for. Perhaps the question is not correctly worded, as the solution seems too simple...
I would say:
A AND (A XOR B)
gives you a 1 where A is 1 and B is 0.
First you detect where A an B are different, then you match with where A is 1.