As we all know, when you write a program, you can use the return statement to return the result you want.But is there a way to return multiple results to a CAS or HOME interface (not a terminal)?For example, I write an output program that contains a return instruction that can be called to return a result.But when I write another program that puts an output program in a for loop, it still has only one result on the CAS or HOME interface. Why is that?Theoretically, I've called the output program multiple times, and it should produce multiple results.

The approach of the Prime built in functions is to return a list. See EIGENVV for example.

RETURN {val1, val2, val3}

You can also dynamically build the list with an arbitrary number of values with the CONCAT function.

(10-25-2020 06:47 PM)pinkman Wrote: [ -> ]The approach of the Prime built in functions is to return a list. See EIGENVV for example.

RETURN {val1, val2, val3}

You can also dynamically build the list with an arbitrary number of values with the CONCAT function.

Thank you for your guidance. I will learn this skill

Here is a small example. FIBO calculates the Fibonacci values between two numbers and returns the values as a list.

Usage example:

FIBO(50, 250)
Returns:

{55,89,144,233}
Code:

EXPORT FIBO(fstart, fend)

BEGIN

LOCAL a := 1, b := 1, c := 1, ret := {};

IF fend >= fstart AND fend >= 0 THEN

WHILE c <= IP(fend) DO

IF c >= fstart THEN

ret := CONCAT(ret, c);

END;

c := a + b;

a := b;

b := c;

END;

END;

RETURN ret;

END;

(10-26-2020 10:27 AM)pinkman Wrote: [ -> ]Here is a small example. FIBO calculates the Fibonacci values between two numbers and returns the values as a list.

Usage example: FIBO(50, 250)

Returns: {55,89,144,233}

Code:

EXPORT FIBO(fstart, fend)

BEGIN

LOCAL a := 1, b := 1, c := 1, ret := {};

IF fend >= fstart AND fend >= 0 THEN

WHILE c <= IP(fend) DO

IF c >= fstart THEN

ret := CONCAT(ret, c);

END;

c := a + b;

a := b;

b := c;

END;

END;

RETURN ret;

END;

I recreated the FIBO program on my Prime, and now I've learned to return multiple values, thank you very much!

(10-26-2020 10:27 AM)pinkman Wrote: [ -> ]ret := CONCAT(ret, c);

CONCAT builds a new list, combining ret and c, using time of O(len(ret) + len(c))

We might speed it up by building nest-list, then flatten it.

ret := {{}}; // list of list

...

ret[0] := {ret[0], c}; // inplace update, not building long list (assumed 0-based indexing)

...

return flatten(ret);

Example:

XCas> lst := [[]]

XCas> lst[0] := [lst[0], [1,2,3]]

XCas> lst[0] := [lst[0], [4,5,6]]

XCas> flatten(lst)

[1,2,3,4,5,6]

Yes good approach, but there is no flatten function on the Prime.

I also wanted to avoid using CAS functions in a Home HPPL program, because of the isolation of both environments.

(10-26-2020 01:23 PM)pinkman Wrote: [ -> ]Yes good approach, but there is no flatten function on the Prime.

We should request it !

I was looking at XCas source misc.cc, I discovered there is an undocumented

flatten1
XCas> flatten1([1,2,3,[4,[5]]]) → [1,2,3,4,[5]]

The code also explained why flatten is so fast.

It only scan the list 1 time, adding 1 element at a time, with time complexity of O(n)

We could emulate the functionality of flatten, but time complexity is O(n^2)

Here is a simple implementation. (note that concat is needed, which kills performance.)

Code:

`flat(lst) := {`

local h := head(lst);

return (h == lst) ? h : concat(flat(h), flat(tail(lst)));

}

(10-26-2020 06:54 PM)Albert Chan Wrote: [ -> ] (10-26-2020 01:23 PM)pinkman Wrote: [ -> ]Yes good approach, but there is no flatten function on the Prime.

We should request it !

I was looking at XCas source misc.cc, I discovered there is an undocumented flatten1

XCas> flatten1([1,2,3,[4,[5]]]) → [1,2,3,4,[5]]

The code also explained why flatten is so fast.

It only scan the list 1 time, adding 1 element at a time, with time complexity of O(n)

We could emulate the functionality of flatten, but time complexity is O(n^2)

Here is a simple implementation. (note that concat is needed, which kills performance.)

Code:

`flat(lst) := {`

local h := head(lst);

return (h == lst) ? h : concat(flat(h), flat(tail(lst)));

}

The discussion is a little difficult for me to understand, so I will continue my study