bug-apl
[Top][All Lists]
Advanced

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

Re: [Bug-apl] String element of general array


From: Elias Mårtenson
Subject: Re: [Bug-apl] String element of general array
Date: Fri, 9 May 2014 23:05:57 +0800

How would you do that? The problem here is that you as a person see 'foo' and think of it as a string in this context and that you want to match on it. However, the ∊ function sees an array of elements (those elements being characters) so it acts upon them.

I suppose the best you can do would be to conditionally enclose the array if the depth is equal to 1. Something like:

  →((≡X)≠1)/skip
  X←⊂X
skip:

But I find that to be quite horrible for various reasons. One of those reasons being the fact that it would fail if you try to pass it the two strings: 'a' 'b'. This, of course, because 'a' is not a string at all, but a character.

I certainly am not overly happy about the fact that a single-character string is in fact a character. I would find APL to be much more logical if 'a' was equivalent to ,'a'. But, it's not, so we'd better embrace it. :-)

Regards,
Elias


On 9 May 2014 22:54, Blake McBride <address@hidden> wrote:
Thanks for the tutorial.  In my case, however, I already knew that.  More specifically, my problem is:

 x←'hello' 'there' 'how' 'are' 'you'

∇r←test v
r←(⊂v)∊x

This works for:

test 'there'

But doesn't work for:

test 'there' 'are'

I know I can remove the ⊂ for the second case.  I need a single line that works in both cases, or a test for v so I can branch to the correct code.

Thanks!

Blake



On Fri, May 9, 2014 at 9:19 AM, Elias Mårtenson <address@hidden> wrote:
I was very confused about this when I started learning APL too (well documented in this very mailing list's archive).

What happens can be illustrated by boxing the output. Let's look at a string:

      8⎕CR 'foo'
┌→──┐
│foo│
└───┘
      ⍴'foo'
3

In order words,this is a three-element array of characters. The third element of this array is the character 'o'.

Here's another example:

      8⎕CR 'foo' 'bar' 'testing'
┌→────────────────────┐
│┌→──┐ ┌→──┐ ┌→──────┐│
││foo│ │bar│ │testing││
│└───┘ └───┘ └───────┘│
└∊────────────────────┘
      ⍴'foo' 'bar' 'testing'
3

This is a three-element array of arrays. The subarrays themselves has the dimensions 3, 3 and 7 respectively. The third element is the string 'testing'.

Since the ∊ function matches each individual element, when passed the string 'foo' it will match each individual one. I.e, the characters 'f', 'o' and 'o'.

Finally, let's look at what the enclose function does:

      8⎕CR ⊂'foo'
┌─────┐
│┌→──┐│
││foo││
│└───┘│
└∊────┘
      8⎕CR ⍴⊂'foo'
┌⊖┐
│0│
└─┘

What the function does is to encapsulate the argument into a single scalar. A scalar has no dimensions, as seen by the fact that ⍴ returned ⍬.

Of course, you could also put the string inside a single-element array. Such array can be constructed as such:

      8⎕CR ,⊂'foo'
┌→────┐
│┌→──┐│
││foo││
│└───┘│
└∊────┘
      ⍴,⊂'foo'
1

The ∊ function will give identical results for that, since it interprets a scalar and a single-element array the same.

I'm a beginner myself, so perhaps I made a mistake in my explanation. I'll leave it to others to fill in any information I have missed.

Regards,
Elias


On 9 May 2014 20:07, Blake McBride <address@hidden> wrote:
Greetings,


On Fri, May 9, 2014 at 12:10 AM, Daniel H. Leidisch <address@hidden> wrote:
Hello!

Blake McBride <address@hidden>
writes:

> x←'abcd'  'efg'  'hijkl'
>
> Now, if I have:
>
> y←'hijkl'
>
> z←'hhh'
>
> How can I tell if y is in x?  How can I tell if z is in x?

Or for both at once:

      (y z)∊x
1 0

I like this best, except:

u←'abcd'
g←'ghjk'  'dsaw'

g∊x   works. but I have to do:

(⊂u)∊x

but:

u∊x doesn't work.

the left side is being past as an argument to a function.  I don't know if it is going to be a string array or a general array of strings.  I need a way to work in either case.  (Sorry for the stupid questions.  I'm just not straight with APL2 yest.)

Thanks.

Blake



 


Regards,

Daniel







reply via email to

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