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]
Out[3]:
In [4]:
[a,b,c]
Out[4]:
Curly braces may also be used to enter lists in Symata. This may change in the future.
In [5]:
{a,b,c}
Out[5]:
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.
In [6]:
[
a
c + d
"cat"
Expand((x+y)^2)
]
Out[6]:
In [7]:
f(x)
Out[7]:
In [8]:
e1 = (1,2,a+b)
e2 = (1,
2,
a+b)
e3 = begin
1
2
a+b
end
e1 == e2 == e3
Out[8]:
Map
In [9]:
f % list
Out[9]:
In [10]:
f % [a,b,c]
Out[10]:
Apply
In [11]:
x .% y
Out[11]:
In [12]:
f .% g(1,2)
Out[12]:
In [ ]:
Rule
Rule can be entered in the following ways. The symbol ⇒ can be entered with \Rightarrow[TAB]
In [13]:
[Rule(a,b), a => b , a ⇒ b]
Out[13]:
RuleDelayed
In [14]:
[RuleDelayed(a,b), a .> b]
Out[14]:
ReplaceAll
The short "infix" symbol for ReplaceAll is ./. In Mathematica, it is /..
Also note the parentheses surrounding the rule.
In [15]:
[a, x^2, b^3, (a+b)^3] ./ ( x_^n_ => g([n],x))
Out[15]:
In [16]:
f(x_, y__) := [x,[y]]
f(1,[2,3,4])
Out[16]:
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 [17]:
[
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[17]:
Repeated can be used in operator form.
In [18]:
ClearAll(f)
f(x_...) := x
In [19]:
f(3,3,3,3)
Out[19]:
In Mathematica, Repeated[expr,n] matches at least n occurences of expr. In Symata, Repeated(expr,n) does the same.
In [20]:
[
MatchQ([1,2,3], [Repeated(_Integer,2)])
MatchQ([1,2,3], [Repeated(_Integer,3)])
]
Out[20]:
In [21]:
MatchQ([], [RepeatedNull(_Integer)])
Out[21]:
As in Mathematica, default values of optional arguments are specified using :.
In [22]:
ClearAll(fa,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[22]:
In Mathematica, names for complex patterns use a colon a:(b_^c_). In Symata, use two colons.
In [23]:
ClearAll(g,a,b)
b^b ./ ( a::(_^_) => g(a) )
Out[23]:
In [24]:
countprimes = Count(_:?(PrimeQ))
countprimes(Range(100))
Out[24]:
You can use a Julia function as the test.
In [25]:
p = _:?(J( x -> -1 < x < 1 ))
[
MatchQ(0,p)
MatchQ(.5,p)
MatchQ(-1/2,p)
MatchQ(-1,p)
]
Out[25]:
In [26]:
ClearAll(f)
f(x_, x_ | y_String) := [x,y]
[ f(2,2) , f(2,"cat")]
Out[26]:
In [27]:
[
MatchQ( -2 , Condition( x_ , x < 0))
MatchQ( 2 , Condition( x_ , x < 0))
]
Out[27]:
In [28]:
ClearAll(y)
ReplaceAll([1,2,3, "cat"], x_Integer => Condition( y, x > 2))
Out[28]:
In [29]:
ClearAll(f)
f(x_) := Condition(x^2, x > 3)
[f(2),f(4)]
Out[29]: