Diamond: Difference between revisions

From NARS2000
Jump to navigationJump to search
Paul Robinson (talk | contribs)
use header template
expanded description, added example
 
Line 8: Line 8:


==Usage==
==Usage==
While the diamond operator (<big><big>{diamond}</big></big>) can be used in an immediate statement to the Session Manager, it is most commonly used in a [[functions|function]] to start a new statement on the same line. It is used similarly to the semi colon ('''<big>;</big>''') in C, Pascal, PHP and many other languages, or the colon ('''<big>:</big>''') and backslash ('''<big>\</big>''') in some versions of Basic. It allows another statement to be entered on the same line.
While the diamond operator ('''<big><big>{diamond}</big></big>''') can be used in an immediate statement to the Session Manager, it is most commonly used in a [[functions|function]] to start a new statement '''on the same''' physical line-row of user-code. It is used similarly to the semi colon ('''<big>;</big>''') in C, Pascal, PHP and many other languages, or the colon ('''<big>:</big>''') and backslash ('''<big>\</big>''') in some versions of Basic. It allows another statement to be concisely set on the same line.


'''Note:''' The <big><big>{diamond}</big></big> operator cannot be used after a [[lamp]] (<big><big>{lamp}</big></big>) comment operator appears on the same line, as all characters after <big><big>{lamp}</big></big> are ignored.
'''Note:''' The '''<big><big>{diamond}</big></big>''' operator <u>cannot</u> be used after a [[lamp]] (<big><big>{lamp}</big></big>) comment operator appears on the same line, as all characters after <big><big>{lamp}</big></big> are ignored.


