My first ⎕CSE program - "Hello World"
Posted: November 11th, 2014, 2:30 pm
One APLer's view of C#
Here are some defintion of terms from my point of view. I may not have everything correct yet, this is my start in the ⎕CSE world.
A Method is to C# what a user Function is to APL. Methods always have parenthesis. A niladic APL function, Foo, equivalent in C# would be, Foo()
A Property is to C# what a Variable is to APL. Properties in the C# world can be of many different kinds.
A Namespace is to C# what a Workspace is to APL.
An expression is to C# what a statement is to APL.
The semi-colon in C# marks the end of an expression and must be present.
The semi-colon also acts like an APL diamond, allowing the placement of multiple expressions on a single line
C#: expression; expression; expression;
APL: statement ⋄ statement ⋄ statement
While C# programs can be written on a single line, they are often broken up and indented to show the thought and flow of the program. This is the same as APL!
C# is always index 0.
C# can reference Methods and Properties in other Namespaces. Those references must be declared. APL would need to copy in the Functions and Variables from another workspace or component file in order to use them. These need to be declared in the reference section in Visual Studio. In APL, these are the arguments to the "Init" method of ⎕CSE.
C# is object oriented, and certain namespaces use object in other namespaces, so both must be referenced.
Using ⎕CSE in APL, the arguments of the "Init" Method are the references to the .NET Namespaces that will be referenced in the C# code. This is analogous to the Reference section in the Solution Explorer in Visual Studio.
"using" statements in C# create shorter names to objects in other Namespaces. These are optional, but make writing code easier/shorter.
My first APL/⎕CSE program - Spoken "Hello World"
The Namespace 'System.Speech' contains the System.Speech.Synthesis namespace and the using statement creates a shortcut to the SpeechSynthesizer, otherwise it would have needed to be called by typing System.Speech.Synthesis.SpeechSynthesize.
'System.Speech.Synthesis.SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer();'
using simplifies this to:
'SpeechSynthesizer synth = new SpeechSynthesizer();'
CSSpeak 'Hello World'
Second ⎕CSE program:
CSPlayFile 'c:\windows\media\tada.wav'
The Uri Method is a member of the System Namespace. System.Windows.Media.MediaPlayer references both the PresentationCore and WindowsBase Namespaces and thus they must be "referenced" in the ⎕CSE "Init" method.
The .NET framework has thousands of methods in many different namespaces. The Object Explorer in Visual Studio is useful for finding how to reference a given method. And the Internet is helpful for finding out how to use the various methods, including places such as StackExchange.
I found the videos on C# for the Absolute Beginners to be very helpful.
http://channel9.msdn.com/Series/C-Funda ... -Beginners
I have not been able to write ⎕CSE code to access the console. The code seems to execute without error, but no concole window opens. ???
Here are some defintion of terms from my point of view. I may not have everything correct yet, this is my start in the ⎕CSE world.
A Method is to C# what a user Function is to APL. Methods always have parenthesis. A niladic APL function, Foo, equivalent in C# would be, Foo()
A Property is to C# what a Variable is to APL. Properties in the C# world can be of many different kinds.
A Namespace is to C# what a Workspace is to APL.
An expression is to C# what a statement is to APL.
The semi-colon in C# marks the end of an expression and must be present.
The semi-colon also acts like an APL diamond, allowing the placement of multiple expressions on a single line
C#: expression; expression; expression;
APL: statement ⋄ statement ⋄ statement
While C# programs can be written on a single line, they are often broken up and indented to show the thought and flow of the program. This is the same as APL!
C# is always index 0.
C# can reference Methods and Properties in other Namespaces. Those references must be declared. APL would need to copy in the Functions and Variables from another workspace or component file in order to use them. These need to be declared in the reference section in Visual Studio. In APL, these are the arguments to the "Init" method of ⎕CSE.
C# is object oriented, and certain namespaces use object in other namespaces, so both must be referenced.
Using ⎕CSE in APL, the arguments of the "Init" Method are the references to the .NET Namespaces that will be referenced in the C# code. This is analogous to the Reference section in the Solution Explorer in Visual Studio.
"using" statements in C# create shorter names to objects in other Namespaces. These are optional, but make writing code easier/shorter.
My first APL/⎕CSE program - Spoken "Hello World"
- Code: Select all
ret←larg CSSpeak text;s;sink
:if ⎕dyadic ⍝ used only to redefine the ⎕cse instance when adding new code
'Speak' ⎕cse 'Close'
:end
:if~(⊂⎕cself←'Speak')∊'#'⎕cse'instances'
⎕cself←'Speak' ⎕cse 'Init' 'System' 'System.Speech'
ret←⎕cse'returnonerror'1
s←⊂'using System;'
s,←⊂'using System.Speech.Synthesis;'
s,←⊂'SpeechSynthesizer synth = new SpeechSynthesizer();'
s,←⊂'synth.SetOutputToDefaultAudioDevice();'
s←⎕cse 'Exec' (⊃s)
:end
⍝ret←⎕cse 'ExecStmt' ('synth.Speak("',text,'");') ⍝ play syncronously
ret←⎕cse 'ExecStmt' ('synth.SpeakAsync("',text,'");') ⍝ play asyncronously
The Namespace 'System.Speech' contains the System.Speech.Synthesis namespace and the using statement creates a shortcut to the SpeechSynthesizer, otherwise it would have needed to be called by typing System.Speech.Synthesis.SpeechSynthesize.
'System.Speech.Synthesis.SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer();'
using simplifies this to:
'SpeechSynthesizer synth = new SpeechSynthesizer();'
CSSpeak 'Hello World'
Second ⎕CSE program:
- Code: Select all
ret←larg CSPlayFile file;⎕cself;sink;csobj
csobj←⎕vget 'CSobj' 'Play'
:if ⎕dyadic
ret←csobj ⎕cse 'Close'
:returnif 0=⍴,file
:end
:if~(⊂⎕cself←csobj)∊'#'⎕cse'instances'
⎕cself←csobj ⎕cse 'Init' 'System' 'PresentationCore' 'WindowsBase'
sink←⎕cse'returnonerror'1
sink←⎕cse 'ExecStmt' 'var player = new System.Windows.Media.MediaPlayer();'
:end
sink←⎕cse 'ExecStmt' ('player.Open(new System.Uri(@"',file,'"));')
ret←⎕cse 'ExecStmt' 'player.Play();'
CSPlayFile 'c:\windows\media\tada.wav'
The Uri Method is a member of the System Namespace. System.Windows.Media.MediaPlayer references both the PresentationCore and WindowsBase Namespaces and thus they must be "referenced" in the ⎕CSE "Init" method.
The .NET framework has thousands of methods in many different namespaces. The Object Explorer in Visual Studio is useful for finding how to reference a given method. And the Internet is helpful for finding out how to use the various methods, including places such as StackExchange.
I found the videos on C# for the Absolute Beginners to be very helpful.
http://channel9.msdn.com/Series/C-Funda ... -Beginners
I have not been able to write ⎕CSE code to access the console. The code seems to execute without error, but no concole window opens. ???