如何计算给定动名词的 f1(f2(...fn(x)...))?
How to calculate f1(f2(...fn(x)...)) given a gerund?
可以将f1(f2(f3(x)))
计算为
*: +: >: 4 NB. 100
给定 "unknown" 长度的动名词(例如 m1 =: *:`+:`>:
或 m2 =: +:`>:
)如何计算 f1(f2(...(fn(x)) ...))
?
到目前为止,我只能使用 m `: 0
来 evoke 动名词。
(*:`+:`>: `: 0) 4 NB. 16 8 5
这个解决方案并不优雅,但它确实有效。
首先使用 Foreign Conjunctions Define (5!:0
) 副词和 Atomic Representation (5!:1
) 将动名词转换为字符串形式,然后使用 Raze (;
) 拆箱。
然后使用默认格式 (":
) 将 y 参数转换为字符串,并在前面加上一个空格以将 space 赋给动名词字符串。
使用 Append (,
) 创建一个字符串并将 Do (".
) 应用到该字符串以获得结果。
g=: 4 : 0
s=: ; (5!:1 <'t'[t=.x) 5!:0 NB. changes gerunds to string
a=:' ' , ": y NB. makes argument into a string prefixed by blank
". s,a
)
或在一行中
g1=: 4 : ' ". (; (5!:1 <''t''[t=.x) 5!:0 ), '' '' , ": y'
*:` +:` >: g 4
100
*:` +:` >: g1 4
100
设:
NB. an example gerund
] g=: *:`<:`+:`>:
┌──┬──┬──┬──┐
│*:│<:│+:│>:│
└──┴──┴──┴──┘
NB. a sentence to evoke
*: <: +: >: 4
81
第一种方法(通过动态规划)
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
优点:没有
缺点:
- 输入数据类型应该是准确的(既不是浮点数也不是复数)因为格式 (
":
) 可能会被截断
- 应用于输入的动词只能是 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
优点:
- 允许输入任何数据类型
缺点:
- 应用于输入的动词只能是 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
优点:
- 允许输入任何数据类型
- 应用于输入的动词可以是任何价(单价、二价或双价)
缺点:没有
2B。将动名词转换为传送带
通过 At (@:
) 以某种方式连接组成动词。生成的动词将是组成动词的链式序列。
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
优点:
- 允许输入任何数据类型
- 应用于输入的动词可以是任何价(单价、二价或双价)
缺点:没有
备注:
a2b1a 可以简化为结构略有不同但功能相同的链:
a2b1b=: 1 : '(''@:'' arconj/ m) 5!:0'
arconj 实用程序可以手动重新实现为 [1]:
arconj2=: (<'@:') ,@<@, <@,
a2b1c=: 1 : '({. arconj2/ m) 5!:0'
或[1]:
arconj3=: 4 : '(x`:6)@(y`:6)`'''''
a2b1d=: 1 : '({. arconj3/ m) 5!:0'
2B2。通过火车 (`:6
)
加入组成动词,然后应用 Train (`: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
优点:
- 允许输入任何数据类型
- 应用于输入的动词可以是任何价(单价、二价或双价)
- 最简单的版本
缺点:没有
备注
J Forums 是询问您是否希望更快得到答复的最佳地点。
参考资料
[1]: [Jprogramming] Gerund composed application
作者:劳尔·米勒 (Raul Miller),2017-09-25
[2]:System/Interpreter/Requests#verb pipelines 作者:Dan Bron,2007-12-21
可以将f1(f2(f3(x)))
计算为
*: +: >: 4 NB. 100
给定 "unknown" 长度的动名词(例如 m1 =: *:`+:`>:
或 m2 =: +:`>:
)如何计算 f1(f2(...(fn(x)) ...))
?
到目前为止,我只能使用 m `: 0
来 evoke 动名词。
(*:`+:`>: `: 0) 4 NB. 16 8 5
这个解决方案并不优雅,但它确实有效。
首先使用 Foreign Conjunctions Define (5!:0
) 副词和 Atomic Representation (5!:1
) 将动名词转换为字符串形式,然后使用 Raze (;
) 拆箱。
然后使用默认格式 (":
) 将 y 参数转换为字符串,并在前面加上一个空格以将 space 赋给动名词字符串。
使用 Append (,
) 创建一个字符串并将 Do (".
) 应用到该字符串以获得结果。
g=: 4 : 0
s=: ; (5!:1 <'t'[t=.x) 5!:0 NB. changes gerunds to string
a=:' ' , ": y NB. makes argument into a string prefixed by blank
". s,a
)
或在一行中
g1=: 4 : ' ". (; (5!:1 <''t''[t=.x) 5!:0 ), '' '' , ": y'
*:` +:` >: g 4
100
*:` +:` >: g1 4
100
设:
NB. an example gerund
] g=: *:`<:`+:`>:
┌──┬──┬──┬──┐
│*:│<:│+:│>:│
└──┴──┴──┴──┘
NB. a sentence to evoke
*: <: +: >: 4
81
第一种方法(通过动态规划)
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
优点:没有
缺点:
- 输入数据类型应该是准确的(既不是浮点数也不是复数)因为格式 (
":
) 可能会被截断 - 应用于输入的动词只能是 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
优点:
- 允许输入任何数据类型
缺点:
- 应用于输入的动词只能是 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
优点:
- 允许输入任何数据类型
- 应用于输入的动词可以是任何价(单价、二价或双价)
缺点:没有
2B。将动名词转换为传送带
通过 At (@:
) 以某种方式连接组成动词。生成的动词将是组成动词的链式序列。
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
优点:
- 允许输入任何数据类型
- 应用于输入的动词可以是任何价(单价、二价或双价)
缺点:没有
备注:
a2b1a 可以简化为结构略有不同但功能相同的链:
a2b1b=: 1 : '(''@:'' arconj/ m) 5!:0'
arconj 实用程序可以手动重新实现为 [1]:
arconj2=: (<'@:') ,@<@, <@, a2b1c=: 1 : '({. arconj2/ m) 5!:0'
或[1]:
arconj3=: 4 : '(x`:6)@(y`:6)`''''' a2b1d=: 1 : '({. arconj3/ m) 5!:0'
2B2。通过火车 (`:6
)
加入组成动词,然后应用 Train (`: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
优点:
- 允许输入任何数据类型
- 应用于输入的动词可以是任何价(单价、二价或双价)
- 最简单的版本
缺点:没有
备注
J Forums 是询问您是否希望更快得到答复的最佳地点。
参考资料
[1]: [Jprogramming] Gerund composed application 作者:劳尔·米勒 (Raul Miller),2017-09-25
[2]:System/Interpreter/Requests#verb pipelines 作者:Dan Bron,2007-12-21