给定动名词,如何计算 f1(f2(...fn(x)...))?

Mic*_*ann 5 j

一个可以计算f1(f2(f3(x)))

*: +: >: 4                 NB. 100
Run Code Online (Sandbox Code Playgroud)

给定一个“未知”长度的动名词(例如m1 =: *:`+:`>:,或m2 =: +:`>:)如何计算f1(f2(...(fn(x)) ...))

到目前为止,我只能够唤起使用动名词m `: 0

(*:`+:`>: `: 0) 4         NB. 16 8 5
Run Code Online (Sandbox Code Playgroud)

Igo*_*lov 5

让:

   NB. an example gerund
   ] g=: *:`<:`+:`>:
?????????????
?*:?<:?+:?>:?
?????????????
   NB. a sentence to evoke
   *: <: +: >: 4
81
Run Code Online (Sandbox Code Playgroud)

第一种方法(通过动态规划)

1A。写下整个句子然后唤起它

将动名词中的所有元素转换为线性表示(LR),组成句子,然后唤起它:

   NB. utility to find out a LR from an atomic
   NB. representation (AR) and parenthesize it
   NB. pLR=. plr AR
   plr=: 3 : 0
v=. y 5!:0
'(' , (5!:5 < 'v') , ') '
)
   NB. result=. gerund v1 input
   v1=: 4 : '". (; <@plr"0 x) , ": y'
   NB. evoke v1 with a gerund and input applied
   g v1 4
81
Run Code Online (Sandbox Code Playgroud)

优点:没有

缺点:

  1. 输入数据类型应该是精确的(既不是浮点数也不是复数)因为 Format ( ":) 可能会截断
  2. 应用于输入的动词只能是 monad

1B。依次唤起成分动词

从动名词中取出当前元素,将其转换为动词,将该动词应用于累加器,对动名词的下一个元素重复:

   NB. result=. gerund v2 input
   v2=: >@(4 : '(x 5!:0)&.> y'/)@(, <)
   NB. evoke v2 with a gerund and input applied
   g v2 4
81
Run Code Online (Sandbox Code Playgroud)

优点:

  1. 允许输入的任何数据类型

缺点:

  1. 应用于输入的动词只能是 monad

第二种方法(功能)

2A。将动名词转换为 fork

   NB. a fork we are going to compose, in action
   ([: *: [: <: [: +: >:) 4
81
   NB. its structure
   ([: *: [: <: [: +: >:)
??????????????????????????
?[:?*:????????????????????
?  ?  ??[:?<:?????????????
?  ?  ??  ?  ??[:?+:?>:???
?  ?  ??  ?  ?????????????
?  ?  ????????????????????
??????????????????????????
   NB. how to compose it
   ('[:' ; g)@.0 1 0 2 0 3 4
??????????????????????????
?[:?*:????????????????????
?  ?  ??[:?<:?????????????
?  ?  ??  ?  ??[:?+:?>:???
?  ?  ??  ?  ?????????????
?  ?  ????????????????????
??????????????????????????
   NB. selector (ISO generator)
   NB. iso=. sel gerund
   sel=: (< < < _2) { (0 ,@,. #\)
   NB. let's test it
   sel g
0 1 0 2 0 3 4
   NB. sentence to assemble a fork
   ('[:' ; g)@.(sel g)
??????????????????????????
?[:?*:????????????????????
?  ?  ??[:?<:?????????????
?  ?  ??  ?  ??[:?+:?>:???
?  ?  ??  ?  ?????????????
?  ?  ????????????????????
??????????????????????????
   NB. an adverb executing that sentence
   NB. fork=. gerund a2a
   a2a=: 1 : '(''[:'' ; m)@.(sel m)'
   NB. evoke that adverb to transform a gerund to a fork
   g a2a
??????????????????????????
?[:?*:????????????????????
?  ?  ??[:?<:?????????????
?  ?  ??  ?  ??[:?+:?>:???
?  ?  ??  ?  ?????????????
?  ?  ????????????????????
??????????????????????????
   NB. apply a fork produced to input
   g a2a 4
81
Run Code Online (Sandbox Code Playgroud)

优点:

  1. 允许输入的任何数据类型
  2. 应用于输入的动词可以是任何价态(monad、dyad 或 ambivalent)

缺点:没有

2B。将动名词转换为传送带

@:以某种方式通过 At ( )连接成分动词。结果动词将是组成动词的 At-chained 序列。

2B1. 通过 arconj 实用程序

连接成分动词组成AR,然后通过Define ( 5!:0)将AR 转换为动词。

   NB. a chain we are going to compose, in action
   *:@:<:@:+:@:>: 4
81
   NB. its structure
   *:@:<:@:+:@:>:
??????????????????????????
????????????????????@:?>:?
?????????????@:?+:??  ?  ?
???*:?@:?<:??  ?  ??  ?  ?
?????????????  ?  ??  ?  ?
????????????????????  ?  ?
??????????????????????????
   NB. its AR
   chain=. *:@:<:@:+:@:>:
   5!:1 < 'chain'
??????????????????????????????????
??????????????????????????????????
??@:??????????????????????????????
??  ?????????????????????????>:???
??  ???@:????????????????????  ???
??  ???  ???????????????+:???  ???
??  ???  ???@:??????????  ???  ???
??  ???  ???  ??*:?<:???  ???  ???
??  ???  ???  ??????????  ???  ???
??  ???  ???????????????  ???  ???
??  ???  ????????????????????  ???
??  ?????????????????????????  ???
??  ??????????????????????????????
??????????????????????????????????
??????????????????????????????????
   NB. we'll compose it with AR utility (arconj) from addon
   load 'misc/miscutils/langexten'
   NB. an adverb assembling a chain
   NB. chain=. gerund a2b1a
   a2b1a=: 1 : '(''@:'' arconj~/ |. m) 5!:0'
   NB. evoke that adverb to transform a gerund to chain
   g a2b1a
??????????????????????????
????????????????????@:?>:?
?????????????@:?+:??  ?  ?
???*:?@:?<:??  ?  ??  ?  ?
?????????????  ?  ??  ?  ?
????????????????????  ?  ?
??????????????????????????
   NB. apply a chain produced to input
   g a2b1a 4
81
Run Code Online (Sandbox Code Playgroud)

优点:

  1. 允许输入的任何数据类型
  2. 应用于输入的动词可以是任何价态(monad、dyad 或 ambivalent)

缺点:没有

笔记:

  1. 可以简化 a2b1a 以生成结构略有不同但功能相同的链:

    a2b1b=: 1 : '(''@:'' arconj/ m) 5!:0'
    
    Run Code Online (Sandbox Code Playgroud)
  2. arconj 实用程序可以手动重新实现 [1]:

    arconj2=: (<'@:') ,@<@, <@,
    a2b1c=: 1 : '({. arconj2/ m) 5!:0'
    
    Run Code Online (Sandbox Code Playgroud)

    或作为 [1]:

    arconj3=: 4 : '(x`:6)@(y`:6)`'''''
    a2b1d=: 1 : '({. arconj3/ m) 5!:0'
    
    Run Code Online (Sandbox Code Playgroud)

2B2。乘火车 ( `:6)

加入成分动词,然后应用训练 ( `:6) [2]。

   NB. a chain we are going to compose, in action
   *:@:<:@:+:@:>: 4
81
   NB. its structure
   *:@:<:@:+:@:>:
??????????????????????????
????????????????????@:?>:?
?????????????@:?+:??  ?  ?
???*:?@:?<:??  ?  ??  ?  ?
?????????????  ?  ??  ?  ?
????????????????????  ?  ?
??????????????????????????
   NB. an adverb assembling a chain
   NB. chain=. gerund a2b2
   a2b2=: 1 : '(}: , m ,. <''@:'')`:6'
   NB. evoke that adverb to transform a gerund to chain
   g a2b2
??????????????????????????
????????????????????@:?>:?
?????????????@:?+:??  ?  ?
???*:?@:?<:??  ?  ??  ?  ?
?????????????  ?  ??  ?  ?
????????????????????  ?  ?
??????????????????????????
   NB. apply a chain produced to input
   g a2b2 4
81
Run Code Online (Sandbox Code Playgroud)

优点:

  1. 允许输入的任何数据类型
  2. 应用于输入的动词可以是任何价态(monad、dyad 或 ambivalent)
  3. 最简单的版本

缺点:没有

笔记

J 论坛是询问您是否希望更快获得答案的最佳场所。

参考

[1]:[Jprogramming] Gerund 编写的应用程序 ,Raul Miller,2017-09-25

[2]:系统/解释器/请求#verb pipelines by Dan Bron,2007-12-21