Post Reply 
CAS questions: IF...THEN and length() function
12-01-2015, 10:00 PM (This post was last modified: 12-01-2015 10:08 PM by Han.)
Post: #2
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
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: CAS questions: IF...THEN and length() function - Han - 12-01-2015 10:00 PM



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