Post Reply 
Create function by program and store as variable ?
09-25-2015, 11:13 PM (This post was last modified: 09-25-2015 11:15 PM by xset.)
Post: #1
Create function by program and store as variable ?
I've tried to define function in EVAL(EXPR(...)) environment.
Something like:
LOCAL fnstr:="MyFn(x) BEGIN PRINT(x); END;";
LOCAL fn:=EVAL(EXPR(fnstr));
fn(33);


All I got was declaration and printing of local variable: EVAL(EXPR("LOCAL mvar:=33; PRINT(mvar);")) in non-CAS program.

In any mode it doesn't accept lambda expressions or any syntax of function declaration. EVAL returns "Error: Unmatch control word"

But this worked in CAS command line:
(x)->BEGIN PRINT(x); END; works in CAS mode and resulted in function creation (x)->[ PRINT(x); ] which really prints argument when called!

Is any way to parse string with code and create function during execution of program ?

Thanks in advance
XSet
Find all posts by this user
Quote this message in a reply
09-26-2015, 08:54 AM
Post: #2
RE: Create function by program and store as variable ?
ToDevelopers:

If you allow to parse arbitrary PPL code and store it as functions (could be unnamed lambdas) to variables, I'd be almost happy. It does not violate any exam mode related stuff, but make us free to create! This feature was in RPL, but PPL though allows short lambdas does not allow to parse and return function as object :-(

Thanks
XSet
Find all posts by this user
Quote this message in a reply
09-26-2015, 03:14 PM
Post: #3
RE: Create function by program and store as variable ?
What is the object you did create called.?
How do you execute it?

BTW how do you know about what you did do. I cant find any
Advanced docs about ppl which mention any of this.

Does PPL mimic some othe language everyone
knows about?

Ive been experimenting for days and learned
a bunch and finding scraps of info. Is there a better way?

Some things ive learned the hard way:
If a routine fun () is defined then

eval(fun); is same as fun(); in compiled code.

I use this to make jump (call) vectors for very fast case like routines.

I also learned that the compiler treats user variables as names and does lookup at runtime which is very slow.
So compiled variable must be declared in the pgm.
User (really cas) variables act like userrpl objects we just cant seem to store program objects.

Also s:= Notes ("name") gets string into code so i can maybe add an editing to my pgm.

You all probably know all this.

BTW im trying to make an app which emulates sysRPL and maybe useRPLl.
Ive had to simulate the rpn stack but its coming along well.

One last question: if i override the enter key can i invoke the function the enter key default is?
I.e. can i call enter () or somesuch?

I work with android Prime only for now on my
Galaxy Tab Pro/Note 2
Find all posts by this user
Quote this message in a reply
09-26-2015, 04:25 PM (This post was last modified: 09-26-2015 04:28 PM by xset.)
Post: #4
RE: Create function by program and store as variable ?
(09-26-2015 03:14 PM)ji3m Wrote:  What is the object you did create called.?
How do you execute it?

I've put code in string and made EVAL(EXPR(str))
I also was able to create program object in CAS window.
(x)->BEGIN PRINT(x); END;
Ans->MyFun
Eval(MyFun(5)) gives 5 to terminal.

and store it to my variable. It does not work in programming mode (????).

(09-26-2015 03:14 PM)ji3m Wrote:  BTW how do you know about what you did do. I cant find any
Advanced docs about ppl which mention any of this.

It contained PRINT which left a trail in the terminal.

(09-26-2015 03:14 PM)ji3m Wrote:  Does PPL mimic some othe language everyone
knows about?

It definitely contains some properties of functional languages.

For other questions better ask HP developers.
Find all posts by this user
Quote this message in a reply
09-26-2015, 04:41 PM (This post was last modified: 09-26-2015 06:19 PM by xset.)
Post: #5
RE: Create function by program and store as variable ?
When i do it in CAS programming mode i can't force to execute EXPR and EVAL, they just become symbolic names.

I've found that one can see rewritten body of CAS function translated in something like XCas syntax ??? I suspect these code executed by CAS engine.

Found undocumented shit:
program(x,0,x)
gives function x->x

also works in CAS window expr("program(x,0,x)")
gives functional object which you can store in variable.
I also found undocumented mysterious functions bloc(expr1,expr2) which allow group expressions within functional declaration.
Find all posts by this user
Quote this message in a reply
09-26-2015, 07:49 PM
Post: #6
RE: Create function by program and store as variable ?
I managed to create the code from string (passed from non-CAS environment) and could call it from non-CAS function.

#cas
mkfun(str):=
BEGIN
fn:=expr(str); // only global var works
END;
#end

EXPORT MKIT()
BEGIN
LOCAL fnstr:="(x,y)->BEGIN
IF x>2 THEN
RETURN(7)
END ;
RETURN(x);
END;";

CAS(mkfun(fnstr));
RETURN CAS.fn(32,1);
END;

MKIT() will return 7

