The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

HP Prime: Lists in programs
Message #1 Posted by Alberto Candel on 2 Dec 2013, 10:32 a.m.

Hi,

What is the way to input a list to a program? I wrote a program (ProgA) that uses a list to do some arithmetic with its items. But if I input the list as {1,2,3,4} (say), it does not like it. Thus I modified the program so that it will read the list from the list L1. This works, but it brings another set of problems. If I create L1 using another program (ProgB) and after that I run Prog A, this ProgA may or may not read the list L1 correctly (the list is correct as I can confirm with [Shift]+[F7]). It is as if the list was there but not available to programs yet. If I power off after ProgB is run and then ON and run ProgA, it seems to work OK. Sorry if I am confusing...

      
Re: HP Prime: Lists in programs
Message #2 Posted by cyrille de Brébisson on 3 Dec 2013, 1:37 a.m.,
in response to message #1 by Alberto Candel

Hello,

could you post your programs so that we could try them and see what is happening?

in a program, you can create a variable, put an empty list in it and use EDITLIST(var) to edit the list.

Cyrille

      
Re: HP Prime: Lists in programs
Message #3 Posted by Patrice on 3 Dec 2013, 3:04 a.m.,
in response to message #1 by Alberto Candel

show your program code !

            
Re: HP Prime: Lists in programs
Message #4 Posted by Alberto Candel on 3 Dec 2013, 11:51 a.m.,
in response to message #3 by Patrice

Hi, here they are:

ProgramA takes a list of integers and finds the rational number whose continued fraction is that list. I have the list stored in L1. I would like to input the list as {n1, n2, ...} but I cannot.

EXPORT ProgramA()
BEGIN
LOCAL M:=[[1,0],[0,1]];
FOR I FROM 1 TO SIZE(L1) DO
M:=M*EXPR("[["+L1(I)+",1],[1,0]]");
END
PRINT(exact(M(1,1)/M(2,1));
END;

ProgramB inputs a rational number P/Q and outputs its continued fraction expansion (and writes it to list L1 also). I have to input the number as P and then Q; if I input P/Q the Prime makes that into a decimal, and the program sometimes does not work. Also, in 5th line, "not0" is for the "slashed equal" sign.

EXPORT ProgramB(P,Q)
BEGIN
LOCAL M,N:={};
M:=EXPR("[[1,0,"+P+"],[0,1,"+Q+"]]");
WHILE M(2,3) not0 DO
N:=CONCAT(N,{FLOOR(M(1,3)/M(2,3))});
M:=EXPR("[[0,1],[1,"+-FLOOR(M(1,3)/M(2,3))+")*M;
END;
L1:=N;
PRINT(N);
END;

(I have just copied these rather that posting pics. There may be some typo.) (BTW, and earlier version would not work with iPart instead of FLOOR).

If I run ProgramB on a number P/Q and then run ProgramA, this one has trouble reading list L1. I have not seen any consistent error to figure out what is wrong. If I run the Debug feature in ProgramA, most of the time works OK, but sometimes it does not, showing that list L1 has some strange items in it, even when L1 looks OK using [Shift]+[7]

Thanks!

[added] In programB, third line from the bottom, I have N->L1; instead of L1:=N. I just do not have that "store" symbol on my keyboard) [/added]

Edited: 3 Dec 2013, 2:44 p.m. after one or more responses were posted

                  
Re: HP Prime: Lists in programs
Message #5 Posted by Alberto Candel on 3 Dec 2013, 12:36 p.m.,
in response to message #4 by Alberto Candel

BTW, ContinuedFraction does not appear on my Prime, though it exists on Xcas.

            
Re: HP Prime: Lists in programs
Message #6 Posted by Alberto Candel on 3 Dec 2013, 2:43 p.m.,
in response to message #3 by Patrice

Hi again, I have just upgraded to firmware 20131125, tested these programs, and it appears that the issue with ProgramA reading list L1 correctly seems to have been resolved.

Thanks

                  
Re: HP Prime: Lists in programs
Message #7 Posted by Alberto Candel on 3 Dec 2013, 4:00 p.m.,
in response to message #6 by Alberto Candel

And matrices now work in programs! That is great, thanks

                        
Re: HP Prime: Lists in programs
Message #8 Posted by Alberto Candel on 4 Dec 2013, 2:16 a.m.,
in response to message #7 by Alberto Candel

Hi again,

Program B now works like this:

EXPORT ProgramB(P,Q)
BEGIN
LOCAL M,N:={};
M:=[[1,0,P],[0,1,Q]]);
WHILE M(2,3) not0 DO
N:=CONCAT(N,{FLOOR(M(1,3)/M(2,3))});
M:=[[0,1],[1,-FLOOR(M(1,3)/M(2,3))]]*M;
END;
L1:=N;
PRINT(N);
END;

I would like to be able to input the rational number R as P/Q, instead as inputing P and then Q. I replaced the 1st and 4th lines of the program with

EXPORT ProgramB(R)
.........
M:=[[1,0,numer(R)],[0,1,denom(R)]];
The program runs OK but gives the wrong answer. It seems that the input makes the number into a decimal and then numer and denom cannot reconstruct it correctly (try 256/157)

Also, the Help for numer in the Prime has several typos. Usage is numer(a/b) and not numer(a,b)

Thanks!


[ Return to Index | Top of Index ]

Go back to the main exhibit hall