APL中的条件函数 [英] Conditional function in APL
问题描述
在任何APL方言中,是否有条件函数的符号或着名成语?
我确定我错过了某些东西,因为这是一个基本的语言元素。在其他语言中,它被称为条件运算符,但我会在这里避免使用这个术语,因为APL运算符是完全不同的东西。
例如C朋友有 x? T:F
LISP有
(如果x TF)
Python有
T if x else F
等等。
我知道现代APL有:如果
和朋友,但它们是控制程序流的命令语句:它们不返回值,不能在表达式内使用,当然不能应用于布尔数组。他们有完全不同的目的,这对我来说很好。
唯一能够做出功能选择的体面表达是 (FT)[⎕IO+ x]
,它看起来并不是特别简短或可读的,尽管它完成了工作,甚至在数组上也是如此:
('no''yes')[⎕IO+(⍳5)∘。>(⍳5)]
no no no no no
是否否否否否
是是否否否
是是是否否
是是是是否
我试图用类似于⌷
的类似表达式,但在布尔数组上失败了。即使我可以,它仍然需要嵌入⎕IO
或硬编码1,就可读性而言甚至更糟。
在我继续并定义自己的 if
并将其用于每一个我将永远写下的程序之前,有没有关于此的任何规范?我缺少一个明显的函数或操作符?
(是否有任何APL程序员在SO?上: - )
是的,这里有APL程序员(但不是很多!)。
我认为答案是没有标准。
对于标量解决方案,我使用pick:
$ b $ / code>
虽然对于布尔数组,我使用上面的索引:
ft [x]
我总是使用索引原点零,所以不需要添加1,并且不需要parens。
如果这些不够简单,我想你必须用一个名为if的函数来覆盖它们。这也可以让你把真假放在tf的自然排序中。
Is there a symbol or well-known idiom for the conditional function, in any of the APL dialects?
I'm sure I'm missing something, because it's such a basic language element. In other languages it's called conditional operator, but I will avoid that term here, because an APL operator is something else entirely.
For example C and friends have x ? T : F
LISPs have (if x T F)
Python has T if x else F
and so on.
I know modern APLs have :If
and friends, but they are imperative statements to control program flow: they don't return a value, cannot be used inside an expression and certainly cannot be applied to arrays of booleans. They have a different purpose altogether, which is just fine by me.
The only decent expression I could come up with to do a functional selection is (F T)[⎕IO+x]
, which doesn't look particularly shorthand or readable to me, although it gets the job done, even on arrays:
('no' 'yes')[⎕IO+(⍳5)∘.>(⍳5)]
no no no no no
yes no no no no
yes yes no no no
yes yes yes no no
yes yes yes yes no
I tried to come up with a similar expression using squad ⌷
, but failed miserably on arrays of booleans. Even if I could, it would still have to embed ⎕IO
or an hardcoded 1, which is even worse as far as readability is concerned.
Before I go ahead and define my own if
and use it on every program I will ever write, is there any canon on this? Am I missing an obvious function or operator?
(Are there any APL programmers on SO? :-)
Yes, there are APL programmers on SO (but not many!).
I think the answer is that there is no standard on this.
For a scalar solution, I use "pick":
x⊃f t
While for a Boolean array I use indexing as you do above:
f t[x]
I always use index origin zero, so there is no need to add 1, and the parens are not needed.
If these are not simple enough, I think you have to cover them with a function named "if". That will also let you put the true and false in the perhaps more natural ordering of t f.
这篇关于APL中的条件函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!