Current issue

Vol.26 No.4

Vol.26 No.4

Volumes

© 1984-2024
British APL Association
All rights reserved.

Archive articles posted online on request: ask the archivist.

archive/22/2

Volume 22, No.2

APL Idioms - Responses

Ajay Askoolum’s APL Idioms

from Steve Mansour (steve@carlislegroup.com)

All solutions are written using dynamic functions in Dyalog APL, ⎕ml←3.  All are origin independent.

Some notes:

I have two solutions to idiom #1,  CE and CE1. CE1 doesn't use the rho symbol or the comma symbol.

  CE←{×/⍴⍵}
  CE1←{+/∊⍵≡¨⍵}

I have two solutions to idiom #2, RA and RA1. Both require a 2-item vector on the left as the range. RA requires a scalar right argument. RA1 permits any array as the right argument and doesn’t use each. For more details on this solution, please see my article “Arithmetic Boundary Checking”in Vector Vol 21 No. 2.  Solution to this particular problem is at the top of page 122.

  RA←{+/×⍵-⍺}
  RA1←{+/×⍵∘.-⍺}

Idiom #3:

  SN←{⍵[⍋⌊10⍟|⍵;]}

Idiom #4:

  LD←{{⍵}⌿⍵}

Idiom #5:  Curried left argument can be any suitable value > 0.  Domain error may result if the base of the exponential function is not chosen correctly.

  LS←0.9∘{⍺⍟×⌿⍺*⍵}

Idiom #6:  Note – ⎕D  is equivalent to '0123456789' :

  CN←{10⊥-⎕IO-⎕D⍳⍵}

Idiom #7:

  ZM←{0+/⍵}

Idiom #8:

  FO←{2</0,⍵}

Idiom #9:

  LO←{2>/⍵,0}

Idiom #10:

  RE←{↑¨↓∘⍺¨↓⍵-1}

More Solutions ...

from Gianluigi Quario (giangiquario@hotmail.com)

1. How many elements does a given variable have?

Restriction: assume that the keyboard does not have the comma (,) symbol.

CE←×/∘⍴ ⍝ ⍵←A

2. Is a value within a given range?

Restriction: assume that the keyboard does not have any relational operators.

C2←{+/×⍵[⍋⍵]-⍺} ⍝ ⍺←D0 ⍵←D1 ⍴⍵←→2

3. Sort a numeric array of integers in ascending order of the number of digits.

Restriction: assume that the keyboard does not have the format () or quad () symbol.

SN←{⍵[⍋⌈10⍟|(⍵+2×0=⍵)+×⍵;]} ⍝ ⍵←I2 (1↓⍴⍵)←→1

4. Return the element(s) of a numeric array indexed by its first dimension.

Restriction: assume that the keyboard does not have the shape of () symbol.

NB.(GQR) the definition of this problem is not complete:

a) should return an array with a lesser rank?

b) a scalar has not a “first dimension”, but it is required by the challenge:

  LD 6←→6

  LD←{,[1,(0<≡+/~0×⍵)/2]1↑[1],[(0<≡⍵)/1]⊂[⍬]⊖⍵}
       ⍝ ⍵←D (also scalar)

or

  LD_←{,[1,(0<≡+/~0×⍵)/2]1↑[1]⊖⍉1↓[1],[1 2]⍉,[⍬]0,⍵}
       ⍝ ⍵←D (also scalar)

or

  LD__←{1↑[1]⊖⍵} ⍝ ⍵←D ; ⍵ not scalar;
         NB.result has same rank of ⍵

5. Return the sum of element(s) of a numeric array on its first dimension.

Restriction: assume that the keyboard does not have the plus (+) symbol.

NB.(GQR) a scalar has not a “first dimension”, but it is required by the challenge:

  LS 6←→6

  LS←{⎕ML←2 ⋄ ↑¨⍴¨∊¨⊂[(~0∊⍴⍴⍵)/1]⍵⍴¨1}
          ⍝APL2 ⍝⍵←D (also scalar)

6. Convert the string representation of integers to numbers.

Restriction: assume that the keyboard does not have the execute () symbol.

  CN←{10⊥2⊥4↓[⎕IO]⍉11 ⎕DR,[⍬]⍵} ⍝ ⍵←C1 (also scalar)
  CN_←{10⊥2⊥¨4↓¨11 ⎕DR¨⍵} ⍝ ⍵←C1 (also scalar)
  CN__←{10⊥(-⎕AV⍳'0')+⎕AV⍳⍵} ⍝ ⍵←C1 (also scalar)
  CN___←{10⊥(-⎕IO)+'0123456789'⍳⍵} ⍝ ⍵←C1 (also scalar)

