If I understand correctly, the problem is
to write a function taking as argument
both functions and data.

Here are two suggestions.

The first is to write the desired function as
 a conjunction, called say, F. Here the left argument
 will be the payoff function and the  right argument is
 the list of numbers spot rate vola matur num

   F =: 2 : 0
'spot rate vola matur num' =. v
payoff =. u
payoff spot * rate + vola - matur % num   NB. for example
)

   (0 >. 100 - ]) F 1 2 3 4 5       NB. applying F to its arguments
95.8

A second possibility is a verb, G say, that takes
a single argument (like your example table t).
Here the argument contains a representation of the payoff function.

   G =: 3 : 0
'spot rate vola matur num p' =. y
payoff =. (< p) (5!:0)          NB. recover function from representation
payoff spot * rate + vola - matur % num
)

It will be useful to define a helper function H.
If the payoff  function is (0 >. 100 -]) then
the representation of the payoff function is the expression (0 >. 100 - ]) H
)

   H =: 1 : '5 !: 1 <''u'''

   G 1;2;3;4;5;(0 >. 100 - ]) H
95.8

----- Original Message ----- 
From: "Kim Kuen Tang" <[email protected]>
To: <[email protected]>
Sent: Thursday, August 18, 2011 8:49 PM
Subject: Re: [Jprogramming] Several questions about j


>
> 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