2+2 2+2
5 posts
• Page 1 of 1
2+2 2+2
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.
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: 12
 Joined: Mon Apr 09, 2018 8:37 am
Re: 2+2 2+2
Hi! Thanks for asking this question because it points out a common stumbling point when starting with APL. The important things to remember are
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.
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!
 APL executes from right to left
 Data "strands" together
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!

BrianDyalog  Posts: 73
 Joined: Thu Nov 26, 2009 4:02 pm
 Location: West Henrietta, NY
Re: 2+2 2+2
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 righttoleft. Once you've done this any other approach will look complicated and wrong ;)
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 righttoleft. Once you've done this any other approach will look complicated and wrong ;)

kai  Posts: 88
 Joined: Thu Jun 18, 2009 5:10 pm
 Location: Stevenage, UK
Re: 2+2 2+2
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
"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
Back to your query.
Adding redundant parenthesis one gets
giving 6 6 and not
which would indeed give 4 4
While
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.
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.
Please excuse any smelling pisstakes.

ray  Posts: 119
 Joined: Wed Feb 24, 2010 12:24 am
 Location: Blackwater, Camberley. UK
Re: 2+2 2+2
Hmmm , back to C#. Just kidding!
Thank you all for clearification. It really helps a ton.
Henk.
Thank you all for clearification. It really helps a ton.
Henk.
 hbarkhof
 Posts: 12
 Joined: Mon Apr 09, 2018 8:37 am
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group