[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: exercise 2019/01
From: |
Otto Diesenbacher-Reinmüller |
Subject: |
Re: exercise 2019/01 |
Date: |
Sun, 12 Apr 2020 12:05:10 +0200 |
User-agent: |
mu4e 1.0; emacs 28.0.50 |
Hi Kacper,
many, many thanks for your insights!
Kacper Gutowski <address@hidden> writes:
> I would write it as {(+\(⍴,⍵)⍴⍺↑1)⊂,⍵}.
(+\(⍴,⍵)⍴⍺↑1) - cool, generating the right binary vector and scan it...
I get more and more the impression, binary vectors - in conjunction with
f.e. scan and reduce are basis of a lot algos. Beside of the examples
with ∨ and ∧ in most literature/tutorials, is there somewhere a
collection about usage of binary vectors, or which path would you
recommend, to get deeper insights in this area? (I know about
aplcart.info, but it is quite heavy on Dyalog, and for a beginner
difficult to comprehend).
> Forcing right argument into a vector using ravel should be enough to
> handle scalars without any additional branching.
got it, ravel(,) to "normalize" any parameter into a vector.
>> 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
[...]
>
> 0≡↑0⍴X ⍝ X is numeric (if simple)
> ' '≡↑0⍴X ⍝ X is a character array (if simple)
sidenote: asking for the first element(↑) of an "empty" array, gives you
the prototypes: ' ' or 0.
> 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
got it, string in the sense of "a (simple) vector filled with chars" or
a "char-scalar".
> 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
br & many thanks! - Otto
--
Dipl. Ing. (FH) Otto Diesenbacher-Reinmüller
diesenbacher.net
Salzburg, Österreich