如何在APL中使用等级运算符代替每个运算符 [英] How to use rank operator instead of each in APL

查看:161
本文介绍了如何在APL中使用等级运算符代替每个运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

dummytxt←'abcdefghijk'
texttoadd←'down'
rfikv←20 30 50

并且需要以下输出

 defghijk20down  defghijk30down  defghijk50down 

我可以做到:

scenv←(¯10↑¨(⊂dummytxt),¨⍕¨rfikv),¨⊂texttoadd

但是请帮助我在不使用每个运算符的情况下使用等级

but please help me to write without each operator but using rank

我使用Dyalog APL,但请不要使用火车.

I use Dyalog APL, but please do not use trains.

谢谢

推荐答案

使用 Each

表达式(如f¨x)可以用 Rank 表示为{⊂f⊃⍵}⍤0⊢x(请注意,是将数组右操作数分开) ,0(来自数组右参数x)).换句话说,在参数的标量上,我们:

Expressions using Each, like f¨x, can be expressed in terms of Rank as {⊂f⊃⍵}⍤0⊢x (note that is to separate the array right operand, 0 from the array right argument x). In other words, on the scalars of the argument we:

  1. 公开标量:⊃⍵
  2. 应用功能:f⊃⍵
  3. 将结果括起来:⊂f⊃⍵
  1. disclose the scalar: ⊃⍵
  2. apply the function: f⊃⍵
  3. enclose the result: ⊂f⊃⍵

类似的表达式适用于二进位情况,x f¨y,但我们需要:

A similar expression applies for the dyadic case, x f¨y, but we need to:

  1. 公开两个标量:(⊃⍺)(⊃⍵)
  2. 应用功能:(⊃⍺)f(⊃⍵)
  3. 将结果括起来:⊂(⊃⍺)f(⊃⍵)
  1. disclose both scalars: (⊃⍺)(⊃⍵)
  2. apply the function: (⊃⍺)f(⊃⍵)
  3. enclose the result: ⊂(⊃⍺)f(⊃⍵)

这给了我们x{⊂(⊃⍺)f(⊃⍵)}⍤0⊢y.因此,我们可以使用 Rank 来构建我们自己的 Each 运算符,该运算符允许对派生函数的单子和二元应用:

This gives us x{⊂(⊃⍺)f(⊃⍵)}⍤0⊢y. We can thus use Rank to build our own Each operator which allows both monadic and dyadic application of the derived function:

      Each←{⍺←⊢ ⋄ ⍺ ⍺⍺{×⎕NC'⍺':⊂(⊃⍺)⍺⍺(⊃⍵) ⋄ ⊂⍺⍺⊃⍵}⍤0⊢⍵}
      (¯10↑Each(⊂dummytxt),Each⍕Each rfikv),Each⊂texttoadd
 defghijk20down  defghijk30down  defghijk50down 

或者,我们可以将两个更简单的等价替换为您的表达式:

Alternatively, we can substitute the two simpler equivalences into your expression:

      (¯10{⊂(⊃⍺)↑(⊃⍵)}⍤0⊢(⊂dummytxt){⊂(⊃⍺),(⊃⍵)}⍤0{⊂⍕⊃⍵}⍤0⊢rfikv){⊂(⊃⍺),(⊃⍵)}⍤0⊂texttoadd
 defghijk20down  defghijk30down  defghijk50down 

请注意,我们将texttoadd括起来,使其成为标量,然后我们使用⍤0来处理整个标量,只再次公开它.相反,我们可以使用⍤0 1表示要在应用函数时使用整个 vector 右参数,而该函数又不需要公开其右参数:

Notice that we are enclosing texttoadd so it becomes scalar, and then we use ⍤0 to address that entire scalar, only to disclose it again. Instead, we can use ⍤0 1 to say that want to use the entire vector right argument when applying the function, which in turn doesn't need to disclose its right argument:

      (¯10{⊂(⊃⍺)↑(⊃⍵)}⍤0⊢(⊂dummytxt){⊂(⊃⍺),(⊃⍵)}⍤0{⊂⍕⊃⍵}⍤0⊢rfikv){⊂(⊃⍺),⍵}⍤0 1⊢texttoadd
 defghijk20down  defghijk30down  defghijk50down 

rfikv¯10是简单的标量,因此公开它们没有作用:

rfikv and ¯10 are a simple scalars, so disclosing them has no effect:

      (¯10{⊂⍺↑(⊃⍵)}⍤0⊢(⊂dummytxt){⊂(⊃⍺),(⊃⍵)}⍤0{⊂⍕⍵}⍤0⊢rfikv){⊂(⊃⍺),⍵}⍤0 1⊢texttoadd
 defghijk20down  defghijk30down  defghijk50down 

dummytxt与上面的texttoadd处于相同的情况,但是作为left参数,因此我们可以跳过enclose-disclose并要求Rank使用整个向量left参数; ⍤1 0:

dummytxt is in the same situation as texttoadd above, but as left argument, so we can skip the enclose-disclose and ask Rank to use the entire vector left argument; ⍤1 0:

      (¯10{⊂⍺↑(⊃⍵)}⍤0⊢dummytxt{⊂⍺,(⊃⍵)}⍤1 0{⊂⍕⍵}⍤0⊢rfikv){⊂(⊃⍺),⍵}⍤0 1⊢texttoadd
 defghijk20down  defghijk30down  defghijk50down 

这与使用常规方法获得的结果一样简单.但是,如果相反地观察到唯一的非标量是rfikv,则可以将dummytxttexttoadd视为全局常量,并将整个内容表示为rfikv上的单个⍤0函数应用程序:

This is about as simple as it gets using a general method. However, if we instead observe that the only non-scalar is rfikv, we can treat dummytxt and texttoadd as global constants and express the entire thing as a single ⍤0 function application on rfikv:

      {⊂(¯10↑dummytxt,⍕⍵),texttoadd}⍤0⊢rfikv
 defghijk20down  defghijk30down  defghijk50down 

当然,每个 也可以做到这一点:

Of course, Each can do this too:

      {(¯10↑dummytxt,⍕⍵),texttoadd}¨rfikv
 defghijk20down  defghijk30down  defghijk50down 

这篇关于如何在APL中使用等级运算符代替每个运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