==Example==
'''Also note''' the '''<big><big>{diamond}</big></big>''' operator represents an <u>exception to the rule</u> that APL's interpreter executes from right to left, since the ''leftmost APL statement'' is executed BEFORE the adjacent APL statement after the next '''<big><big>{diamond}</big></big>''' operator and so on. Each APL statement is executed sequentially '''as if''' listed on multiple lines. The '''<big><big>{diamond}</big></big>''' operator also helps ''condense'' APL code since fewer ''visual'' lines will contain the same amount of APL coding - and in many instances it ''can result'' in cleaner, or easier-to-read-and-understand APL code.
In this example, the variable K is tested to see if it is greater than or equal to 1. If the test fails, k is set to 1.<br/>
 
:<code><big>{goto}(k{ge}1)/Xr {diamond} K{:=}1<br/>Xr:</big></code><br/>
==Examples==
In the first example, the variable K is tested to see if it is greater than or equal to 1. If the test fails, k is set to 1.<br/>
:<code><big>{goto}(k{ge}1)/Xr {diamond} K{:=}1<br/>Xr: &nbsp; &nbsp; ⍝This branch/goto Label is deliberately placed <u>first</u> on its own line!</big></code><br/>
It is the same as
It is the same as
:<code><big>{goto}(k{ge}1)/Xr <br/> K{:=}1<br/>Xr:</big></code><br/>
:<code><big>{goto}(k{ge}1)/Xr <br/> K{:=}1<br/>Xr:</big></code><br/>


The following represents more <u>condensed</u> ''':for ... :endfor''' control structure in a user-function than if it were shown in four separate lines:<br><font color=purple><strong>
<code>
A←1 3 5 7 ⋄ :for ii :in ⍳⍴A ⋄ A[ii]←A[ii]*2 ⋄ :endfor  ⍝Easier to maintain code if segmented well, esp. when surrounded by long code blocks.
</code></strong></font>


{{Article footer}}
{{Article footer}}
{{Symbol footer|11|D}}
{{Symbol footer|11|D}}

Latest revision as of 23:03, 21 February 2015

⋄ — Statement separator — Keystroke ALT+` (Accent grave) — Character 8900 or x22C4

[[file:]]
{{#if: {{{kb2}}} | [[File:{{{kb2}}}]] | }}

Usage

While the diamond operator (⋄) can be used in an immediate statement to the Session Manager, it is most commonly used in a function to start a new statement on the same physical line-row of user-code. It is used similarly to the semi colon (;) in C, Pascal, PHP and many other languages, or the colon (:) and backslash (\) in some versions of Basic. It allows another statement to be concisely set on the same line.

Note: The ⋄ operator cannot be used after a lamp (⍝) comment operator appears on the same line, as all characters after ⍝ are ignored.

Also note the ⋄ operator represents an exception to the rule that APL's interpreter executes from right to left, since the leftmost APL statement is executed BEFORE the adjacent APL statement after the next ⋄ operator and so on. Each APL statement is executed sequentially as if listed on multiple lines. The ⋄ operator also helps condense APL code since fewer visual lines will contain the same amount of APL coding - and in many instances it can result in cleaner, or easier-to-read-and-understand APL code.

Examples

In the first example, the variable K is tested to see if it is greater than or equal to 1. If the test fails, k is set to 1.

→(k≥1)/Xr ⋄ K←1
Xr:     ⍝This branch/goto Label is deliberately placed first on its own line!

It is the same as

→(k≥1)/Xr
K←1
Xr:

The following represents more condensed :for ... :endfor control structure in a user-function than if it were shown in four separate lines:
A←1 3 5 7 ⋄ :for ii :in ⍳⍴A ⋄ A[ii]←A[ii]*2 ⋄ :endfor ⍝Easier to maintain code if segmented well, esp. when surrounded by long code blocks.


See Also
System Commands System Variables and Functions Operators


Keyboard
A+S ⍪ ≡ ≢ ⍒ ⍋ ⌽ ⍉ ⊖ ⍟ ⍱ ⍲ ⍠ ⌹
Alt ⋄ ¨ ¯ < ≤ ∅ ≥ > ≠ ∨ ∧ × ÷
Sh ~ ! @ # $ % ^ & * ( ) _ +
Key ` 1 2 3 4 5 6 7 8 9 0 - =
A+S ⍷ √ ⍨ ⍸ ⍥ ⍣ ⍞ ⍬ ⊣
Alt ? ⍵ ∊ ⍴ § ↑ ↓ ⍳ ○ π ← → ⊢
Sh Q W E R T Y U I O P { } |
Key q w e r t y u i o p [ ] \
A+S ∫ ∂ ⌻ ⍢ ⍙ ⍤ ⍫ ⌷
Alt ⍺ ⌈ ⌊ ∞ ∇ ∆ ∘ ‼ ⎕ ⍎ ⍕
Sh A S D F G H J K L : "
Key a s d f g h j k l ; '
A+S ⊆ ⊇ χ ⍡ ⍭ ⊙
Alt ⊂ ⊃ ∩ ∪ ⊥ ⊤ ⍦ ⍝ ⍀ ⌿
Sh Z X C V B N M < > ?
Key z x c v b n m , . /
{{#ifeq:{{{1}}}|4||title="match" style="border-width:thick; border-color:blue; background-color:yellow;"|≡ {{#ifeq:{{{1}}}|7||title="iota" style="border-width:thick; border-color:blue; background-color:yellow;" |⍳ {{#ifeq:{{{1}}}|9||title="slash" style="border-width:thick; border-color:blue; background-color:yellow;" |/ {{#ifeq:{{{1}}}|11||title="diamond" style="border-width:thick; border-color:blue; background-color:yellow;" |⋄ {{#switch: {{{1}}}
NARS 2000 Lang
Tool
Bar

{{#ifeq:{{{1}}}|1||title="assign" style="border-width:thick; border-color:blue; background-color:yellow;" |←

→ bgcolor=cyan title="assign" |← → }}

{{#ifeq:{{{1}}}|2||title="plus" style="border-width:thick; border-color:blue; background-color:yellow;" |+

- × ÷ * ⍟ ⌹ ○ ! ? √ bgcolor=cyan title="plus" |+ - × ÷ * ⍟ ⌹ ○ ! ? √ }}

{{#ifeq:{{{1}}}|3||title="mod" style="border-width:thick; border-color:blue; background-color:yellow;" ||

⌈ ⌊ ⊥ ⊤ ⊣ ⊢
⌈ ⌊ ⊥ ⊤ ⊣ ⊢
≢ < ≤ = ≥ > ≠ bgcolor=cyan title="match" |≡ ≢ < ≤ = ≥ > ≠ }}

{{#ifeq:{{{1}}}|5||title="down caret" style="border-width:thick; border-color:blue; background-color:yellow;" |∨

∧ ⍱ ⍲ bgcolor=cyan title="down caret" |∨ ∧ ⍱ ⍲ }}

{{#ifeq:{{{1}}}|6||title="take" style="border-width:thick; border-color:blue; background-color:yellow;" |↑

↓ ⊂ ⊃ ⌷ ⍋ ⍒
↓ ⊂ ⊃ ⌷ ⍋ ⍒
∊ ⍸ ⍷ ∪ ∩ ⊆ ⊇ ~ § π .. bgcolor=cyan title="iota" |⍳ ∊ ⍸ ⍷ ∪ ∩ ⊆ ⊇ ~ § π .. }}

{{#ifeq:{{{1}}}|8||title="comma" style="border-width:thick; border-color:blue; background-color:yellow;" |,

⍪ ⍴ ⌽ ⊖ ⍉
⍪ ⍴ ⌽ ⊖ ⍉
\ ⌿ ⍀ ⊙ ¨ ⍨ ⍤ ⍡ ⍥ ⍦ . ∘ ⍠ ‼ ⌻ ∂ ∫ bgcolor=cyan title="slash" |/ \ ⌿ ⍀ ⊙ ¨ ⍨ ⍤ ⍣ ⍡ ⍥ ⍦ . ∘ ⍠ ‼ ⌻ ∂ ∫ }}

{{#ifeq:{{{1}}}|10||title="quotequad" style="border-width:thick; border-color:blue; background-color:yellow;" |⍞

⎕ ⍎ ⍕
⎕ ⍎ ⍕
⍝ ∇ ∆ ⍙ _ ⍺ ⍵ bgcolor=cyan title="diamond" |⋄ ⍝ ∇ ∆ ⍙ _ ⍺ ⍵ }}

{{#ifeq:{{{1}}}|12||title="neg" style="border-width:thick; border-color:blue; background-color:yellow;" |¯

⍬ ∞ title="neg" |¯ ⍬ ∞ ∅
colspan=8 |Second Row i j k a b e g p r v x z colspan=8 |Second Row i j k a b e g p r v x z colspan=8 |Second Row i j k a b e g p r v x z colspan=6 |Second Row i j k i j k l g p r v x

}}

{{#ifeq:{{{1}}}|0| |[[Category:Mouse Group {{{1}}}|{{{2}}}]]}}


{{#ifeq:11|0| |}}