User:Ramcinfo/fancu staile: Difference between revisions

From Lojban
Jump to navigation Jump to search
Line 89: Line 89:


  lo goi la karis. ge'u ka la jalge la fancu ce'oi
  lo goi la karis. ge'u ka la jalge la fancu ce'oi
   lo goi la karis.xipa ge'u ka la jalge xipa la fancu la terbri ce'oi
   lo goi la karis.xipa ge'u ka la jalge xipa la fancu xipa la terbri ce'oi
     la jalge xipa ka la jalge xire la sumti ce'oi
     la jalge xipa ka la jalge xire la sumti ce'oi
       lo goi la terbri xipa ge'u poi'i ke'a me'au la zbasu me'u la sumti la terbri kei  zo'u
       lo goi la terbri xipa ge'u poi'i ke'a me'au la zbasu me'u la sumti la terbri kei  zo'u
       la jalge cu me'au la cuxna me'u
       la jalge xire cu me'au la cuxna me'u
         lo du'u lo se tersu'imei be la fancu cu dubdu'i lo se pormei be la terbri xipa kei
         lo du'u lo se tersu'imei be la fancu xipa cu dubdu'i lo se pormei be la terbri xipa kei
         lo poi'i ke'a bridi la fancu la terbri xipa kei
         lo poi'i ke'a bridi la fancu xipa la terbri xipa kei
         lo poi'i ke'a me'au la karis.xipa me'u la terbri xipa kei  kei  kei  zo'u
         lo poi'i ke'a me'au la karis.xipa me'u la terbri xipa kei  kei  kei  zo'u
   la jalge cu me'au la karis.xipa me'u la fancu lo kutpoi  kei
   la jalge cu me'au la karis.xipa me'u la fancu lo kutpoi  kei

Revision as of 21:37, 25 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.

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 me'u 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 cons, being {la zbasu} (x1 is sequence with head x2 and tail sequence x3. 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 la karis. ge'u ka la jalge la fancu ce'oi
  lo goi la karis.xipa ge'u ka la jalge xipa la fancu xipa la terbri ce'oi
    la jalge xipa ka la jalge xire la sumti ce'oi
      lo goi la terbri xipa ge'u poi'i ke'a me'au la zbasu me'u la sumti la terbri kei  zo'u
      la jalge xire cu me'au la cuxna me'u
        lo du'u lo se tersu'imei be la fancu xipa cu dubdu'i lo se pormei be la terbri xipa kei
        lo poi'i ke'a bridi la fancu xipa la terbri xipa kei
        lo poi'i ke'a me'au la karis.xipa me'u la terbri xipa kei  kei  kei  zo'u
  la jalge cu me'au la karis.xipa me'u la fancu lo kutpoi  kei
function curry(fun) {
  function curry1(fun, args) {
    return function (arg) {
        var args1 = args;
        args1.unshift(arg);
        if (fun.length == args1.length) {
           return fun(args1)
        } else {
           return curry1(fun, args1);
        }
    }
  }
  return curry1(fun, []);
}

Usage:

   la fanxu xinu cu me'au la karis. me'u la fancu
.i la fanxu xipu cu me'au la fancu xinu me'u li pu
.i la fanxu xire cu me'au la fancu xipu me'u li re
.i la jalge cu me'au la fanxu xire me'u li ci
var curriedFun = curry(fun);
var curriedFun1 = curriedFun(1);
var curriedFun2 = curriedFun(2);
var result = curriedFun(3);

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