7. Return a numeric array as zeros, increasing the last dimension by 1.

Restriction: assume that the keyboard does not have the times (×), take (?), minus (-), or comma (,) symbols and it does not end with ⍴0.

ZM←{((((¯1+⍴⍴⍵)⍴0),1)+⍴⍵)⍴⍵=⍵+1} ⍝ ⍵←D

8. Return the first ones from a Boolean vector.

Restriction: assume that the keyboard does not have the not (~) or rotate (²) symbols.

FO←{⍵^1,(1↓⍵)≠¯1↓⍵} ⍝ ⍵←B1

9. Return the last ones from a Boolean vector.

Restriction: assume that the keyboard does not have the not (~) or rotate (²) symbols.

LO←{⍵^((1↓⍵)≠¯1↓⍵),1} ⍝ ⍵←B1

10. Return the elements of a numeric array found at given coordinates.

Restrictions: assume that a looping solution is not allowed nor one involving semi-colon and that index origin is 1.

RE← {(,⍺)[1+(⍴⍺)⊥⍉⍵-1]} ⍝ ⍺←D2 ; ⍵←D 1

More Solutions ...

by Claude Henriod (chenriod@vtx.ch)

I present some solutions to the problems posed by Ajay Askoolum in APL Idioms, Vector 21:4, pp92-96.

The development is for a great part from my libraries originated in the 198x years on :
/APLSV IBM5110/APL68000 Wicat&Ampère/later/IBMAPL2 PS+PC/IBMwinAPL2/

All are shown as DDF valid I-APL. Tests are made in I-APL to be sure that the code is OK. But the function code is cloned in APL2 Unicode, for a better understanding.

= Requested functions APL2, valid with several interpreters.

 LIM: +/×(2⍴⍺)∘.-⍵
 SN: (⍴⍵)⍴(,⍵)[⍋IX+~×IX←⌈10⍟LG+~×LG←|,⍵]
 LS: -⌿⍵×⍉(⌽⍴⍵)⍴(1↑(⍴⍵),1)⍴1 ˉ1
 CN: ((⍴⍵)⍴10⊥⍵←'0123456789'⍳⍵ : ⎕IO←0 : 0
 FO: ⍵>ˉ1↓0,⍵
 LO: ⍵>1↓⍵,0
 RE: (,⍺)[⎕IO+R⊥⍵-⎕IO] : ∨/0=(R←⍴⍺),⍴⍵←⍉⍵ : ⍳0

= Functions which do not match prerequisite

 CE: ⍴1/⍵              ⍝ Answer fail
 LD: ˉ1↑[1↑(⍳⍴⍴⍵),1)⍵  ⍝ ⍴ is forbidden
 ZM: ~⍵÷⍵←⍵,1          ⍝ , is forbidden








C. Henriod, retired IBM Advisory SE, chenriod@vtx.ch
December 7, 2005.


script began 9:12:30
caching off
debug mode off
cache time 3600 sec
indmtime not found in cache
cached index is fresh
recompiling index.xml
index compiled in 0.1878 secs
read index
read issues/index.xml
identified 26 volumes, 101 issues
array (
  'id' => '10007130',
)
regenerated static HTML
article source is 'HTML'
source file encoding is 'ASCII'
read as 'Windows-1252'
URL: ../arch.css => trad/v222/../arch.css
URL: chenriod@vtx.ch => trad/v222/chenriod@vtx.ch
URL: ../v214/idiom.htm => trad/v222/../v214/idiom.htm
URL: henriod1.gif => trad/v222/henriod1.gif
URL: henriod3.gif => trad/v222/henriod3.gif
URL: henriod5.gif => trad/v222/henriod5.gif
URL: henriod07.gif => trad/v222/henriod07.gif
URL: henriod9.gif => trad/v222/henriod9.gif
URL: henrio11.gif => trad/v222/henrio11.gif
URL: henio13.gif => trad/v222/henio13.gif
URL: henrio15.gif => trad/v222/henrio15.gif
URL: henrio17.gif => trad/v222/henrio17.gif
completed in 0.2138 secs