User:Ramcinfo/fancu staile: Difference between revisions

From Lojban
Jump to navigation Jump to search
No edit summary
No edit summary
Line 33: Line 33:


   lo ka me'oi .inf bu rec buboi kei goi .inf bu rec buboi
   lo ka me'oi .inf bu rec buboi kei goi .inf bu rec buboi
 
   function infRec() {
   function infRec() {
     infRec();
     infRec();
Line 51: Line 51:
       lo sumji be li pa lo me'au len buboi
       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 bubui
         be list buboi bei lo bavla'i be .item buboi bei list buboi kei goi len bubui
  function len(list, item) {
    return (list.indexOf(item) == array.length - 1) ? 0 :
      1 + len(list, list[list.indexOf(item)] + 1); }

Revision as of 20:22, 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 function taking a function as argument and returning other function. 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 no
     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 bubui
 function len(list, item) {
   return (list.indexOf(item) == array.length - 1) ? 0 :
      1 + len(list, list[list.indexOf(item)] + 1); }