User:Ramcinfo/fancu staile: Difference between revisions

From Lojban
Jump to navigation Jump to search
No edit summary
No edit summary
Line 13: Line 13:
We are now ready to construct functions taking functions as arguments and/or returning other functions. 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 goi la .pliri'a. ge'u ka la .jalge. la fancu la .sumti. ce'ai
   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
     la jalge cu me'au la fancu la sumti kei


   function apply(fun, arg) {
   function apply(fun, arg) {
Line 31: Line 31:


Function composition, ''compose'' (for unary functions, still):
Function composition, ''compose'' (for unary functions, still):
   lo goi la finti ge'u ka la jalge la fancu xipa la fancu xire ce'ai
   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 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
       la jalge xipa cu me'au la fancu xipa lo me'au la fancu xire ku be la sumti kei kei
Line 43: Line 43:
Infinite recursion (do not try it at home):
Infinite recursion (do not try it at home):


   lo goi <u>.inf bu rec buboi</u> ge'u ka me'oi <u>.inf bu rec buboi</u> kei  
   lo goi la cimnrekursi ge'u ka me'oi la cimnrekursi kei  


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


   lo goi <u>.if buboi</u> ge'u ka <u>result buboi</u> <u>condition buboi</u> <u>by boi</u> <u>cy boi</u> ce'ai
   lo goi la cuxna ge'u ka la jalge la krinu by boi cy ce'ai
     ge ganai <u>condition buboi</u> gi <u>result buboi</u> du <u>by boi</u>
     ge ganai la krinu gi la jalge cu du by
       gi ga <u>condition buboi</u> gi <u>result buboi</u> du <u>cy boi</u> kei
       gi ga la krinu gi la jalge cu du cy kei


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


   lo goi <u>.if buboi</u> ge'u ka <u>result buboi</u> <u>condition buboi</u> <u>by boi</u> <u>cy boi</u> ce'ai
   lo goi la cuxna ge'u ka la jalge la krinu by boi cy ce'ai
     <u>condition buboi</u> ifle
     la krinu cu ifle
       lo du'u <u>result buboi</u> du <u>by boi</u> kei
       lo du'u la jalge cu du by kei
       lo du'u <u>result buboi</u> du <u>cy boi</u> kei
       lo du'u la jalge cu du cy kei kei
 
== Recursion (again) ==
== Recursion (again) ==


Line 71: Line 72:
{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.
{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 <u>len buboi</u> ge'u ka <u>result buboi</u> <u>list buboi</u> <u>.item buboi</u> ce'ai
   lo goi la kancu ge'u ka la jalge la porsi la cabna ce'ai
     <u>result buboi</u> me'au <u>.if buboi</u> lo du'u <u>.item buboi</u> cu fanmo <u>list buboi</u> kei li pa
     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 <u>len buboi</u>
       lo sumji be li pa lo me'au la kancu ku
         be <u>list buboi</u> bei lo bavla'i be <u>.item buboi</u> bei <u>list buboi</u> kei
         be la porsi bei lo bavla'i be la cabna bei la porsi kei
   je'abo goi <u>sample buboi</u> ge'u by ce'o cy ce'o dy zo'u
   je'abo goi la mupli ge'u by ce'o cy ce'o dy zo'u
   li ci cu me'au <u>len buboi</u> <u>sample buboi</u> lo krasi be <u>sample buboi</u>
   li ci cu me'au la kancu la mupli lo krasi be la mupli


   // approximately
   // approximately
Line 86: Line 87:


== curry ==
== curry ==
For now, let us pretend that we already have function for ''tail'', being {<u>rest buboi</u>}. 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 {<u>len buboi</u>} instead of {se pormei}).
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 <u>karis buboi</u> ge'u ka <u>result buboi</u> <u>fun buboi</u> ce'ai
   lo goi <u>karis buboi</u> ge'u ka <u>result buboi</u> <u>fun buboi</u> ce'ai

Revision as of 12:12, 24 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.

Through this draft, {cizra} consctruction {NAME buboi} is used for variable names. It is a hack, but I know 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. {boi} terminator could be elided in many places, but was left for consistency.

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'oi 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)        
    
aulbuboi.jpg