Here are the full answers to your questions:
Verbs are not first-class objects in the sense that they cannot be used as
arguments or return values to other verbs. The reason is that if this were
the case, it would be impossible to tell when to invoke verbs or when to use
them as arguments; should
f g h
be interpreted as (f (g h)), two monadic applications, or (f g h), a single
dyadic application? I consider this one of the weaknesses of J.
However, there are a number of ways of getting around this problem. The
first is to use adverbs and conjunctions, which can take verbs as arguments
and return verbs. However, this is only a partial solution as adverbs and
conjunctions cannot take each other as arguments or outputs.
The second is to refer to verbs by name, using a string. This is the
approach taken by plot. Utilities for this form are 5!:5, which takes a
boxed name and returns a string, and ~ , an adverb which takes a string and
returns a verb. So, to make a verb "apply," which takes a string on the left
and a noun on the right and applies the verb to the noun, you could write
apply =: 4 :'x~ y'
The third is to use gerunds, which are J's way of turning a verb into a
noun. f`g`h will produce a list of boxes, each of which is the "atomic
representation" of f, g, or h. This is similar to a list of verbs; if you
wanted to apply them you could use (f`g`h) `: 0 , which produces a list of
the outputs of f, g, and h. You could also make `:0 a verb:
apply =: 4 :'x`:0 y'
allowing you to control it precisely using rank.
A dictionary data type is not supported. However, you could make a
dictionary conjunction which takes two lists and returns a dictionary
function:
dict =: 2 :'n {~ m&i.'
1 2 3 dict 7 8 9
7 8 9 {~ 1 2 3&i.
(1 2 3 dict 7 8 9) 2
8
(1 2 3 dict 7 8 9) 1.5
|index error
| (1 2 3 dict 7 8 9)1.5
Marshall
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Raul Miller
Sent: Monday, August 15, 2011 7:10 AM
To: Programming forum
Subject: Re: [Jprogramming] Several questions about j
On Mon, Aug 15, 2011 at 5:08 AM, Kim Kuen Tang <[email protected]> wrote:
> Are verbs ( dyad or monad) first-class citizen in j?
The answer to this is "no" in the same sense that objects are not
first-class citizens in any object oriented language.
> Is it possible to forward a verb to another verb?
Yes.
> Is it possible to box a verb into list ?
Yes.
> Is it possible to have a dictionary like the case in kdb? Something
> like : (`a`b`c)!(1 2 3)
Yes, but not exactly.
Typically, in J, locales are used for this purpose. But locales are not
values and can only be referred to (by name).
You would have to implement a replacement for this use of ! as a user
defined verb.
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm