I am writing something in Python syntax and wasn't getting the same answer. Upon inspection, the reason for this is because values are not passed by reference. I'm writing an algorithm where several functions need to work on the same array. How do I pass by reference ?
(10-26-2020 03:10 AM)medwatt Wrote: [ -> ]I am writing something in Python syntax and wasn't getting the same answer. Upon inspection, the reason for this is because values are not passed by reference. I'm writing an algorithm where several functions need to work on the same array. How do I pass by reference ?
Pass by reference is not supported on the Prime. There are some workarounds discussed here, which presumbly can be adapted for a Python syntax cas program:
https://www.hpmuseum.org/forum/thread-9185.html
You can emulate pass by reference by putting data inside a list
XCas> add1(lst) := {local I; for I from 1 to len(lst) do lst(I) += 1 end}
XCas> a := [1,2,3]
XCas> add1(a) → [2, 3, 4]
XCas> add1(a) → [3, 4, 5]
I've often wondered why Haskell favors passing arguments by value, and having studied it I've never seen a problem with it.
Could you give us the principles of this algorithm so that we can find the right way to implement it without sharing a common data structure?
(10-26-2020 11:43 AM)pinkman Wrote: [ -> ]I've often wondered why Haskell favors passing arguments by value, and having studied it I've never seen a problem with it.
Could you give us the principles of this algorithm so that we can find the right way to implement it without sharing a common data structure?
Take for example the quick sort algorithm where one of the benefits it has over other sorting algorithms is that you can do in-place sorting.
Another example, in-place permutations of list.
We can swap 2 elements to create another permutation, this involved 3 assignments.
x = lst[i]; lst[i] = lst[j]; lst[j] = x;
We could reduce above to about 2 assignments, by reusing x.
Here is Ive's method, see
Permutation Generation Methods, by Robert Sedgewick, page 148-149
Code:
def ives(lst, i, j):
if i >= j: yield lst; return
x = lst[i]
idx = xrange(i+1, j+1)
for pat in ives(lst, i+1, j-1): # (n-2)! permutations
for block in idx: # (n-1) blocks
yield pat # n pats/block
for k in idx: # shift x to right
pat[k-1] = pat[k]
pat[k] = x
yield pat
pat[j] = pat[i] # restore x to front
pat[i] = x
>>> permute = lambda lst: ives(lst, 0, len(lst)-1)
>>> for pat in permute(range(1,6)): print pat
...
[
1, 2, 3, 4, 5]
[2,
1, 3, 4, 5]
[2, 3,
1, 4, 5]
[2, 3, 4,
1, 5]
[2, 3, 4, 5,
1]
[
1, 3, 4, 5, 2]
[3,
1, 4, 5, 2]
[3, 4,
1, 5, 2]
[3, 4, 5,
1, 2]
[3, 4, 5, 2,
1]
...
You can store a value by reference in a list with =< instead of :=
Example
Code:
l:=[1,2]; m:=l; l[1]=<3; m;
The modification will affect all references, therefore be careful!
Hi, parisse
We get pass-by-reference also with +=. Is this a feature, or a bug ?
XCas> l:=[1,2]; m:=l; l[1]=<3; m → [1,2],[1,2],[1,3],[1,3]
XCas> l:=[1,2]; m:=l; l[1]+=1; m → [1,2],[1,2],[1,3],[1,3]
XCas> l:=[1,2]; m:=l; l[1]:=l[1]+1; m → [1,2],[1,2],[1,3],[1,2]
I would say it's neither a bug nor a feature...