User:Ramcinfo/fancu staile

From Lojban
Jump to navigation Jump to search

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.

apply

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 goi la pliri'a ge'u ka la jalge la fancu la sumti ce'ai
   la jalge cu me'au la fancu la sumti kei
 function apply(fun, arg) {
   return fun(arg); } 

Now we can use it as such:

 lo me'au la .pliri'a. ku be me'ei tavla bei mi be'o

It could be rewritten in CLL Lojban, but would be (even) less readable:

 cylylypapi'epa jo'au
 lo ka bridi ce'u ce'u ce'o ce'u kei goi ko'a
 zo'u co'e zo'e poi bridi be ko'a bei lo ka ce'u tavla ce'u ku ce'o ke'a ce'o mi

compose

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

 lo goi la finti ge'u ka la jalge la fancu xipa la fancu xire ce'ai
   la jalge cu ka la jalge xipa la sumti ce'ai
     la jalge xipa cu me'au la fancu xipa lo me'au la fancu xire ku be la sumti kei kei
 function compose(fun, fuun) {
   return function (arg) {
     return fun(fuun(arg)); }}

Recursion

Infinite recursion (do not try it at home):

 lo goi la cimnrekursi ge'u ka me'oi la cimnrekursi kei 
 function infRec() {
   infRec();
 }

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

if

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

 lo goi la cuxna ge'u ka la jalge la krinu by boi cy ce'ai
   ge ganai la krinu gi la jalge cu du by
      gi ga la krinu gi la jalge cu du cy kei

Maybe also la gleki's {ifle} will work:

 lo goi la cuxna ge'u ka la jalge la krinu by boi cy ce'ai
   la krinu cu ifle
     lo du'u la jalge cu du by kei
     lo du'u la jalge cu du cy kei kei

Recursion (again)

Now we are ready to build a simple recursion. {krasi} and {fanmo} are used to get first and last elements of sequence, respectively; I am not very sure if this is appropriate. Now if the style is to be developed in the direction of LISP, then functions working on sequences, like head, tail and cons are to be defined.

{ja'ebo goi} is a hack even dirtier than it is usual for these matters; it is used to both a) group the sequence as one sumti to attach relative clause to it and b) to move that clause to the front.

 lo goi la kancu ge'u ka la jalge la porsi la cabna ce'ai
   la jalge cu me'au la cuxna lo du'u la cabna cu fanmo la porsi kei li pa
     lo sumji be li pa lo me'au la kancu ku
       be la porsi bei lo bavla'i be la cabna bei la porsi kei
 je'abo goi la mupli ge'u by ce'o cy ce'o dy zo'u
 li ci cu me'au la kancu la mupli lo krasi be la mupli
 // 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]));

curry

For now, let us pretend that we already have function for tail, being {la selyli'a}. We'll also use {tersu'imei} for getting function (selbri) arity (we could use {lo se pormei be lo se zilbri be lo ka broda}, or above defined {la kancu} instead of {se pormei}).

 lo goi karis buboi ge'u ka result buboi fun buboi ce'ai
 (to be done)        

buboi

In the first version of this draft, {cizra} consctruction {NAME buboi} was used for variable names. It was a hack, but I knew no better way to assign more-or-less meaningful names to assignable variables. Without {buboi}, I'd be up to my neck in {ko'a}s. Luckily, la menli proposed assigning values with {goi} to cmene. Though {goi} behavior is not fully standardized [1], I consisently place name to be assigned after it, and value being assigned before.

aulbuboi.jpg

References