Products > Programming

The Julia thread

<< < (8/8)

brucehoult:
Oh, and JavaScript too:


--- Code: ---$ node -e 'a=[1,2,3,4];b=a;b[2]=42;console.log(a)'
[ 1, 2, 42, 4 ]

--- End code ---

And Ruby:


--- Code: ---$ ruby -e 'a=[1,2,3,4];b=a;b[2]=42;print a,"\n"'
[1, 2, 42, 4]

--- End code ---

But you'd like Perl. Maybe it's the true beginner's language?


--- Code: ---$ perl -e '@a=(1,2,3,4);@b=@a;$b[2]=42;print "@a\n";print "@b\n"'
1 2 3 4
1 2 42 4

--- End code ---

DiTBho:
Perhaps we should use two different operators
"A ==> B" to assign the object pointer (B points to the same content of A)
"A = B" to allocate a new content area, copy the content from A, and assign the pointer to B

rstofer:
By itself, it's not a problem.  Most languages try to avoid the copy including Fortran.  Certainly, the copy is wasteful of time and memory if it isn't truly needed.  Where it can become an issue is for those familiar with MATLAB.  Even in MATLAB, the copy operation is controllable with care:

https://www.mathworks.com/help/matlab/matlab_prog/avoid-unnecessary-copies-of-data.html

Pass by value versus pass by reference has been a debate for 70+ years.  By default, Fortran passes by reference and in modern incantations, this can be overridden.  C is pass by value and pointers are explicit.  Clearly, both schemes work and we use both in our daily lives.  We just need to keep it straight.

https://docs.oracle.com/cd/E19957-01/805-4940/z400091044b8

In the end, it's just something to be aware of.  This topic is of great importance in machine learning where the matrices are HUGE.  Copying is to be avoided both in terms of time and space.  We already have to copy the data from CPU memory to GPU memory and bring the results back.  12 GB on a graphics card seems a lot but it's only 1.5 giga qwords.  Start throwing some 10,000x10,000 matrices of qwords around and it fills up fast.  Yet the idea is to keep as much of the math inside GPU space as possible to avoid copying.

SiliconWizard:
This is a sane choice indeed.
But it's interesting to see how something that may look as mundane as an "assignment" can mean different things to different people, and in turn to different languages.

And it comes with the now old question of whether using the equal sign for assignments even makes sense.

rstofer:

--- Quote from: SiliconWizard on January 11, 2022, 07:14:02 pm ---This is a sane choice indeed.
But it's interesting to see how something that may look as mundane as an "assignment" can mean different things to different people, and in turn to different languages.

And it comes with the now old question of whether using the equal sign for assignments even makes sense.

--- End quote ---

Niklaus Wirth thought it made no sense and used := for assignment in Pascal as did the authors of Algol.  It makes sense because the operator was described as 'becomes' rather then 'equal'.  Equality is an entirely different concept and symbol '='.  Other languages use '=' and '=='.

Wirth shows up as one of the designers of Algol W in 1966 and later Pascal, Modula, Modula 2, Oberon, Oberon 2 and Oberon-07.   A true luminary!

Navigation

[0] Message Index

[*] Previous page

There was an error while thanking
Thanking...
Go to full version