可以计算f1(f2(f3(x)))
作为
*: +: >: 4 NB. 100
给定一个“未知”长度的动名词(例如
m1 =: *:`+:`>:
或 m2 =: +:`>:
)如何计算 f1(f2(...(fn(x)) ...))
?到目前为止我只能evoke使用
m `: 0
的动名词.(*:`+:`>: `: 0) 4 NB. 16 8 5
最佳答案
让:
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
优点:没有
缺点:
":
)可能会截断 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
优点:
缺点:
第二种方法(功能)
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 (
@:
) 加入组件动词。结果动词将是组成动词的 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
优点:
缺点:没有
笔记:
a2b1b=: 1 : '(''@:'' arconj/ m) 5!:0'
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
作者:劳尔·米勒,2017-09-25
[2]:System/Interpreter/Requests#verb pipelines丹·布朗,2007-12-21
关于j - 给定动名词,如何计算 f1(f2(...fn(x)...))?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59319625/