How to pass by reference in XCAS ?
|
10-26-2020, 03:10 AM
Post: #1
|
|||
|
|||
How to pass by reference in XCAS ?
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, 07:20 AM
(This post was last modified: 10-26-2020 07:22 AM by Stevetuc.)
Post: #2
|
|||
|
|||
RE: How to pass by reference in XCAS ?
(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 |
|||
10-26-2020, 11:17 AM
Post: #3
|
|||
|
|||
RE: How to pass by reference in XCAS ?
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] |
|||
10-26-2020, 11:43 AM
Post: #4
|
|||
|
|||
RE: How to pass by reference in XCAS ?
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? Thibault - not collector but in love with the few HP models I own - Also musician : http://walruspark.co |
|||
10-26-2020, 12:31 PM
Post: #5
|
|||
|
|||
RE: How to pass by reference in XCAS ?
(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. 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. |
|||
10-26-2020, 01:46 PM
Post: #6
|
|||
|
|||
RE: How to pass by reference in XCAS ?
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): >>> 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] ... |
|||
10-27-2020, 07:32 AM
Post: #7
|
|||
|
|||
RE: How to pass by reference in XCAS ?
You can store a value by reference in a list with =< instead of :=
Example Code:
|
|||
10-27-2020, 08:14 AM
Post: #8
|
|||
|
|||
RE: How to pass by reference in XCAS ?
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] |
|||
10-27-2020, 05:17 PM
Post: #9
|
|||
|
|||
RE: How to pass by reference in XCAS ?
I would say it's neither a bug nor a feature...
|
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)