HP Forums
How to write a program to output multiple results - 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 write a program to output multiple results (/thread-15791.html)



How to write a program to output multiple results - cloudxff - 10-25-2020 03:52 AM

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.


RE: How to write a program to output multiple results - pinkman - 10-25-2020 06:47 PM

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.


RE: How to write a program to output multiple results - cloudxff - 10-26-2020 03:35 AM

(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


RE: How to write a program to output multiple results - pinkman - 10-26-2020 10:27 AM

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;



RE: How to write a program to output multiple results - cloudxff - 10-26-2020 11:32 AM

(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!


RE: How to write a program to output multiple results - Albert Chan - 10-26-2020 12:49 PM

(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]


RE: How to write a program to output multiple results - pinkman - 10-26-2020 01:23 PM

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.


RE: How to write a program to output multiple results - StephenG1CMZ - 10-26-2020 02:51 PM

There is not a flatten built-in, but you will find a couple of LIST FLATTEN functions here:
https://www.hpmuseum.org/forum/thread-9411.html


RE: How to write a program to output multiple results - Albert Chan - 10-26-2020 06:54 PM

(10-26-2020 01:23 PM)pinkman Wrote:  Yes good approach, but there is no flatten function on the Prime.
We should request it ! Big Grin

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)));
}



RE: How to write a program to output multiple results - cloudxff - 10-27-2020 04:55 AM

(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 ! Big Grin

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