bug-apl
[Top][All Lists]
Advanced

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

Re: Question on scalar extension of left arg


From: Russtopia
Subject: Re: Question on scalar extension of left arg
Date: Thu, 29 Jul 2021 23:42:04 -0700

Please excuse my bad form in replying to my own message. I see now there is a subtle difference in the two expressions (namely, a down-arrow in the left edge of the outermost box in the second form below):

      (w w w w w)
┌→───────────────────────────────────────────┐
│┌→─────┐ ┌→─────┐ ┌→─────┐ ┌→─────┐ ┌→─────┐│
││listen│ │listen│ │listen│ │listen│ │listen││
│└──────┘ └──────┘ └──────┘ └──────┘ └──────┘│
└∊───────────────────────────────────────────┘
      ⍴(w w w w w)
┌→┐
│5│
└─┘
      (∊1 (⍴cl))⍴⊂w
┌→───────────────────────────────────────────┐
↓┌→─────┐ ┌→─────┐ ┌→─────┐ ┌→─────┐ ┌→─────┐│
││listen│ │listen│ │listen│ │listen│ │listen││
│└──────┘ └──────┘ └──────┘ └──────┘ └──────┘│
└∊───────────────────────────────────────────┘
      ⍴(∊1 (⍴cl))⍴⊂w
┌→──┐
│1 5│
└───┘
      ⍴((∊1 (⍴cl))⍴⊂w)[1;]    ⍝ Very ugly, but this seems to coerce to match the above ⍴(w w w w w)
┌→┐
│5│
└─┘

I suppose this is the reason one form works as an argument to my function, where the other does not.

I still seek to understand why APL does not apply scalar extension on the left arg though, to avoid needing either of these manual expansions to match a single left arg with the list of right args.

-Russ


On Thu, 29 Jul 2021 at 22:48, Russtopia <rmagee@gmail.com> wrote:
Hello, I am trying an exercise that finds anagrams of a word from a list of candidate words.

I am having issues with scalar extension of the left arg, which I thought should automatically repeat the scalar left arg to match each item in the right vector arg. It seems to work only if I do the expansion manually (that is, repeat the left arg explicitly rather than let APL do it).

(If formatting below is not readable, please convert it to a fixed-with font)

      w
┌→─────┐
│listen│
└──────┘
      cl
┌→────────────────────────────────────────────┐
│┌→──────┐ ┌→─────┐ ┌→─────┐ ┌→─────┐ ┌→─────┐│
││enlists│ │google│ │inlets│ │banana│ │tinsel││
│└───────┘ └──────┘ └──────┘ └──────┘ └──────┘│
└∊────────────────────────────────────────────┘
      ⎕cr 'is_anagram_of'
┌→──────────────────────────────┐
↓r ← word is_anagram_of other   │
│r ← word[⍋word] ≡ other[⍋other]│
└───────────────────────────────┘
      w is_anagram_of cl  
0
      w is_anagram_of ¨ cl    ⍝ <-- Should this not implicitly expand left arg?
DOMAIN ERROR
is_anagram_of[1]  r←word[⍋word]≡other[⍋other]
                         ^      ^
      w w w w w is_anagram_of cl    ⍝ <-- this works, w w w w w matches ⍴cl
0
      w w w w w is_anagram_of ¨ cl
┌→────────┐
│0 0 1 0 1│
└─────────┘
      (w w w w w is_anagram_of ¨ cl)/cl
┌→────────────────┐
│┌→─────┐ ┌→─────┐│
││inlets│ │tinsel││
│└──────┘ └──────┘│
└∊────────────────┘
      (∊1 (⍴cl))⍴⊂w
┌→───────────────────────────────────────────┐
↓┌→─────┐ ┌→─────┐ ┌→─────┐ ┌→─────┐ ┌→─────┐│
││listen│ │listen│ │listen│ │listen│ │listen││
│└──────┘ └──────┘ └──────┘ └──────┘ └──────┘│
└∊───────────────────────────────────────────┘
      w w w w w
┌→───────────────────────────────────────────┐
│┌→─────┐ ┌→─────┐ ┌→─────┐ ┌→─────┐ ┌→─────┐│
││listen│ │listen│ │listen│ │listen│ │listen││
│└──────┘ └──────┘ └──────┘ └──────┘ └──────┘│
└∊───────────────────────────────────────────┘
      ((∊1 (⍴cl))⍴⊂w) is_anagram_of ¨ cl   ⍝ <-- ?? Why does this not work as (w w w w w)?
RANK ERROR
      ((∈1(⍴cl))⍴⊂w)is_anagram_of¨cl
      ^                           ^

Advice appreciated,
Thank you,
-Russ

reply via email to

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