J challenge 
Author Message
 J challenge

A "pons asinorum" for many functional languages is the
(higher-order) function twice:

twice f x = f (f x)
(in a miranda-like syntax).  I.e. twice of a function is the
composition of the function with itself:  if f x = x+1,

twice f 0
2
twice twice f 0
4

It seems easy enough to define such a function in J:

tw =: ^: 2

   >: tw 0
2
   >: tw tw 0
4

But in many functional languages, functions associate to
the left:

twice twice twice f 0 <--> (((twice twice) twice) f) 0

with the result that application is hyper-exponential:

twice twice twice f 0
16
twice twice twice twice f 0
65536

Your mission, should you decide to accept it:
Write a function (or adverb, or whatever) in J
with the behavior of the miranda twice, e.g. so that

   >: tw 0
2
   >: tw tw 0
4
   >: tw tw tw 0
16
   >: tw tw tw tw 0
65536

-j



Mon, 16 Apr 2001 03:00:00 GMT  
 J challenge

Quote:

> A "pons asinorum" for many functional languages is the
> (higher-order) function twice:

> twice f x = f (f x)
> (in a miranda-like syntax).  I.e. twice of a function is the
> composition of the function with itself:  if f x = x+1,

> twice f 0
> 2
> twice twice f 0
> 4

> It seems easy enough to define such a function in J:

> tw =: ^: 2

>    >: tw 0
> 2
>    >: tw tw 0
> 4

> But in many functional languages, functions associate to
> the left:

> twice twice twice f 0 <--> (((twice twice) twice) f) 0

> with the result that application is hyper-exponential:

> twice twice twice f 0
> 16
> twice twice twice twice f 0
> 65536

> Your mission, should you decide to accept it:
> Write a function (or adverb, or whatever) in J
> with the behavior of the miranda twice, e.g. so that

>    >: tw 0
> 2
>    >: tw tw 0
> 4
>    >: tw tw tw 0
> 16
>    >: tw tw tw tw 0
> 65536

> -j

Cute.

I'm unable to find a definition of tw in K s.t. tw(tw(tw(tw f))) is
semantically identical to left-associating twice.  (In K, tw cannot
stand in adverb position, and noun trains are strictly interpreted
as application.)

Otherwise,

        f:1+


        n[1;tw][f]0
      2
        n[2;tw][f]0
      4
        n[3;tw][f]0
      16
        n[4;tw][f]0
      65535

In other words,


      65535

Alternatively,

        n:{x y/y}               / x times y over y
        n[4;tw][f]0
      65535

In other words,

        ((4 tw/tw)f)0
      65534



Mon, 16 Apr 2001 03:00:00 GMT  
 J challenge

Quote:

>       65535
>       65535
>       65535
>       65534

65536 of course.

Once again, typing proves to be the mother of all challenges.



Mon, 16 Apr 2001 03:00:00 GMT  
 J challenge

Quote:
>Your mission, should you decide to accept it:
>Write a function (or adverb, or whatever) in J
>with the behavior of the miranda twice, e.g. so that

>   >: tw 0
>2
>   >: tw tw 0
>4
>   >: tw tw tw 0
>16
>   >: tw tw tw tw 0
>65536

   tw2 =:   2 : 'u. ^: (^/ #/ |. n.)'

allows for a pseudo-exponential (with the zero power for free) :

   (>: tw2 2 0)0
1
   (>: tw2 2 1)0
2
   (>: tw2 2 2)0
4
   (>: tw2 2 3)0
16
   (>: tw2 2 4)0
65536

Close, but it may be that the precedence rules prevent an exact solution.

--
|\/| Randy A MacDonald       |"We ARE the weirdos, mister!"

     BSc(Math) UNBF '83      | APL: If you can say it, it's done.

     I use Real J            | Also http://www.godin.com/godin/
------------------------------------------------<-NTP>----{ gnat }-



Tue, 17 Apr 2001 03:00:00 GMT  
 J challenge

Quote:

> A "pons asinorum" for many functional languages is the
> (higher-order) function twice:

> twice f x = f (f x)
> (in a miranda-like syntax).  I.e. twice of a function is the
> composition of the function with itself:  if f x = x+1,

If composition is first-class, then you should be able to define
the function 'on', such that on[n] produces a function which
applies a monad n times:


        over:{x y/y}

        f:1+
        a:over[4]
        b:on 2
        c:a b
        d:c f
        d 0
     65536

or, without temps,

        ((over[4]on 2)f)0
     65536

How would this be done in Miranda?



Wed, 18 Apr 2001 03:00:00 GMT  
 J challenge
Here is an APL solution to the "twinning" challenge posted by J. Storrs
Hall:

Using the "dynamic function and operator" facility implemented in Dyalog APL
(taking the liberty here to substitute "a" for "alpha" and "w" for "omega"

Define the function "f" which will be "hyperexponentially" applied

     f<is>{w+1}

And define the operator "tw" which will do the applying

     tw<is>{
          x<is>aa
          <execute>((3<times><exponential>/a<reshape>2)<reshape>'aa '),'w'
     }

Then call the operator

           0  f  tw  0
     1
           1  f  tw  0
     2
           2  f  tw  0
     4
           3  f  tw  0
     16
           4  f  tw  0
     65536

Note that a bit of liberty has been taken to make the syntax of the call
appropriate to the syntax rules of APL.

--



Wed, 18 Apr 2001 03:00:00 GMT  
 J challenge

Quote:

> ...

>      tw<is>{
>           x<is>aa
>           <execute>((3<times><exponential>/a<reshape>2)<reshape>'aa
> '),'w'
>      }

> Then call the operator

>            0  f  tw  0
>      1
>            1  f  tw  0
>      2
>            2  f  tw  0
>      4
>            3  f  tw  0
>      16
>            4  f  tw  0
>      65536

I was mildly surprised to realize you can write this in J in completely
tacit form:

   i=. "_
   h=. i # 2:

   f =. ]: ^: g
   >: f 0 (0)
1
   >: f 1 (0)
2
   >: f 2 (0)
4
   >: f 3 (0)
16
   >: f 4 (0)
65536

-j



Thu, 19 Apr 2001 03:00:00 GMT  
 J challenge
In case anyone wanted the definition of a function that works in the
actual syntax I gave, e.g.

   >: tw 0
2
   >: tw tw 0
4
   >: tw tw tw 0
16
   >: tw tw tw tw 0
65536

here it is:

   tw =: 1 : 0
z=. 5!:1 <'x.'
if. (<'^:')-:{.>z
do. z=. < '^:'; < ({.>{:>z),<(,'0');2^>{:>{:>{:>z
else. z=. <'^:'; < z,<(,'0');2
end.
z 5!:0
)

(yes, it's disgusting.  Sorry!  I believe that J syntax
 prohibits a true elegant solution.)
-j



Sat, 21 Apr 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. JS-EAI with *JS*-callback

2. js.exception 3279.js

3. NB. gray.js: a J verb that generates a grayscale postscript image from a 2d array

4. lapackTest.js

5. profile.js

6. J script file profile.js

7. JS valueOf() in RB ?

8. JS for functional programming?

9. JS for functional programming?

10. prefs.js question

11. JS / VRML / HTML / camera binding

12. help: js generated vrml

 

 
Powered by phpBB® Forum Software