Type control
02-22-2015, 03:42 PM (This post was last modified: 02-22-2015 03:56 PM by salvomic.)
Post: #1
 salvomic Senior Member Posts: 1,365 Joined: Jan 2015
Type control
hi,
why the IF ELSE control for type control in this program works, but it doesn't works in a non CAS program?
Code:
 #cas multinom(args):= // multinom(n,{list k_i}) BEGIN local n, k, argv,argc; argv:=[args]; argc:=size(argv); IF argc !=2 THEN return "Input: integer n, {list k}";  ELSE n:=argv(1); k:=argv(2); IF (type(k) != DOM_LIST) THEN return "2nd argument must be a list"; ELSE  n:= ip(n); k:= ip(k);  return  n!/∏LIST(k!); END; // if inner END; // if out END; #end

If I try
Code:
 EXPORT Multinomd(n,k,p) BEGIN ... IF ((type(k) != DOM_LIST) OR (type(k) != DOM_LIST)) THEN return "2nd and 3rd argument must be a list"; ELSE ... END;
type(k) control won't be executed, here.
If I use

Code:
 EXPORT Multinomd(args) BEGIN local argv,argc; argv:=[args]; argc:=size(argv); IF argc !=3 THEN return "Input: integer n, {list k}, {list p}";  ELSE n:=argv(1); k:=argv(2); p:=argv(3); IF ((type(k) != DOM_LIST) OR (type(k) != DOM_LIST)) THEN return "2nd and 3rd argument must be a list"; ELSE ... END;
the program doesn't work at all, giving "Error: Bad argument value" (I think the logical error is in "argv:= [args]"...: why? this approach works well in the #cas / #end version...

I must control in a 3 arguments if 2nd and 3rd are lists, otherwise return to the user the message that they *must* be lists.

Thank you!

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-22-2015, 04:37 PM
Post: #2
 Han Senior Member Posts: 1,811 Joined: Dec 2013
RE: Type control
In CAS, typing: a,b,c,d creates a list. That's just how the CAS environment parses a sequence of objects separated by a comma. So in a CAS program,

f(a,b,c,d)

can be considered as a function that takes 4 separate arguments named a,b,c, and d, or f can be considered as a function that takes a single argument: a list consisting of 4 items.

Graph 3D | QPI | SolveSys
02-22-2015, 04:46 PM (This post was last modified: 02-22-2015 05:36 PM by salvomic.)
Post: #3
 salvomic Senior Member Posts: 1,365 Joined: Jan 2015
RE: Type control
Code:
 EXPORT Multinomd(n, k, p) // Multinomial distribution (n>0,{list k_i}, {list p_i}) BEGIN IF ((type(k) ≠ 6) OR (type(p) ≠ 6)) THEN  return "2nd and 3th argument must be a list"; ELSE n:= ip(n); return  (n!/ΠLIST(k!))*ΠLIST(p^k); END; // if  END;

