How to pass by reference in XCAS ? - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: How to pass by reference in XCAS ? (/thread-15794.html) |
How to pass by reference in XCAS ? - medwatt - 10-26-2020 03:10 AM 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 ? RE: How to pass by reference in XCAS ? - Stevetuc - 10-26-2020 07:20 AM (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 RE: How to pass by reference in XCAS ? - Albert Chan - 10-26-2020 11:17 AM 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] RE: How to pass by reference in XCAS ? - pinkman - 10-26-2020 11:43 AM 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? RE: How to pass by reference in XCAS ? - medwatt - 10-26-2020 12:31 PM (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. RE: How to pass by reference in XCAS ? - Albert Chan - 10-26-2020 01:46 PM 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] ... RE: How to pass by reference in XCAS ? - parisse - 10-27-2020 07:32 AM You can store a value by reference in a list with =< instead of := Example Code:
RE: How to pass by reference in XCAS ? - Albert Chan - 10-27-2020 08:14 AM 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] RE: How to pass by reference in XCAS ? - parisse - 10-27-2020 05:17 PM I would say it's neither a bug nor a feature... |