I need something more convenient Sad
Find all posts by this user
Quote this message in a reply
09-26-2015, 08:15 PM (This post was last modified: 09-26-2015 08:23 PM by xset.)
Post: #7
RE: Create function by program and store as variable ?
Thats funny I just found function AProgram which allows me send whole program as text to application :-)
A can even store them in files using AFiles and put to program when needed. I am almost happy.

OMG there are also Programs variable!!! I am thinking to buy second HP Prime, it gives me enough freedom i need from calculator.
Find all posts by this user
Quote this message in a reply
09-26-2015, 11:44 PM
Post: #8
RE: Create function by program and store as variable ?
So how is it used? And where did you find it?

I work with android Prime only for now on my
Galaxy Tab Pro/Note 2
Find all posts by this user
Quote this message in a reply
09-27-2015, 07:34 AM (This post was last modified: 09-27-2015 02:47 PM by xset.)
Post: #9
RE: Create function by program and store as variable ?
(09-26-2015 11:44 PM)ji3m Wrote:  So how is it used? And where did you find it?

Just look manual or on-calculator docs on Home variable "Programs". You can edit and store program of applications using variable "AProgram". No need to use Notes.
Find all posts by this user
Quote this message in a reply
09-27-2015, 09:46 AM
Post: #10
RE: Create function by program and store as variable ?
(09-26-2015 11:44 PM)ji3m Wrote:  So how is it used? And where did you find it?
It's not a bad idea, to read the manuals ... enjoy the time
Find all posts by this user
Quote this message in a reply
09-28-2015, 06:36 AM
Post: #11
RE: Create function by program and store as variable ?
Hello,

At this point in time, HPPPL does not let you do lambda functions and the like.

Althrough function are internally objects like expressions and numbers, there are no user way to manipulate them at this point.

One of the issues with functions is: what is a RCL, what is an evaluation?

If func contains a function, does doing func evaluate the function or does it recals it?
it's easy to make the difference when func has some parameters, but much less when it does not (remember empty () are optional in PPL).

One thing that you can do at the moment is programaticaly create a program (Programs("name"):= string) and then use the functions there...

but that is about it.

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
09-28-2015, 04:24 PM (This post was last modified: 09-28-2015 05:34 PM by xset.)
Post: #12
RE: Create function by program and store as variable ?
(09-28-2015 06:36 AM)cyrille de brébisson Wrote:  At this point in time, HPPPL does not let you do lambda functions and the like.

I can't parse that. It is not true. You can use lambda functions in MAKELIST and other functions, you can save them in variables and call them (at least in CAS mode). Look at documentation for the "apply" function, there said first argument is Var-f(Var) - in fact lambda function with arity=1.

XSet
Find all posts by this user
Quote this message in a reply
09-29-2015, 06:37 AM
Post: #13
RE: Create function by program and store as variable ?
Hello,

It depends on your definition of a Lambda function.

But first, I was talking about PPL, NOT the CAS programming which is, in some ways more powerful, and in other more frustrating (the 2 languages do not have the same target applications/aim after all)...

A lambda function is generally seen as a sequence of statements declared in a statement and passed as parameter to a function which will do the evaluation.

In PPL, you do have calls like MAKELIST which to take an EXPRESSION as an input and evaluate them. This is, in some way a very simple type of lambda function. You can even have that statement call a program function for more complex evaluation.
BUT you can not do:
MAKELIST((I) BEGIN Complex algo here that takes a lot of lines RETURN result END 1, 10, 10);

which would be a full blown lambda function.

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
09-29-2015, 07:47 PM
Post: #14
RE: Create function by program and store as variable ?
(09-29-2015 06:37 AM)cyrille de brébisson Wrote:  BUT you can not do:
MAKELIST((I) BEGIN Complex algo here that takes a lot of lines RETURN result END 1, 10, 10);

which would be a full blown lambda function.

Cyrille

Give me some time, i am sure i can :-)
Find all posts by this user
Quote this message in a reply
09-30-2015, 05:37 AM (This post was last modified: 09-30-2015 05:39 AM by cyrille de brébisson.)
Post: #15
RE: Create function by program and store as variable ?
Hello

> Give me some time, i am sure i can :-)

Ho, I am sure you can.
This is one of the perks of creating 'stuff', you get to see what people can think/succeed in doing with it. And it is often astounding!

One of my favorite sentence (and I mean by that a sentence that I love finding myself saying) is: "My stuff can do THAT!? How is that even possible!"

It's kind of finding that you made something transcendental. Something that is going above and beyond what you made.

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
09-30-2015, 07:45 AM
Post: #16
RE: Create function by program and store as variable ?
(09-30-2015 05:37 AM)cyrille de brébisson Wrote:  My stuff can do THAT!? How is that even possible!

:-)) Because it is really powerful stuff. My opinion on Prime is quite changed in good direction when I touched mysterious Xcas stuff and number of HP functions including graphics (there are things to hack) :-) Only thing which makes me sad is impossibility to add some features by myself. For example we can not make programmable clock alarm and override build-in programs, but I hope they will add it in future releases. I'll try to make life more comfortable on my side, writing some tools.
Find all posts by this user
Quote this message in a reply
Post Reply 




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