bug-apl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: exercise 2019/01


From: Kacper Gutowski
Subject: Re: exercise 2019/01
Date: Sun, 12 Apr 2020 02:32:30 +0200

On Sat, Apr 11, 2020 at 11:02:39PM +0200, Otto Diesenbacher-Reinmüller wrote:
1 chunk monkey

I would write it as {(+\(⍴,⍵)⍴⍺↑1)⊂,⍵}.
(In low-⎕ML Dyalog it would be {((≢⍵)⍴⍺↑1)⊂,⍵} without needing that +\ on left argument.) You can write ≢⍵ or ⍴,⍵ here, using ≢ is probably more idiomatic in Dyalog and works just as fine in GNU APL. Forcing right argument into a vector using ravel should be enough to handle scalars without any additional branching.

  →((0=≢⍴a)∧1=≢a)/skalar        ⍝ matches a skalar, but not ''

This is redundant as 0=≢⍴a implies 1=≢a.
Note that '' is not a scalar but a vector of length zero. The notation for character literals is confusing, I know.

Checking for types is not easy. 0=⍴N is a scalar. Howto check for a string?

"String" usually means a "vector of characters". You already know how to check a rank (I suppose it's just a typo and you meant 0=⍴⍴N or maybe 0=≢⍴N for a scalar, i.e. an array of rank zero) and vector is just an array of rank one.

The closest thing to what can be informally called a type of an array is its typical element (also called fill element or a prototype). For simple numeric arrays the typical element is zero, for simple character arrays it is a blank. You can use it like this:

      0≡↑0⍴X    ⍝ X is numeric (if simple)
      ' '≡↑0⍴X  ⍝ X is a character array (if simple)

So it would be something like (1=⍴⍴X)∧' '≡↑0⍴X to check for a "string".
Or maybe (1≥⍴⍴X)∧' '≡↑0⍴X if you wanted to include character scalars like 'a' in the definition of a "string". It will nevertheless fail if your array is mixed because those have implementation-defined typical elements, so you might want to check depth and every element:
      ∧/(1=⍴⍴X),(1≥≡X),{' '≡↑0⍴⍵}¨X

(In case of Dyalog replace monadic ↑ with ⊃ as needed depending on the value of ⎕ML)

You won't really need checks like that. Just write your code assuming your data is of the right "type" and let some primitive throw a domain error if it isn't. If you need to accept vectors but also pretend that scalar is a one-element vector, just ravel it.

-k



reply via email to

[Prev in Thread] Current Thread [Next in Thread]