User:Ramcinfo/fancu staile: Difference between revisions

From Lojban
Jump to navigation Jump to search
No edit summary
No edit summary
Line 9: Line 9:
{loka}- and {me'ei}-abstractions, then, are predication-returning functions as first-class values. {me'au} with these abstractions is function application; {kai'u} is immediate function application.
{loka}- and {me'ei}-abstractions, then, are predication-returning functions as first-class values. {me'au} with these abstractions is function application; {kai'u} is immediate function application.


We are now ready to construct function taking a function as argument and returning other function. Let's start with ''apply'' (for unary function):
We are now ready to construct functions taking functions as arguments and/or returning other functions. Let's start with ''apply'' (for unary function):


   lo ka result buboi fun buboi .arg buboi ce'ai
   lo ka result buboi fun buboi .arg buboi ce'ai

Revision as of 21:20, 23 February 2015

Let us think about selbri as function taking its terbri as arguments and returning the predication, or bridi. This predication can be then claimed in context, returning truth-value of resulting bridi in that context.

 function tavla(speaker, listener, subject, language) {...} // returns predication
 function claim(predication, context) {...} // returns boolean
 claim(tavla("me", "you", "lojban", "lojban"), context);

To build functions returning other things, we can use {lo}. Let us not go into intricacies of descriptors and pretend what {lo selbri} returns simply some value s1 such that claim(selbri(s1, s2...sx), context) will be true for some values of s2...sx and claimed with some context. Arguments other than context could be restricted with {be...bei...be'o}. So {lo selbri be sumti2 bei sumti3 be'o} is x1 = selbri1(sumti2, sumti3).

{loka}- and {me'ei}-abstractions, then, are predication-returning functions as first-class values. {me'au} with these abstractions is function application; {kai'u} is immediate function application.

We are now ready to construct functions taking functions as arguments and/or returning other functions. Let's start with apply (for unary function):

 lo ka result buboi fun buboi .arg buboi ce'ai
   result buboi me'au fun buboi .arg buboi kei goi .apli buboi
 function apply(fun, arg) {
   return fun(arg); } 

Now we can use it as such:

 lo me'au .apli buboi be me'ei tavla bei mi be'o

Function composition, compose (for unary functions, still):

 lo ka result buboi fun buboi fu'un buboi ce'ai
   result buboi ka resu'ult buboi .arg buboi ce'ai
     resu'ult buboi me'au fun buboi lo fu'un buboi be .arg buboi kei kei goi compose buboi
 function compose(fun, fuun) {
   return function (arg) {
     return fun(fuun(arg)); }}

Infinite recursion (do not try it at home):

 lo ka me'oi .inf bu rec buboi kei goi .inf bu rec buboi
 function infRec() {
   infRec();
 }

I am not sure of the last one. Maybe {goi} works only forward in scope. {me'oi ka nei} (or simply {nei}) will do almost the same, I guess.

Confitional (taken from Tsani’s recursive predicate definition, replacing {ckaji} with {du}):

 lo ka result buboi condition buboi by boi cy boi ce'ai
   ge ganai condition buboi gi result buboi du dy boi
      gi ga condition buboi gi result buboi du cy boi kei goi .if buboi


Now we are ready to build a simple recursion:

 lo ka result buboi list buboi .item buboi ce'ai
   result buboi me'au .if buboi lo du'u .item buboi cu fanmo list buboi kei li pa
     lo sumji be li pa lo me'au len buboi
       be list buboi bei lo bavla'i be .item buboi bei list buboi kei goi len buboi
 by ce'o cy ce'o dy boi goi sample buboi zo'u
 li ci cu me'au len buboi sample buboi lo krasi be sample buboi
 // approximately
 function len(list, item) {
   return (list.indexOf(item) === list.length - 1) ? 1 :
      1 + len(list, list[list.indexOf(item)] + 1); }
 var sample = ["b", "c", "d"];
 assert(3 === len(sample, sample[0]));
aulbuboi.jpg