In [1]:
using Symata
In [2]:
[2x + 1, 2 * x + 1, 2 * 3, Cos(x) * Sin(x)]
Out[2]:
In [3]:
# a = 1
b = 2
[a,b]
In [4]:
[a,b,c]
Out[4]:
Elements in a list may be separated by commas, as above. But, in Symata they may also be separated by a newline after a complete expression. (This does not work if the code is loaded from a file with Get)
In [5]:
[
a
c + d
"cat"
Expand((x+y)^2)
]
Out[5]:
In [6]:
f(x)
Out[6]:
In [7]:
e1 = (1,2,a+b)
e2 = (1,
2,
a+b)
e3 = begin
1;
2;
a+b
end
e1 == e2 == e3
Out[7]:
Map
In [8]:
f % list
Out[8]:
In [9]:
f % [a,b,c]
Out[9]:
Apply
In [10]:
x .% y
Out[10]:
In [11]:
f .% g(1,2)
Out[11]:
Rule
Rule can be entered in the following ways. The symbol ⇒ can be entered with \Rightarrow[TAB]
In [12]:
[Rule(a,b), a => b , a ⇒ b]
Out[12]:
RuleDelayed
In [13]:
[RuleDelayed(a,b), a .> b]
Out[13]:
ReplaceAll
The short "infix" symbol for ReplaceAll is ./. In Mathematica, it is /..
Also note the parentheses surrounding the rule.
In [14]:
[a, x^2, b^3, (a+b)^3] ./ ( x_^n_ => g([n],x))
Out[14]:
In [15]:
f(x_, y__) := [x,[y]]
f(1,2,3,4)
Out[15]:
In Mathematica, Repeated[a] is denoted by a... In Symata, Repeated(a) is denoted by a.... Notice that in Symata, there are three dots instead of two.
In [16]:
[
MatchQ([a, a, b, (a + b)^3, c, c, c], [a..., b, _^3... , c...])
MatchQ([a, a, (a + b)^3, c, c, c], [a..., b, _^3... , c...])
]
Out[16]:
Repeated can be used in operator form.
In [17]:
ClearAll(f)
f(x_...) := x
In [18]:
f(3,3,3,3)
Out[18]:
In Mathematica, Repeated[expr,n] matches at least n occurences of expr. In Symata, Repeated(expr,n) does the same.
In [19]:
[
MatchQ([1,2,3], [Repeated(_Integer,2)])
MatchQ([1,2,3], [Repeated(_Integer,3)])
]
Out[19]:
In [20]:
MatchQ([], [RepeatedNull(_Integer)])
Out[20]:
As in Mathematica, default values of optional arguments are specified using :.
In [21]:
ClearAll(f, a, b)
f(x_, y_:a, z_:b) := [x,y,z]
[
f(1) == [1,a,b]
f(1,2) == [1,2,b]
f(1,2,3) == [1,2,3]
]
Out[21]:
In Mathematica, names for complex patterns use a colon a:(b_^c_). In Symata, use two colons.
In [22]:
ClearAll(g,a,b)
b^b ./ (a::(_^_) => g(a))
Out[22]:
In [23]:
countprimes = Count(_`PrimeQ`) # We use the curried form of Count
countprimes(Range(100))
Out[23]:
You can use a Julia function as the test.
In [24]:
p = _`J( x -> -1 < x < 1 )`
[
MatchQ(0, p),
MatchQ(.5, p),
MatchQ(-1/2, p),
MatchQ(-1, p)
]
Out[24]:
In [25]:
ClearAll(f)
f(x_, x_ | y_String) := [x, y]
[ f(2,2) , f(2,"cat"), f(2, 3)]
Out[25]:
In [26]:
[
MatchQ( -2 , Condition(x_ , x < 0))
MatchQ( 2 , Condition(x_ , x < 0))
]
Out[26]:
In [27]:
ClearAll(y)
ReplaceAll([1, 2, 3, "cat"], x_Integer => Condition(y, x > 2))
Out[27]:
In [28]:
ClearAll(f)
f(x_) := Condition(x^2, x > 3)
[f(2), f(4)]
Out[28]:
In [29]:
VersionInfo()
In [30]:
InputForm(Now())
Out[30]: