HP Forums
using lambda functions (they've been found) - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: using lambda functions (they've been found) (/thread-4875.html)



using lambda functions (they've been found) - ji3m - 10-04-2015 04:46 PM

Since my previous post looking for lambdas things have improved.

We do have access to true lambda functions which can be generated and passed around at runtime.
Certaintly with #cas <> #cas functions.

But some care is required when using ppl code and lambdas.

Below is code with lambda examples and comments.
You can copy and paste it or download the attached text file(same stuff).

//BELOW ARE TWO HELPER FUNCTINS:
//NEEDED SINCE #CAS PGMS DONT STO WELL TO LISTS

EXPORT putl(L,N,V)
BEGIN
L(N) := exact(V);//PPL IS EVAL CRAZY
END;

//FOR USE IN PPL TO "COMPILE" LAMBDA STRINGS
//CAS HATES COMMENTS SO ADD THE DUMMY BELOW
LOCAL DUMMY1 ;

#cas
myexpr(S):=
BEGIN
return expr(S);
END;
#end

#cas
lopl(f,l1,l2) :=
BEGIN
LOCAL K,Q := {};
FOR K FROM 1 TO SIZE(l1) DO
Q := putl(Q,K,f(l1(K),l2(K)));
END;
Q;
END;
#end

//THE ABOVE CAS PGM APPLIES THE LAMBDA FUN f
// TO THE TWO LISTS AND RETURNS LIST Q

LOCAL DUMMY2;

//SIMPLE BINARY LAMBDAS (NO ";" AFTER END!)

EXPORT lamdiv := "(A,B)->BEGIN A/B; END";
EXPORT lamadd := "(A,B)->BEGIN A+B; END";



//YOU CAN COMPILE THE LAM STRINGS AS CAS VARS

// clamdiv := expr(lamdiv);

// AND CALL A CAS FUN LIKE THIS

// lopl(clamdiv,{1,2},{3,5}) -> {1/3,2/5}

//THIS WORKS

EXPORT trymediv(L1,L2)
BEGIN
LOCAL F := myexpr(lamdiv);
lopl(F,L1,L2);
END;

//BUT THIS WILL CRASH AND BURN
EXPORT crash(f,L1,L2)
BEGIN
lopl(f,L1,L2);
END;

// IF YOU ENTER crash(clamdiv,{2,5,6},{7,8,9})

//PPL SEEMS TO DISLIKE RAW LAMBDAS


//BUT THIS DOES WORK
EXPORT nocrash(fstr,L1,L2)
BEGIN
lopl(myexpr(fstr),L1,L2);
END;

//WHEN YOU PASS ANY LEGIT LAMBDA STRING
//WHY myexpr? BECAUSE expr COMPILES WRONG IN PPL
//AND myexpr IS #cas.

// try
// nocrash("(W,Z)->BEGIN 2*W-Z END",{1},{9})

//OF COURSE YOU CAN GEN ANY LAM AT RUNTIME


RE: using lambda functions (they've been found) - ji3m - 10-09-2015 06:41 PM

Is it just me that didnt know about this?
Or does nobody care?

Seems like pretty powerful stuff, passing functions around.