Post Reply 
How to write a program to output multiple results
10-25-2020, 03:52 AM
Post: #1
How to write a program to output multiple results
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.
Find all posts by this user
Quote this message in a reply
10-25-2020, 06:47 PM
Post: #2
RE: How to write a program to output 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.

Thibault - not collector but in love with the few HP models I own - Also musician : http://walruspark.co
Find all posts by this user
Quote this message in a reply
10-26-2020, 03:35 AM
Post: #3
RE: How to write a program to output multiple results
(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
Find all posts by this user
Quote this message in a reply
10-26-2020, 10:27 AM (This post was last modified: 10-26-2020 10:55 AM by pinkman.)
Post: #4
RE: How to write a program to output multiple results
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;

Thibault - not collector but in love with the few HP models I own - Also musician : http://walruspark.co
Find all posts by this user
Quote this message in a reply
10-26-2020, 11:32 AM
Post: #5
RE: How to write a program to output multiple results
(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!
Find all posts by this user
Quote this message in a reply
10-26-2020, 12:49 PM
Post: #6
RE: How to write a program to output multiple results
(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]
Find all posts by this user
Quote this message in a reply
10-26-2020, 01:23 PM
Post: #7
RE: How to write a program to output multiple results
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.

Thibault - not collector but in love with the few HP models I own - Also musician : http://walruspark.co
Find all posts by this user
Quote this message in a reply
10-26-2020, 02:51 PM
Post: #8
RE: How to write a program to output multiple results
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

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
10-26-2020, 06:54 PM
Post: #9
RE: How to write a program to output multiple results
(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)));
}
Find all posts by this user
Quote this message in a reply
10-27-2020, 04:55 AM
Post: #10
RE: How to write a program to output multiple results
(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
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)