Post Reply 
functions with surprising behaviour/fonctions au comportement surprenant
07-16-2022, 09:53 AM (This post was last modified: 07-16-2022 10:48 AM by Tyann.)
Post: #1
functions with surprising behaviour/fonctions au comportement surprenant
Bonjour,

Je me suis aperçu que quelque fonctions de programmation avait un comportement auquel
je ne m'attendais pas.
1) CHAR et ASC :
CHAR(0) renvoie "", mais ASC("") provoque une erreur ??? alors que j'attendais 0.
2) DIM
DIM sur une valeur numérique renvoie 1 alors que j'attendais une erreur 'type d'argument'.

En avez vous vu d'autres ?

Hello,

I noticed that some programming functions had a behaviour I didn't expect.
I was not expecting.
1) CHAR and ASC :
CHAR(0) returns "", but ASC("") causes an error when I expected 0.
2) DIM
DIM on a numeric value returns 1 when I was expecting an 'argument type' error.

Have you seen others?

Sorry for my english
Find all posts by this user
Quote this message in a reply
07-16-2022, 02:17 PM
Post: #2
RE: functions with surprising behaviour/fonctions au comportement surprenant
Quote: CHAR(0) returns "", but ASC("") causes an error when I expected 0.

Lower cased char, asc work

CAS> asc(char(0))      → {0}

BTW, char(0) actually produced a 1 char string "\0", not ""

CAS> len(char(0))      → 1
CAS> asc("\0")           → {0}

Quote:DIM on a numeric value returns 1 when I was expecting an 'argument type' error.

DIM on a list produced a list.
Perhaps DIM on an atom return an atom is correct behavior?
Find all posts by this user
Quote this message in a reply
07-17-2022, 03:35 PM (This post was last modified: 07-17-2022 03:36 PM by Tyann.)
Post: #3
RE: functions with surprising behaviour/fonctions au comportement surprenant
Bonjour Albert Chan

Effectivement CHAR(0) renvoie bien "\0", bien que dans l'écran d'accueil cela affiche ""
si on rappelle ce résultat on retrouve sur la ligne de commande "\0".
Pour DIM, je pensais que cette fonction était exclusivement dédiée aux chaînes de caractères,
puisque l'on dispose de SIZE pour les listes, matrices et vecteurs.
Cependant d'après l'aide intégrée, DIM fonctionne également avec les matrices et vecteurs
et comme SIZE renvoie 1 pour un réel.
Bien dommage, j'espérai pouvoir déclenché une erreur dans une fonction avec DIM en cas de
paramètres <> d'une chaîne sans avoir à testé le type.
Merci pour vos éclaircissements.


Hello Albert Chan

CHAR(0) does indeed return "\0", although in the home screen it displays "".
if we call back this result we find "\0" on the command line.
For DIM, I thought that this function was exclusively dedicated to strings,
since SIZE is available for lists, matrices and vectors.
However, according to the built-in help, DIM also works with matrices and vectors
and as SIZE returns 1 for a real.
Too bad, I was hoping to be able to trigger an error in a function with DIM in case of
parameters <> of a string without having to test the type.
Thank you for your clarification.

Translated with http://www.DeepL.com/Translator (free version)

Sorry for my english
Find all posts by this user
Quote this message in a reply
07-17-2022, 03:59 PM
Post: #4
RE: functions with surprising behaviour/fonctions au comportement surprenant
Du coup Voici ce que j'utiliserai pour mes besoins.

So this is what I will use for my needs.

Code:

EXPORT CDIM(s)
BEGIN
 IFTE(TYPE(s)==6,MAKELIST(cdim(s(I)),I,1,SIZE(s)),cdim(s));
END;
cdim(s)
BEGIN
 IFTE(TYPE(s)==2,DIM(s),UPPER(1));
END;

UPPER(1) déclenchera une erreur type d'argument incorrect.

UPPER(1) will trigger an incorrect argument type error.

Sorry for my english
Find all posts by this user
Quote this message in a reply
07-21-2022, 11:01 AM
Post: #5
RE: functions with surprising behaviour/fonctions au comportement surprenant
Bonjour

Petit problème, DIM accepte des sous listes comme beaucoup d'autres fonctions d'ailleurs :
DIM({"abc",{"cd","eee"}) --> {3,{2,3}}

Voici donc une nouvelle version de CDIM :


Hello

Small problem, DIM accepts sublists like many other functions:
DIM({"abc",{"cd", "eee"}) --> {3,{2,3}}

So here is a new version of CDIM:
Code:
EXPORT CDIM(s)
BEGIN
 LOCAL f:=(TYPE(s)=6),i,r:={};
 IF f THEN
  FOR i FROM 1 TO SIZE(s) DO
   r(0):=CDIM(s(i));
  END; 
 ELSE
  RETURN IFTE(TYPE(s)==2,DIM(s),UPPER(1));
 END;
END;

Sorry for my english
Find all posts by this user
Quote this message in a reply
Post Reply 




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