for now it' almost ok, but I must also control the number of parameters, to avoid the "error: Bad Argument value" if they are less or more than 3.
(in CAS version I could use "IF argc !=3 ...)

Other: why in CAS I could write
IF ((type(k) != DOM_LIST) OR (type(p) != DOM_LIST)) THEN

and in non-CAS it doesn't works?
I must use
IF ((type(k) ≠ 6) OR (type(p) ≠ 6)) THEN

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-22-2015, 08:47 PM
Post: #4
 Joe Horn Senior Member Posts: 1,506 Joined: Dec 2013
RE: Type control
(02-22-2015 04:46 PM)salvomic Wrote:  Other: why in CAS I could write
IF ((type(k) != DOM_LIST) OR (type(p) != DOM_LIST)) THEN

and in non-CAS it doesn't works?
I must use
IF ((type(k) ≠ 6) OR (type(p) ≠ 6)) THEN

The "type" command is different from the "TYPE" command (see their respective help screens in the Toolbox Catalog). Both work correctly in CAS, but Home's parser interprets "type" as "TYPE", which causes the following unexpectedly different results:

CAS: TYPE( {1,2,3} ) --> 6
CAS: Type( {1,2,3} ) --> 6
CAS: type( {1,2,3} ) --> DOM_LIST
CAS: type( {1,2,3} )+0 --> 7

Home: TYPE( {1,2,3} ) --> 6
Home: Type( {1,2,3} ) --> 6
Home: type( {1,2,3} ) --> 6
Home: type( {1,2,3} )+0 --> 6

<0|ɸ|0>
-Joe-
02-22-2015, 08:52 PM
Post: #5
 salvomic Senior Member Posts: 1,365 Joined: Jan 2015
RE: Type control
(02-22-2015 08:47 PM)Joe Horn Wrote:  The "type" command is different from the "TYPE" command (see their respective help screens in the Toolbox Catalog). Both work correctly in CAS, but Home's parser interprets "type" as "TYPE",...

ah, you are right, I didn't remember...
thank you.

Then, there is also difference with ≠ and !=; CAS accepts the second one, Home don't.

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-23-2015, 04:17 AM
Post: #6
 Joe Horn Senior Member Posts: 1,506 Joined: Dec 2013
RE: Type control
(02-22-2015 08:52 PM)salvomic Wrote:  ... there is also difference with ≠ and !=; CAS accepts the second one, Home don't.

A substitute for ≠ that works in both Home and CAS is <>. Handy when using your computer keyboard instead of Prime's keyboard (on which ≠ is available in just three keystrokes: Shift 6 5).

<0|ɸ|0>
-Joe-
02-23-2015, 08:31 AM
Post: #7
 salvomic Senior Member Posts: 1,365 Joined: Jan 2015
RE: Type control
(02-23-2015 04:17 AM)Joe Horn Wrote:  A substitute for ≠ that works in both Home and CAS is <>. Handy when using your computer keyboard instead of Prime's keyboard (on which ≠ is available in just three keystrokes: Shift 6 5).

Yes, in Windows it's a bit tricky to insert special characters. In Mac OS ≠ it would be only ALT 0.

Another symbol to know, writing for the Prime with PC keyboard is the -> that in the Prime is a single char: any hints?

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-23-2015, 04:51 PM
Post: #8
 salvomic Senior Member Posts: 1,365 Joined: Jan 2015
RE: Type control
(02-22-2015 04:37 PM)Han Wrote:  In CAS, typing: a,b,c,d creates a list. That's just how the CAS environment parses a sequence of objects separated by a comma. So in a CAS program,

f(a,b,c,d)

can be considered as a function that takes 4 separate arguments named a,b,c, and d, or f can be considered as a function that takes a single argument: a list consisting of 4 items.

Han,
if I input [a,b], there is a way (in CAS) to tell the program that this input should be a matrix or vector and *not* a list? or other trick to do that?
In my program for Wronskian I would like threat only input like [x, x^2, ...] not also {...} or x,x^2,... if it's possible.

Thank you!

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-23-2015, 04:52 PM
Post: #9
 toml_12953 Senior Member Posts: 1,196 Joined: Dec 2013
RE: Type control
(02-23-2015 08:31 AM)salvomic Wrote:
(02-23-2015 04:17 AM)Joe Horn Wrote:  A substitute for ≠ that works in both Home and CAS is <>. Handy when using your computer keyboard instead of Prime's keyboard (on which ≠ is available in just three keystrokes: Shift 6 5).

Yes, in Windows it's a bit tricky to insert special characters. In Mac OS ≠ it would be only ALT 0.

Another symbol to know, writing for the Prime with PC keyboard is the -> that in the Prime is a single char: any hints?

You could try → Which in Windows is U+2192 or ► which is U+25BA. To type thise in, Hold down the Alt key, the numeric pad + key and type the four-digit hex value. Now release the Alt key. In order for this to work, you have to use RegEdit. Navigate to HKEY_Current_User/Control Panel/Input Method, and set EnableHexNumpad to "1" or add it as a text value if it's not already there.

Tom L
I think therefore I am-Descartes
I think therefore you are-Gorgias
You're not here to think-Army Sergeant
02-23-2015, 05:01 PM
Post: #10
 salvomic Senior Member Posts: 1,365 Joined: Jan 2015
RE: Type control
(02-23-2015 04:52 PM)toml_12953 Wrote:  You could try → Which in Windows is U+2192 or ► which is U+25BA. To type thise in, Hold down the Alt key, the numeric pad + key and type the four-digit hex value. Now release the Alt key. In order for this to work, you have to use RegEdit. Navigate to HKEY_Current_User/Control Panel/Input Method, and set EnableHexNumpad to "1" or add it as a text value if it's not already there.

thank you!
I'll try soon.

Have a nice day

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-23-2015, 05:10 PM
Post: #11
 Han Senior Member Posts: 1,811 Joined: Dec 2013
RE: Type control
(02-23-2015 04:51 PM)salvomic Wrote:
(02-22-2015 04:37 PM)Han Wrote:  In CAS, typing: a,b,c,d creates a list. That's just how the CAS environment parses a sequence of objects separated by a comma. So in a CAS program,

f(a,b,c,d)

can be considered as a function that takes 4 separate arguments named a,b,c, and d, or f can be considered as a function that takes a single argument: a list consisting of 4 items.

Han,
if I input [a,b], there is a way (in CAS) to tell the program that this input should be a matrix or vector and *not* a list? or other trick to do that?
In my program for Wronskian I would like threat only input like [x, x^2, ...] not also {...} or x,x^2,... if it's possible.

Thank you!

If you're trying to do some sort of dynamic input, then your function should be only defined as a function of a single variable (a list). For example:

Code:
#cas myfunc(v):= BEGIN   local s;   s:=SIZE(v);   IF s>4 THEN RETURN("Too many arguments"); END;   CASE     IF s==1 THEN       IF TYPE(v(1))<>4 THEN RETURN("First argument must be vector"); END;       // and so on...     END;     IF s==2 THEN       // etc.     END;   END; END;

Note that in CAS, TYPE() and type() are two different functions. In non-CAS programs, there is only one "type" command -- the non-CAS one. In non-CAS programs, whether you use TYPE() or type(), either one will be interpreted as TYPE(). CASE vs case, RETURN() vs return, are among many differences you will have to learn.

Graph 3D | QPI | SolveSys
02-23-2015, 05:40 PM (This post was last modified: 02-23-2015 05:56 PM by salvomic.)
Post: #12
 salvomic Senior Member Posts: 1,365 Joined: Jan 2015
RE: Type control
(02-23-2015 05:10 PM)Han Wrote:  Note that in CAS, TYPE() and type() are two different functions. In non-CAS programs, there is only one "type" command -- the non-CAS one.
RETURN() vs return,
... are among many differences you will have to learn.

yes, I've still many differences to learn

ok, I'm doing about in that mode.
I've now two little issues to solve:
1. in my specific program the list is type [x, x, x], so I cannot control for v(1), but I must control for the whole v, and it is seen always as list, so the control for <>4 doesn't match (in that program the list is always seen as type 6)...
2. how to avoid (if it's possible) to input nameprogram without () that in my case give a strange (flist)→LOCAL[[mat s j k d ... error?

thanks again!
your help is very precious for me ;-)

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-23-2015, 07:18 PM (This post was last modified: 02-23-2015 07:21 PM by Han.)
Post: #13
 Han Senior Member Posts: 1,811 Joined: Dec 2013
RE: Type control
(02-23-2015 05:40 PM)salvomic Wrote:  1. in my specific program the list is type [x, x, x], so I cannot control for v(1), but I must control for the whole v, and it is seen always as list, so the control for <>4 doesn't match (in that program the list is always seen as type 6)...
2. how to avoid (if it's possible) to input nameprogram without () that in my case give a strange (flist)→LOCAL[[mat s j k d ... error?

1. My example was simply that -- an example. It was not a direct answer to your specific case. You would need to modify the example to make it fit your own needs. I don't understand your need to encase args inside [ and ]. The input args is already a CAS list.

Just try this:
Code:
#cas myexample(args):=   local s;   s:=SIZE(args);   IF s==0 THEN RETURN("No args given"); END;   RETURN(args(s)); END; #end

This will always return the last item in the argument list. The more general case (code below allows myprog(real, list) or myprog(real, list, list):

Code:
#cas myprog(args):= BEGIN   local s:=SIZE(args);   IF s==2 THEN     IF TYPE(args(1)) OR TYPE(args(2))<>6 THEN       RETURN("Usage: myprog(real, list)");     ELSE       // your code; use a RETURN()     END;   END;   IF s==3 THEN     IF TYPE(args(1)) OR TYPE(args(2))<>6 OR TYPE(args(3))<>6 THEN       RETURN("Usage: myprog(real, list, list)");     ELSE       // your code; use a RETURN()     END;   END   RETURN("Invalid input"); END; #end

This doesn't work for non-CAS programs because the Home environment doesn't parse comma-separated entries the same way. The non-CAS programs must either be programmed to take an explicit list of inputs or you must use a CAS program. That is, HOMEPROG(list) where list is an actual list would work for non-CAS programs. However, a CAS program can simply use CASPROG(args).

2. A CAS program is essentially a function, much like f is a function when declared as f(x):=x^2. When you type f, you will see (x)->(x^2) as this is how the CAS represents functions. Thus, by simply typing the name of a function, you return its actual contents. There is no getting around this.

Graph 3D | QPI | SolveSys
02-23-2015, 07:29 PM
Post: #14
 salvomic Senior Member Posts: 1,365 Joined: Jan 2015
RE: Type control
(02-23-2015 07:18 PM)Han Wrote:  1. My example was simply that -- an example. It was not a direct answer to your specific case. You would need to modify the example to make it fit your own needs. I don't understand your need to encase args inside [ and ]. The input args is already a CAS list.
Yes, ok, it is already a CAS list. I thought that it was possible to handle a vector or matrix in different mode than with a list...
Quote:2. A CAS program is essentially a function, much like f is a function when declared as f(x):=x^2. When you type f, you will see (x)->(x^2) as this is how the CAS represents functions. Thus, by simply typing the name of a function, you return its actual contents. There is no getting around this.

ok, thank you also for this clear explanation. In the last version of Wronskian it would be the last heavy "error", I can also leave it: the rest works.

Thanks a lot, Han!

Salvo

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-23-2015, 11:40 PM
Post: #15
 Joe Horn Senior Member Posts: 1,506 Joined: Dec 2013
RE: Type control
(02-23-2015 08:31 AM)salvomic Wrote:  Another symbol to know, writing for the Prime with PC keyboard is the -> that in the Prime is a single char: any hints?

In most Windows software, ALT+26 yields the "→" character. Press and hold down the ALT key, then type 26 on the numeric keypad, then release the ALT key. It works in Word, the Prime Connectivity Kit, and this forum.

Another helpful Windows shortcut: ALT+16 yields the "►" symbol, used for "sto" on Prime.

<0|ɸ|0>
-Joe-
02-24-2015, 06:28 AM
Post: #16
 Angus Member Posts: 212 Joined: Feb 2014
RE: Type control
As I mentioned in an other thread I encoutered problem when using the arrow. Go to CAS and define a function foo1 and foo2 as foo:=(x)->x
Even on the hardware when using the arrow directly with Shift+9 you get an Error message. When typing '-' and '>' (from Shift+6) it works fine, but the arrow is visualized as the built in arrow.

Is there anything I am doing wrong?

Attached File(s) Thumbnail(s)

02-24-2015, 06:34 AM
Post: #17
 salvomic Senior Member Posts: 1,365 Joined: Jan 2015
RE: Type control
(02-23-2015 11:40 PM)Joe Horn Wrote:
(02-23-2015 08:31 AM)salvomic Wrote:  Another symbol to know, writing for the Prime with PC keyboard is the -> that in the Prime is a single char: any hints?

In most Windows software, ALT+26 yields the "→" character. Press and hold down the ALT key, then type 26 on the numeric keypad, then release the ALT key. It works in Word, the Prime Connectivity Kit, and this forum.

Another helpful Windows shortcut: ALT+16 yields the "►" symbol, used for "sto" on Prime.

(02-24-2015 06:28 AM)Angus Wrote:  As I mentioned in an other thread I encoutered problem when using the arrow. Go to CAS and define a function foo1 and foo2 as foo:=(x)->x
Even on the hardware when using the arrow directly with Shift+9 you get an Error message. When typing '-' and '>' (from Shift+6) it works fine, but the arrow is visualized as the built in arrow.

Is there anything I am doing wrong?

thanks both!
I note this things for future reference

Salvo

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-25-2015, 05:52 AM
Post: #18
 Joe Horn Senior Member Posts: 1,506 Joined: Dec 2013
RE: Type control
(02-24-2015 06:28 AM)Angus Wrote:  As I mentioned in an other thread I encoutered problem when using the arrow. Go to CAS and define a function foo1 and foo2 as foo:=(x)->x
Even on the hardware when using the arrow directly with Shift+9 you get an Error message. When typing '-' and '>' (from Shift+6) it works fine, but the arrow is visualized as the built in arrow.

Is there anything I am doing wrong?

It only looks like "→" when Textbook Display mode is turned on (different from Textbook Entry mode). If you turn off Textbook Display mode (Home Settings page 2), you'll see that it's really the two characters ->, not the single character →. So it's just a "pretty print" display fiction. Always type what it really is: "->".

<0|ɸ|0>
-Joe-
03-12-2015, 09:59 AM
Post: #19
 salvomic Senior Member Posts: 1,365 Joined: Jan 2015
RE: Type control
(02-23-2015 07:18 PM)Han Wrote:  Just try this:
Code:
#cas myexample(args):=   local s;   s:=SIZE(args);   IF s==0 THEN RETURN("No args given"); END;   RETURN(args(s)); END; #end
...
This doesn't work for non-CAS programs because the Home environment doesn't parse comma-separated entries the same way. The non-CAS programs must either be programmed to take an explicit list of inputs or you must use a CAS program. That is, HOMEPROG(list) where list is an actual list would work for non-CAS programs. ...

Han,
in some CAS program of mine it works well...

now I've a non CAS program, and this code doesn't work:
Code:
 EXPORT myprog(a); BEGIN local s; s:= SIZE(a); IF (s==0 OR TYPE(a)<>0) THEN RETURN("Input a real number"); END; ...
with myprog (not correct, I know) I get the error "Bad argument count" and with myprog() I get "Error: Bad argument value"

So, there isn't any way to control that input?

Thank you

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
03-13-2015, 03:04 PM
Post: #20
 Han Senior Member Posts: 1,811 Joined: Dec 2013
RE: Type control
(03-12-2015 09:59 AM)salvomic Wrote:
(02-23-2015 07:18 PM)Han Wrote:  Just try this:
Code:
#cas myexample(args):=   local s;   s:=SIZE(args);   IF s==0 THEN RETURN("No args given"); END;   RETURN(args(s)); END; #end
...
This doesn't work for non-CAS programs because the Home environment doesn't parse comma-separated entries the same way. The non-CAS programs must either be programmed to take an explicit list of inputs or you must use a CAS program. That is, HOMEPROG(list) where list is an actual list would work for non-CAS programs. ...

Han,
in some CAS program of mine it works well...

now I've a non CAS program, and this code doesn't work:
Code:
 EXPORT myprog(a); BEGIN local s; s:= SIZE(a); IF (s==0 OR TYPE(a)<>0) THEN RETURN("Input a real number"); END; ...
with myprog (not correct, I know) I get the error "Bad argument count" and with myprog() I get "Error: Bad argument value"

So, there isn't any way to control that input?

Thank you

Ensure there are no conflicts between the CAS version of myprog and the non-CAS version of myprog. In other words, name them differently to make sure that when you type myprog you aren't running the wrong program (since different programs can actually have the same name). As for the error (w/ respect to the second code block in your post), it is being generated much further inside your program. What you have posted there does not produce any errors.

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

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