In Erlang, when do I use ; or , or .? - syntax

I have been trying to learn Erlang and have been running into some problems with ending lines in functions and case statements.
When do I use a semicolon (;), comma (,), or period inside my functions or case statements?

I like to read semicolon as OR, comma as AND, full stop as END. So
foo(X) when X > 0; X < 7 ->
Y = X * 2,
case Y of
12 -> bar;
_ -> ook
end;
foo(0) -> zero.
reads as
foo(X) when X > 0 *OR* X < 7 ->
Y = X * 2 *AND*
case Y of
12 -> bar *OR*
_ -> ok
end *OR*
foo(0) -> zero *END*
This should make it clear why there is no ; after the last clause of a case.

Comma at the end of a line of normal code.
Semicolon at the end of case statement, or if statement, etc.
The last case or if statement doesn't have anything at the end.
A period at the end of a function.
example (sorry for the random variable names, clearly this doesn't do anything, but illustrates a point):
case Something of
ok ->
R = 1, %% comma, end of a line inside a case
T = 2; %% semi colon, end of a case, but not the end of the last
error ->
P = 1, %% comma, end of a line inside a case
M = 2 %% nothing, end of the last case
end. %% period, assuming this is the end of the function, comma if not the end of the function

Period (.)
In modules, the period is used to terminate module attributes and function declarations (a.k.a. 'forms'). You can remember this because forms aren't expressions (no value is returned from them), and therefore the period represents the end of a statement.
Keep in mind that definitions of functions with different arities are considered separate statements, so each would be terminated by a period.
For example, the function definitions for hello/0 and hello/1:
hello() -> hello_world.
hello(Greeting) -> Greeting.
(Note that in the erlang shell the period is used to terminate and evaluate expressions, but that is an anomaly.)
Semicolon (;)
The semicolon acts as a clause separator, both for function clauses and expression branches.
Example 1, function clauses:
factorial(0) -> 1;
factorial(N) -> N * fac(N-1).
Example 2, expression branches:
if X < 0 -> negative;
X > 0 -> positive;
X == 0 -> zero
end
Comma (,)
The comma is an expression separator. If a comma follows an expression, it means there's another expression after it in the clause.
hello(Greeting, Name) ->
FullGreeting = Greeting ++ ", " ++ Name,
FullGreeting.

You can think of it like english punctuation. Commas are used to separate things in a series, semicolons are used to separate two very closely related independent clauses[1] (e.g. the different cases of the case statement, function clauses of the same name and arity that match different patterns), and periods are used to end a sentence (complete thought).
Or to prove you went to college. "Do not use semicolons. They are transvestite hermaphrodites representing absolutely nothing. All they do is show you've been to college." -- Kurt Vonnegut

The comma separates expressions, or arguments, or elements of a list/tuple or binary. It is overworked.

Related

How to Concatenate two variable in algorithm?

I need to add a statement (contains three words that come from 3 variables) to a file
for example:
Algorithm1
Input X,Y,Z: input words
Output: file:text file
Begin
...\\some operations
...\\some operations
...\\some operations
file<-- X + Y + Z
End
I need a concatenation symbol other than +, which is used for mathematical operations, to concatenate X, Y, and Z and addd them as one statement in a file.

Understanding different types in Fortran

I was reading a Fortran code, came across the following code, couldn't understand what it does.
m%AllOuts( BAzimuth(k) ) = m%BEMT_u(indx)%psi(k)*R2D
I know that % here works like a pipe indicator to access values in a way similar to a dictionary in Python. I have a dictionary m let's say and the first key is AllOuts, but what does anything inside parentheses mean? Is it like another dictionary?
The percent sign is not denoting a dictionary. There are no native dictionaries in Fortran.
The percent sign denotes the component of a type. For example:
! Declare a type
type :: rectangle
integer :: x, y
character(len=8) :: color
end type rectangle
! Declare a variable of this type
type(rectangle) :: my_rect
! Use the type
my_rect % x = 4
my_rect % y = 3
my_rect % color = 'red'
print *, "Area: ", my_rect % x * my_rect % y
The parentheses could either indicate the index of an array, or the arguments of a call.
So, for example:
integer, dimension(10) :: a
a(8) = 16 ! write the number 16 to the 8th element of array a
Or, as a prodedure:
print *, my_pow(2, 3)
...
contains
function my_pow(a, b)
integer, intent(in) :: a, b
my_pow = a ** b
end function my_pow
In order to figure out what m is, you'd need to look at the declaration of m, which would be something like
type(sometype) :: m
or
class(sometype) :: m
Then you'd need to find out the type declaration, which would be something like
type :: sometype
! component declarations in here
end type
Now one of the components, BEMT_u, is almost certainly an array of a different type, which you'd also need to look up.

About Prolog syntax

Sometimes I see terms like:
X = a:b
or
X = a-b
I can do requests like
X = Y:Z
and the compiler unifies Y with a and Z with b, as expected.
Now my answer:
Which characters (or sequence of characters) am I allowed to use to combine two Prolog atoms?!
Maybe you can give me some links with further informations about this issue.
Thanks for your help and kind regards from Germany
Which characters (or sequence of characters) am I allowed to use to combine two Prolog atoms?!
What you are asking here for, is the entire operator syntax definition of Prolog. To get the very full answer to this, please refer to the tag iso-prolog for full information how to obtain the Prolog standard ISO/IEC 13211-1.
But as a short answer to start with:
Prolog syntax consists of
functional notation, like +(a,b), plus
a dynamically redefinable operator syntax, plus
some extra.
It seems you want to know which "characters" can be used as operators.
The short answer is that you can use all atoms Op that succeed for current_op(Pri,Fix,Op). So you can ask dynamically, which operators are present:
?- current_op(Pri, Fix, Op).
Pri = 1, Fix = fx, Op = ($)
; Pri = 1150, Fix = fx, Op = (module_transparent)
; Pri = 700, Fix = xfx, Op = (=#=)
; Pri = 700, Fix = xfx, Op = (#>=)
; Pri = 700, Fix = xfx, Op = (>=)
; ... .
All those operators can be used in the specified manner, as pre-, in-, or postfix with the indicated priorities. Some of these operators are specific to SWI, and some are defined by the standard. Above, only #>= and >= are standard operators.
Most of the operators consist of the graphic characters #$&*+-./:<=>?#^~ only or of letters, digits and underscores starting with a lower case letter. There are two solo characters !; and then there are ,| which are even more special. Operator names that are different to above need quoting - you rarely will encounter them.
To see how operators nest, use write_canonical(Term).
The long answer is that you are also able to define such operators yourself. However, be aware that changing the operator syntax has often many implications that are very difficult to fathom. Even more so, since many systems differ in some rarely used configurations. For example, the system you mentioned, SWI differs in several ways.
I'd suggest to avoid defining new operators until you have learned more about the Prolog language.
let's see what's inside X = Y:Z
?- display( X = Y:Z ).
=(_G3,:(_G1,_G2))
true.
then we have a nested structure, where functors are operators.
An operator is an atom, and the rule for atom syntax says that we have 3 kind to consider:
a sequence of any printable character enclosed in single quote
a sequence of special characters only, where a special character is one of `.=:-+*/><##~? (I hope I have found all of them, from this page you can check if I forgot someone !)
a sequence of lowercase/uppercase characters or the underscore, starting with a lowercase character
edit
A functor (shorthand for function constructor, I think, but function is misleading in Prolog context) it's the symbol that 'ties' several arguments. The number of arguments is named arity. In Prolog a term is an atomic literal (like a number, or an atom), or a recursive structure, composed of a functor and a number of arguments, each being a term itself (at least 1).
Given the appropriate declaration, i.e. op/3, unary and binary terms can be represented as expressions, like that one you show.
An example of operator, using the : special char, is ':-'
member(X,[X|_]).
member(X,[_|T]) :- member(X, T).
The O.P., said (and I quote):
Sometimes I see terms like: X = a:b or X = a-b
I can do requests like X = Y:Z and the compiler unifies Y with a and Z with b, as expected.
Now my answer: Which characters (or sequence of characters) am I allowed
to use to combine two Prolog atoms?!
The short answer is Pretty much whatever you want (provided it is an atom).
The longer answer is this:
What are seeing are infix (x infix_op b), prefix (pfx_op b) and suffix (b sfx_op ) operators. Any structure with an arity of 2 can be an infix operator. Any structure with an arity of 1 can be a prefix or suffix operator. As a result, any atom may be an operator.
Prolog is parsed via a precedence driven, recursive descent parser (written in Prolog, naturally). Operators are defined and enumerated, along with their precedence and associativity in the operator/3 predicate. Associativity has to do with how the parse tree is constructed. An expression like a - b - c could be parsed as ( a - ( b - c ) ) (right-associative), or ( ( a - b ) - c ) (left-associative).
Precedence has to do with how tightly operators bind. An expression like a + b * c binds as ( a + ( b * c ) not because of associativity, but because '*'/2 (multiplication) has higher precedence that '+'/2 (addition).
You can add, remove and change operators to your heart's content. Not that this gives you a lot of room to shoot yourself in the foot by breaking prolog's syntax.
It should be noted, however, that any operator expression can also be written via ordinary notation:
a + b * c
is exactly identical to
'+'( a , '*'(b,c) )

Mathematica not interpreting CenterDot as Times in numerical calculations

Why doesn't Mathematica show the numerical result of
(0.8\[CenterDot]452\[CenterDot]20+1.5\[CenterDot]4180\[CenterDot]10
-2\[CenterDot]900\[CenterDot]100) / (0.8\[CenterDot]452
+1.5\[CenterDot]4180-1\[CenterDot]2\[CenterDot]900) // N
Just to complete some of the other answers/comments, if you want CenterDot to be interpreted as Times in both input and output by using something like
Unprotect[CenterDot, Times];
CenterDot = Times;
Times /: MakeBoxes[Times[a__], fmt_] :=
With[{cbox = ToBoxes[HoldForm[CenterDot[a]]]},
InterpretationBox[cbox, Times[a]]];
Protect[CenterDot, Times];
Which you can add to your init.m if you want it loaded by default.
This works on both numeric and symbolic expressions, e.g.
In[5]:= 1\[CenterDot]2\[CenterDot]3
Out[5]= 6
In[6]:= a b c
Out[6]= a\[CenterDot]b\[CenterDot]c
You can also make the automatically inserted multiplication symbol between space separated numbers be CenterDot by executing
SetOptions[EvaluationNotebook[],
{AutoMultiplicationSymbol -> True, NumberMultiplier -> "\[CenterDot]"}]
or by selecting Center Dot in the preferences dialog under Appearance > Numbers > Multiplication.
For example:
Just replace \[CenterDot] by a space
Multiplication in Mathematica is written either as a space (Times[a,b] == a b) or as an asterisk (Times[a,b] == a*b). \[CenterDot] is not interpreted as multiplication.
I think Simon's first method can be written more concisely. Please review:
Unprotect[Times];
CenterDot = Times;
Format[a_*b__] := Interpretation[HoldForm[a\[CenterDot]b], a*b];
Second attempt. I believe this works properly with Convert To > StandardForm and editing.
CenterDot = Times;
MakeBoxes[Times[x__], _] := RowBox # Riffle[ToBoxes /# {x}, "\[CenterDot]"]

In Go, how to write a multi-line statement?

In python, we use backslash to indicate that the current statement continues to next line
for example,
a = b + c + s \
+ x + y
or simply,
a = b + c + s +
x + y
Is it possible to do that in Go language? Thanks
Sure it is, just put an operator at the end, for example:
a = b + c + s +
x + y
Also note that it's not possible to break the line before the operator. The following code is invalid:
a = b + c + s
+ x + y
The rule is described here and in the specification.
Interestingly, the the Go language specification itself requires semicolons at the end of each statement, but the lexer will insert implicit semicolons at the end of lines that look like statements immediately before compilation.
Therefore, to prevent the unwanted semicolon at the end of an unfinished line, all you need to do is ensure that the line doesn't end with something that could make it look like a complete statement.
In other words, avoid ending an incomplete line in a variable, constant, function, keyword, or postfix operator (e.g. ++).
What does that leave? Well, a few things come to mind -- an infix operator (e.g. = or +), a comma, or an opening paren or brace or bracket, for example.

Resources