In [1]:
println("Greetings! স্বাগতম!")
In [2]:
julia -v
In [3]:
print(julia -v)
In [4]:
pi
Out[4]:
In [5]:
typeof(1)
Out[5]:
In [6]:
WORD_SIZE
Out[6]:
In [7]:
typeof(ans)
Out[7]:
In [8]:
1/Inf
Out[8]:
In [9]:
1/0
Out[9]:
In [10]:
x = 3
(x-1)x
Out[10]:
In [11]:
x = 1
x+= 3
Out[11]:
In [12]:
z = hypot(1, 3)
Out[12]:
In [13]:
! + 2 im
In [14]:
1 + 2im
Out[14]:
In [15]:
sqrt(4)
Out[15]:
In [16]:
sqrt(-1)
In [17]:
2//3
Out[17]:
In [18]:
Int("x")
In [19]:
Int('x')
In [20]:
Char(120)
In [21]:
'A' < 'a'
Out[21]:
In [22]:
str = "Hello, World"
Out[22]:
In [23]:
str[1]
Out[23]:
In [24]:
str[10]
Out[24]:
In [25]:
str[end]
Out[25]:
In [26]:
endof(str)
Out[26]:
In [1]:
mystring = "Hello World"
Out[1]:
In [2]:
mystring[6:6]
Out[2]:
In [3]:
mystring[6:7]
Out[3]:
In [4]:
this67 = mystring[6:7]
Out[4]:
In [5]:
typeof(this67)
Out[5]:
In [6]:
length(mystring)
Out[6]:
In [7]:
for i = 1:endof(mystring)
try
println(mystring[i])
catch
#ignore index error
end
end
In [9]:
greet = "Hello"
whom = "World"
bothtogether = string(greet, ", ", whom)
Out[9]:
In [10]:
$greet $whom
In [11]:
"$greet, $whom"
Out[11]:
In [12]:
v = [1,2,3]
Out[12]:
In [13]:
world = "world"
"Hello, $world"
Out[13]:
In [14]:
contains("Hello World", "world")
Out[14]:
In [15]:
contains("Hello World", "World")
Out[15]:
In [16]:
contains("Hello World", 'o')
In [17]:
in("Hello World", 'o')
Out[17]:
In [18]:
# create repeats
myreps = repeat("x", 10)
Out[18]:
In [19]:
# you can join words and letters
thisJoined = join(["a", "b"],"," )
Out[19]:
In [23]:
myreg = r"^\x*(?:#|$)"
Out[23]:
In [21]:
typeof(ans)
Out[21]:
In [24]:
ismatch(myreg, "not a comment")
Out[24]:
In [25]:
match(myreg, "not a comment")
# will not result in anything
In [26]:
match(myreg, "# a comment")
# will result in hash
Out[26]:
In [27]:
function myf(x,y)
z = x * y
end
myf(2,3)
Out[27]:
In [28]:
myf
Out[28]:
In [31]:
function g(x,y)
#return x * y
return x + y
end
g(2,3)
Out[31]:
In [32]:
+(1,2,3)
Out[32]:
In [46]:
function hypot(x,y)
x = abs(x)
y = abs(y)
if x > y
r = y/x
return x * sqrt(1+r*r)
end
if y == 0
return zero(x)
end
r = x/y
return y*sqrt(1+r*r)
end
hypot(5,2)
Out[46]:
In [48]:
hypot(6,5)
Out[48]:
In [49]:
hypot(6,0)
Out[49]:
In [50]:
*(1,2,4)
Out[50]:
In [51]:
function foo(a,b)
a+b, a*b
end
foo(2,3)
Out[51]:
In [52]:
x,y = foo(2,3); x
Out[52]:
In [53]:
y
Out[53]:
In [54]:
## compound expressions
# single expression evaluates several subexpressions
# returns the value of last expression
x = begin
a = 1
b = 2
a+b
end
Out[54]:
In [55]:
x
Out[55]:
In [56]:
# Another way to write the same thing
p = (d = 1; e = 2; d + e)
p
Out[56]:
In [58]:
# Yet another way
q = begin g1 = 1; h = 2; g1 + h end
q
Out[58]:
In [59]:
## Conditional evaluation
a = 10
b = 20
function test(a,b)
if a < b
println("a is less than b")
elseif a == b
println("a is equal to b")
else
println("a is not less than b")
end
end
Out[59]:
In [60]:
test(10,20)
In [61]:
test(20,10)
In [62]:
test (10, 10)
In [65]:
## if blocks are leaky and you can use new variables within if
function test2(x,y)
if x < y
relation = "less"
elseif x == y
relation = "same"
else
relation = "more"
end
println("x is ", relation, " y")
end
test2(10, 20)
In [66]:
# note the variable "relation" is defined within if block
# it can still be used outside of the if block
In [67]:
# Example of chaining
x = 1; y = 2
println(x < y ? "less than" : "more than")
In [68]:
x = 7; y = 2
println(x < y ? "less than" : "more than")
In [69]:
i = 1;
while i <= 5
println(i)
i += 1
end
In [71]:
## use of for
for i = 1:5
println(i)
end
In [72]:
function producer()
produce("start")
for n = 1:4 # that is n has a value from 1 to 4
produce(2n) # multiple n with 2
end
produce("stop")
end;
## now, we are going to consume the values produced
p = Task(producer)
consume(p)
Out[72]:
In [73]:
consume(p)
Out[73]:
In [74]:
# what happens when we do a for loop on this?
for x in Task(producer)
println(x)
end
In [75]:
function foo(n)
x = 0
for i = 1:n
x = x + 1
end
x
end
Out[75]:
In [76]:
foo(10)
Out[76]:
In [77]:
function foo(n)
x = 0
for i = 1:n
local x
x = 1
end
x
end
foo(10)
Out[77]:
In [78]:
# x need not come before
function foo(n)
f = y -> n + x + y
x = 1
f(2)
end
foo(10)
Out[78]:
In [79]:
const e
In [80]:
const e = 2.71
const pi = 3.141
In [81]:
methods(+)
Out[81]:
In [82]:
methods(for)
In [83]:
methods("for")
In [84]:
mean(Squares(100))
In [85]:
25 in Squares(10)
In [90]:
immutable Squares
count::Int
end
Base.start(::Squares) = 1
Base.next(S::Squares, state) = (state*state, state+1)
Base.done(S::Squares, s) = s > S.count
Base.eltype(::Type{Squares}) = Int
Base.length(S::Squares) = S.count
for i in Squares(7)
println(1)
end
In [91]:
25 in Squares(10)
Out[91]:
In [92]:
mean(Squares(100))
Out[92]:
In [93]:
for i in Squares(7)
println(i)
end
In [94]:
prog = "1 + 1" # julia programme starts as a string
Out[94]:
In [95]:
## parse each string such as 1+1 into an object.
# this object is called expression
expression1 = parse(prog)
expression1
Out[95]:
In [96]:
## This has three parts:
# A symbol
expression1.head
Out[96]:
In [97]:
# next, expression arguments,
expression1.args
Out[97]:
In [98]:
# third, the result type
expression1.typ
Out[98]:
In [99]:
# Another way to write the programme above,
Exp2 = Expr(:call, :+, 1, 1)
Out[99]:
In [101]:
## Expression1 and Exp2 are same, see
expression1 == Exp2 # will return true
Out[101]:
In [102]:
## Here is the data structure
datastruct = dump(Exp2)
In [103]:
# ":" is a symbol
:foo
Out[103]:
In [104]:
typeof(ans)
Out[104]:
In [105]:
# Use : to create expression objectis
ex1 = :(a+b*c+1)
Out[105]:
In [106]:
typeof(ex1)
Out[106]:
In [107]:
## ehre is the structure
parse(ex1)
In [108]:
ex1.head
Out[108]:
In [109]:
dump(ex1)
In [110]:
eval(ex1)
In [111]:
:(1+2)
Out[111]:
In [112]:
eval(ans)
Out[112]:
In [113]:
ex = :(a+b)
Out[113]:
In [114]:
eval(ex)
Out[114]:
In [115]:
a = 5; b = 10
Out[115]:
In [116]:
eval(ex)
Out[116]:
In [117]:
macro sayHello()
return :(println("Hello World"))
end
In [118]:
@sayHello()
In [119]:
macro sayHello(name)
return :(println("Hello ", $name))
end
@sayHello(John)
In [120]:
@sayHello("John")
In [121]:
const x = rand(8)
In [122]:
x = rand(8)
Out[122]:
In [131]:
Float64[ 0.25 * x[i-1] for i = 2:length(x)-1]
Out[131]:
In [132]:
Pkg.status()
In [133]:
julia -v
In [134]:
versioninfo()
Beyond this, I am going to update Julia to version. 0.4 and update Ijulia as well. So download Julia, install, then do Pkg.add(IJulia)
In [ ]: