You can use 128!:2 for this:

   '0 >. 100&-' (128!:2) 3
97

This is what I meant for the function apply to do, but I had the incorrect
meaning for ~  : it only takes names, not any string that evaluates to a
function.

Marshall

On Thu, Aug 18, 2011 at 3:49 PM, Kim Kuen Tang <[email protected]> wrote:

>
> Hi Marshall and everyone here in the forum.
>
> First of all i need to say sorry for not replying to any of the comments
> or email  because i was off line for several days.
> (My wife didnt allow me to go online during the holidays. :-) )
>
> Marshall thanks for your constructive full answers here. I read it
> several times but still dont know how to use it in my existing project.
> So i will try to elaborate my question again with code snippets in q/kdb:
>
> In kdb i have implemented a function called .ql.binbaum that is able to
> take a function (payoff) as argument to calculate a number based on this
> function.
>
> kdb code:
>
> t:([] spot:100;rate:0.01;vola:0.25;matur:1.0;num:2500;payoff:({x};{max
> 0,x-100};{max 0,100-x};{abs 100-x}));t
>
>
>
> Here you can see that i am able to place function in the list.
>
> Now i update the table with the prices calculated from the arguments of
> the table:
>
>
>
> So my question again:
>
> Can i pass a function to another function as argument?
> Using adverb is not an option for me since payoff is also part of the
> argument.
>
> Using
>
> apply =: 4 :'x~ y'
>
> seems also not an option for me, since refering by name is very ugly.
>
> I was not lazy i define the same function also in J:
>
> binbaum=. monad define
> 's0 k r sig t n' =. y
> dt =. t % n
> beta =. (-:@+/@:^) dt*(0 ,*:sig)+(_1 1)*r
> u =. ([+(%:@:(*:-1:))) beta
> d =. %u
> p =. ((^r*dt)-d)%(u-d)
> S =. s0*(u&^*d&^@|.)i.n
> V =. 0 >. k-S NB. This is the place where i want to use the payoff
> q =. 1-p
>
> v=.((q&*@:}:)+(p&*@:}.)) ^: (-.@:(#=1:)) ^:_ V
> (^(-r*t    ))*v
> )
>
> binbaum (100;100;0.01;0.25;1.0;3000)
>
> Here i would like to pass the payoff in the argument.
> Is it possible to pass the function as character?
> Something like ' 0 >. 100-]' ?
>
> If you can give me some hints this would be great.
>
>
> Regards,
>
> Kim
>
> Am 15.08.2011 16:58, schrieb Marshall Lochbaum:
> > 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
> >
>
> ----------------------------------------------------------------------
> 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