CAS questions: IF...THEN and length() function
12-01-2015, 09:36 PM
Post: #1
 Nigel (UK) Senior Member Posts: 374 Joined: Dec 2013
CAS questions: IF...THEN and length() function
I've been writing some simple CAS programs in CAS mode and I've encountered a few issues. The first is to do with return:
Code:
#cas rtest(x):= BEGIN IF (x<0) THEN return -1; END; return 1; END; #end
I'd expect this to return -1 if x<0 and 1 otherwise, but it always returns 1. It isn't hard to work around this (using ELSE), but is there something that I'm missing here?

The second issue (rather obscure) is to do with the length() function. length("abc") returns 3, which is fine. length(x+2=y) (note: expressions in XCAS seem to be stored as lists) returns 2, although it has three elements.

More generally: does anyone know where I can find lots of CAS programs written for the Prime? I like to learn a programming language by looking at programs that other people have already written!

Nigel (UK)
12-01-2015, 10:00 PM (This post was last modified: 12-01-2015 10:08 PM by Han.)
Post: #2
 Han Senior Member Posts: 1,842 Joined: Dec 2013
RE: CAS questions: IF...THEN and length() function
(12-01-2015 09:36 PM)Nigel (UK) Wrote:  I've been writing some simple CAS programs in CAS mode and I've encountered a few issues. The first is to do with return:
Code:
#cas rtest(x):= BEGIN IF (x<0) THEN return -1; END; return 1; END; #end
I'd expect this to return -1 if x<0 and 1 otherwise, but it always returns 1. It isn't hard to work around this (using ELSE), but is there something that I'm missing here?

The second issue (rather obscure) is to do with the length() function. length("abc") returns 3, which is fine. length(x+2=y) (note: expressions in XCAS seem to be stored as lists) returns 2, although it has three elements.

More generally: does anyone know where I can find lots of CAS programs written for the Prime? I like to learn a programming language by looking at programs that other people have already written!

Nigel (UK)

Using upper-case RETURN() will do what you want. I remember running into this a long time ago and return() in lower-case only works properly if it is the last statement of the program (the actual "return" is likely due to the ending of the program, though, and not the command itself). I had to make this change in my SVD program listing. I do not know if this ever got reported as a bug, though.

Is length counting an equality as 2 since there are two sides of an equation? I'm not sure how you get 3 elements from x+2=y

There are several cas programs in the HP Prime library. (Joe Horn has written a few, I have one that computes the SVD of a matrix and a few other linear algebra routines, and I am sure you can find more if you look).

Graph 3D | QPI | SolveSys
12-01-2015, 10:15 PM
Post: #3
 Nigel (UK) Senior Member Posts: 374 Joined: Dec 2013
RE: CAS questions: IF...THEN and length() function
Thank you for the RETURN() information; I guess I should have thought of trying uppercase!

So far as length(x+2=y) is concerned, I know that there are three elements because (x+2=y)[1] returns '=', (x+2=y)[2] returns x+2, and (x+2=y)[3] returns y. x+2 is then another three-element list: '+', x, and 2. I was just curious about the reason for a length of 2 being reported. If it is a bug, it's not too important.

Thank you for the program library suggestion. Your SVD program is exactly the type of thing I was looking for. It will be studied!

Nigel (UK)
12-01-2015, 10:24 PM
Post: #4
 Han Senior Member Posts: 1,842 Joined: Dec 2013
RE: CAS questions: IF...THEN and length() function
(12-01-2015 10:15 PM)Nigel (UK) Wrote:  Thank you for the RETURN() information; I guess I should have thought of trying uppercase!

So far as length(x+2=y) is concerned, I know that there are three elements because (x+2=y)[1] returns '=', (x+2=y)[2] returns x+2, and (x+2=y)[3] returns y. x+2 is then another three-element list: '+', x, and 2. I was just curious about the reason for a length of 2 being reported. If it is a bug, it's not too important.

Thank you for the program library suggestion. Your SVD program is exactly the type of thing I was looking for. It will be studied!

Nigel (UK)

Just a fair warning, though: I did not insert as many comments as a good programmer would have.

Graph 3D | QPI | SolveSys
12-02-2015, 01:54 PM
Post: #5
 parisse Senior Member Posts: 1,136 Joined: Dec 2013
RE: CAS questions: IF...THEN and length() function
Indeed, return should be recognized like RETURN.
Expressions are coded as trees. size will return the number of arguments : try size(sin(x)) or size(x+y+z), while op(a) will return the sequence of arguments of the root-node function/operator of a.
12-02-2015, 02:01 PM
Post: #6
 Han Senior Member Posts: 1,842 Joined: Dec 2013
RE: CAS questions: IF...THEN and length() function
(12-02-2015 01:54 PM)parisse Wrote:  Indeed, return should be recognized like RETURN.
Expressions are coded as trees. size will return the number of arguments : try size(sin(x)) or size(x+y+z), while op(a) will return the sequence of arguments of the root-node function/operator of a.

The current behavior of return() is that it actually does not "return" to the parent function.

Graph 3D | QPI | SolveSys
 « Next Oldest | Next Newest »

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