Is there a function to convert an integer to a list of its digits?
n=4711 s_digit = string(n)-> n_digit = char(string(n))-48
in witch …
- string(n) converts n to a string, similar “”<<n
- char splits a string to a list of ASCII-Codes
- s_digit is a list of strings “4”, “7”, “1”, “1” and n_digit is a list of number 4,7,1,1.
again: string(n) = ""<<n = "4711"
This is IMMENSELY helpful! Is there any way I can insure that all my questions get to YOU? (You’ve petted a stray cat.)
thank you for the nice answer. I am new to mathstud.io but plan to check the discussion forum regularly. If I see a question from you, I will do my best. Best regards, Robert
I need some help understanding how to create and use (call) subroutines with both input and output arguments. I think it involves scripts, include statements, and compile statements, but I don’t seem to be able to find clear examples in the documentation. Want to have a go?
With appreciation & thanks,
you can define functions by “@functionname” within a file and call dem by “functionname” in the same file. There is no need to compile them. This usage is explained in the tutorial section by Shakey (Share/Tutorials/Beginner Tutorial Variables and Functions). I think that is easy to understand.
I will try to explain what it is with the compiled functions by way of example:
Suppose you miss a few functions in the Mathstudio commands. Suppose these are the functions “Convert number to a list of digits” and “Reverse the order of digits in a number”. You want to use these function in many cases without having to reprogram them every time. In this case, you have to proceed as follows:
Create a new file with e.g. the name “MyTools”. This file
contains the two functions:
L = char(string(n))-48
@NumReverse(Number n) L=NumToList(n) r=0 loop(i,Size(L),1,-1) r=r+L(i)*10^(i-1) end Return(r)
In both cases, the input value is a number ("
Number n"). In the first case, the output value is a list ("
Return (L)") and in the second case the output value is a Number ("
Return (r)"). The second function uses the first function. Both functions can be tested within the file “MyTools” e.g. by enter:
NumToList (4711) NumReverse (4711)
You can also do more complex functions with multiple inputs and outputs by writing:
@AnyFunction (a, b, c, d, e) ... ... do anything with a, b, c, d, e ... and compute x, y, z ... Return (x, y, z)
In the concrete example, the story goes on as follows:
Save the file “MyTools” with the two tested functions. This
is the source code and the file is called “MyTools.math”.
This source code is compiled as follows:
Mathstudio Menu / File / Evaluate all
Mathstudio Menu / File / Export scripts to include folder
File Icon / Compile scripts
In both cases, a so-called compiled file with name “MyTools.script” is generated (located in the “include folder” in case of Mac and in the same directory as MyTools.math in case of iOS).
Close the file “MyTools.math” and you are ready!
If you want to use the new functions in another file, then you proceed as follows:
a) Create any file, e.g. “Testfile”
b) At the beginning of this file you write:
c) After pressing “return” the function names “
d) These functions you can now use in “Testfile” like normal
e) For example:
NumToList (1234) or
So this is a elegant way, to expand Mathstudio to your own needs.
If you want to change the functions “NumToList” or “NumReverse” or want to add more features / other functions, then you have to make these changes in the source code “MyTools.math” and after that
- Save MyTools.math
- recompile again
Hope this helps …
I can’t thank you enough for your elaborate reply to my appeal for help!
I have an Android phone, and I’m eager to follow your examples and see how (and whether) they work.