On Wed, Feb 20, 2013 at 5:27 PM, William Tanksley, Jr <[email protected]
> wrote:

> Raul Miller <[email protected]> wrote:
> > How is treating the "purely functional" case as having an element of
> > unreality the same as the idea that closures (in a stateful context)
> > are purely functional?
>
> If your definitions have "an element of unreality" they are incomplete
> definitions. Unreality is not part of a definition. (Nor is unreality
> the function of the subjunctive.)
>
> If you think closures CAN be used in a functional language, then by
> your definition you think a functional language with closures wouldn't
> allow mutable state, and that's what Boyko said. If you think closures
> cannot be used in a functional language, you haven't said anything to
> hint at it -- unless disagreeing with Boyko was how you hinted at it.
>
> Now, here's an interesting fact... Although some amazing people are
> using J to build dynamic functions, J is a descendant of a language
> called "FP" (invented by Backus) for which building dynamic functions
> was specifically disallowed. I suspect that this is the reason for
> some of J's restrictions on what sort of things you can return from a
> function.


This is how I instinctively approach the generator challenge from a
functional perspective but I am not sure if it counts as closure. People
will no doubt dispute that this approach fits within the rules or counts as
a solution. However, I believe it is more general, more powerful and more
intellectually satisfying, although, perhaps academic.

First, as William points out above, J's design focused on function level as
opposed to functional programming which means functions are not values. But
thankfully J is not purely function level as we have gerunds which allow
you to treat functions as values (ignoring, for the moment, the new
discovery which gives other means
http://www.jsoftware.com/pipermail/programming/2013-January/031260.html).

Now, the idea is we have a function, f, which takes a function, g,  and a
number, x, and returns another function, g', which takes an argument, y,
and whose result is x plus, g applied to y. To use the accumulator call f
on the current accumulator and the value to increment by or call the
accumulator itself on 0 to see the current value or any other integer to
add to the current value. But note that calling the accumulator itself
returns the number and not a new accumulator function. As I say above, I
find this more useful and interesting, others may disagree though.

******** Explicit version ********

NB. Using gerunds to pass around functions
id=: ]`'' NB. identity function
f=: 4 : 0
 (x + y (`:6))`''
)
apply=: `:6 NB. a way to call functions masquerading as values

   1 f id NB. result is a function (as a gerund of course)
┌───────────────┐
│┌─┬───────────┐│
││3│┌─────┬─┬─┐││
││ ││┌─┬─┐│+│]│││
││ │││0│1││ │ │││
││ ││└─┴─┘│ │ │││
││ │└─────┴─┴─┘││
│└─┴───────────┘│
└───────────────┘
   2 f 1 f id
┌─────────────────────────────┐
│┌─┬─────────────────────────┐│
││3│┌─────┬─┬───────────────┐││
││ ││┌─┬─┐│+│┌─┬───────────┐│││
││ │││0│2││ ││3│┌─────┬─┬─┐││││
││ ││└─┴─┘│ ││ ││┌─┬─┐│+│]│││││
││ ││     │ ││ │││0│1││ │ │││││
││ ││     │ ││ ││└─┴─┘│ │ │││││
││ ││     │ ││ │└─────┴─┴─┘││││
││ ││     │ │└─┴───────────┘│││
││ │└─────┴─┴───────────────┘││
│└─┴─────────────────────────┘│
└─────────────────────────────┘
   (2 f 1 f id) apply 0 NB. call the generator function
3
   (2 f 1 f id) apply 10
13

******** Tacit version 1 ********

NB. Using linear representation to pass around functions
f=: (":@[,'+',])
id=: ']' NB. identity function
apply=:128!:2

   1 f id NB. result is a function (as a linear rep. of course)
1+]
   2 f 1 f id
2+1+]
   (2 f 1 f id) apply 0 NB. call the generator function
3
   (2 f 1 f id) apply 10
13

******** Tacit version 2 ********

NB. Back to using gerunds to pass around functions
noun=: [:<(,'0');]
fork=: [:<(<,'3'),<
id=: ''`] NB. identity function
f=: [:fork ],~(<,'+'),~noun@[
apply=: `:6

  1 f id NB. result is a function (as a gerund of course)
┌───────────────┐
│┌─┬───────────┐│
││3│┌─────┬─┬─┐││
││ ││┌─┬─┐│+│]│││
││ │││0│1││ │ │││
││ ││└─┴─┘│ │ │││
││ │└─────┴─┴─┘││
│└─┴───────────┘│
└───────────────┘
   2 f 1 f id
┌─────────────────────────────┐
│┌─┬─────────────────────────┐│
││3│┌─────┬─┬───────────────┐││
││ ││┌─┬─┐│+│┌─┬───────────┐│││
││ │││0│2││ ││3│┌─────┬─┬─┐││││
││ ││└─┴─┘│ ││ ││┌─┬─┐│+│]│││││
││ ││     │ ││ │││0│1││ │ │││││
││ ││     │ ││ ││└─┴─┘│ │ │││││
││ ││     │ ││ │└─────┴─┴─┘││││
││ ││     │ │└─┴───────────┘│││
││ │└─────┴─┴───────────────┘││
│└─┴─────────────────────────┘│
└─────────────────────────────┘
   (2 f 1 f id) apply 0 NB. call the generator function
3
   (2 f 1 f id) apply 10
13

Pepe has been doing much more complicated things like this tacitly for a
long time but it is always a struggle to extract functional programming
style out of J's mostly function level design. 128!:2 opened up an
alternative to gerunds. Another trick is to use adverbs (because they can
take verb arguments) and try to somehow demote the adverbs to verbs. See
his recent post:
http://www.jsoftware.com/pipermail/programming/2013-February/031673.html


> J has an interesting path ahead of it now! We can balance
> things out so that functions can return functions, in which case we
> can argue about closures; or we can take J back down the FP road,
> without closures. (Or we can stay on the same path we're on. I hope we
> do either of the three choices with our eyes open.)
>

Now that we see there _may_ be ways to allow J to do functional programming
more naturally (for quick reference:
http://www.jsoftware.com/pipermail/programming/2013-January/031260.html),
new solutions to this problem can come about by directly allowing verbs to
take verbs and return verbs. Which takes us down the FP road William
mentions below. As encouragement down this path I propose a challenge (to
be done with eyes open!):

Implement the verb f tacitly, without using gerunds or linear
representation. In other words, you should no longer need to define an
'apply':

 (2 f 1 f id) apply 10

could instead be

 (2 f 1 f id) 10

if f could truly take verbs as arguments and return them as results.


> In FP, they call a verb a "function" and an adverb a "functional".
>
> No, that has nothing to do with our discussion. Except one thing:
> closures are impossible in a function-level language.
>
> > Raul
>
> -Wm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to