2+2 2+2

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

2+2 2+2

Postby hbarkhof on Sat Apr 21, 2018 7:40 am

Very new to APL so forgive the dumb question. I simply don't see the logic of an example in the book "MasteringDyalogAPL"

it is :

2+2 2+2 'I would expect 4 4 but I got 6 6

On the other hand :

2+2 ,2+2 'Again I would expect 4 4 but got 4 6

Can someone explain to me how I should come to these results ?

Thanks.

Henk.
hbarkhof
 
Posts: 44
Joined: Mon Apr 09, 2018 8:37 am

Re: 2+2 2+2

Postby Brian|Dyalog on Sat Apr 21, 2018 2:27 pm

Hi! Thanks for asking this question because it points out a common stumbling point when starting with APL. The important things to remember are
  • APL executes from right to left
  • Data "strands" together
I'll try to illustrate this with these problems. In the first problem, the space between the 2 2 doesn't perform any function, it merely separates the elements of this 2-item vector. So the rightmost + has arguments of 2 (on the right) and 2 2 (on the left). This is passed on as the right argument to the next +.
      2 + 2 2 + 2
-------
2 + 4 4
-------
6 6

In the second problem the comma (,) is a function - concatenate. So this means the rightmost + has a right argument of 2 and a left argument of 2. This yields 4, which is the right argument to ,. The left argument is 2 and the concatenation yields 2 4 which is the right argument to the next function, +. The left argument is 2, so 2 + 2 4 yields 4 6.
      2 + 2 , 2 + 2
-----
2 + 2 , 4
-----
2 + 2 4
-------
4 6

I hope I've helped here. Bernard, Mastering Dyalog APL's author, was exceptionally clever at constructing examples like this that help address and overcome the preconceptions we have when starting with APL. The first example addresses our bias to group things visually - we see the space in the middle as separating the addition operations on either side, but in truth the space is merely a separator for data elements. If it was written as 2 + (2 2) + 2, it would be clearer, but defeat the point of the problem. The second example addresses the idea of precedence of operations that most of us learned in mathematics in school. We "see" the + as having higher precedence than the ,. Not so in APL, everything is right to left.

Keep asking questions!
User avatar
Brian|Dyalog
 
Posts: 116
Joined: Thu Nov 26, 2009 4:02 pm
Location: West Henrietta, NY

Re: 2+2 2+2

Postby kai on Sat Apr 21, 2018 2:29 pm

Read and interpret from right to left.

First expression is

2 2+2

Due to scalar extension that is transformed into

2 2+2 2

so you get

4 4

Next is

2 + 4 4

Which translates to

2 2 + 4 4

so you get

6 6

The other one: first expression is

2+2

which is 4

Then you have

2,4

which is just 2 4 of course and finally

2+2 4

hence

4 6

You have to internalize both scalar extension and right-to-left. Once you've done this any other approach will look complicated and wrong ;)
User avatar
kai
 
Posts: 137
Joined: Thu Jun 18, 2009 5:10 pm
Location: Hillesheim / Germany

Re: 2+2 2+2

Postby ray on Sat Apr 21, 2018 3:10 pm

By the time I had written the answer below, I see it has already been answered twice, But here goes!

With APL you must always remember the orders of both execution and association.

Strand notation is the forming of a vector by using spaces between scalar items.
EG
Code: Select all
      'A' 'B' 'C'
ABC
      1 2 3
1 2 3
a←1 2 3 4 5

"a" is the vector formed via strand notation from the scalar elements 1, 2, 3, 4, and 5.

Strand notation takes precedence over order of execution.
Since A + B is the same as B + A,
so
Code: Select all
      1+ 1 2 3 4
2 3 4 5
⍝ is the same as
      1 2 3 4 +1
2 3 4 5


Back to your query.

2+2 2+2 'I would expect 4 4 but I got 6 6


Adding redundant parenthesis one gets
Code: Select all
2 + ( (2 2) + 2)

giving 6 6 and not
Code: Select all
(2 + 2) ( 2+ 2)

which would indeed give 4 4

While
2+2 ,2+2 'Again I would expect 4 4 but got 4 6

Code: Select all
2 + (2 ,(2 + 2))

gives 4 6.

It might be easier 2 3 4 and 5 rather than 2 2 2 and 2 so that
2 + 2 2 + 2 becomes 2 + 3 4 +5

From right to left scalar 5 is added to the left hand argument
(by "strand notation" a 2 element vector) 3 4 (giving 8 9),
which in turn is added to the scalar 2 giving 10 11.
Ray Cannon
Please excuse any smelling pisstakes.
User avatar
ray
 
Posts: 221
Joined: Wed Feb 24, 2010 12:24 am
Location: Blackwater, Camberley. UK

Re: 2+2 2+2

Postby hbarkhof on Sat Apr 21, 2018 4:38 pm

Hmmm , back to C#. Just kidding!

Thank you all for clearification. It really helps a ton.

Henk.
hbarkhof
 
Posts: 44
Joined: Mon Apr 09, 2018 8:37 am


Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest