Como aspecto para poder mostrar los resultados en el notebook se va a empezar explicando la forma de mostrar mensajes en pantalla con el comando println()
println("Hello Julia!")
Los comentarios de una sola linea empiezan con #, mientras que los multilinea empiezan con #= y terminan con =#
println("Este si")
# println("Este no")
#=
println("Mucho menos")
println("estos dos")
=#
Este si
some_var = 5
x = "Hello Julia"
X = 4.914
esta!esUna_cadena = "En realidad si lo es"
println(some_var)
println(x)
println(X)
println(esta!esUna_cadena)
5 Hello Julia 4.914 En realidad si lo es
try
som_other_var
catch e
println(e)
end
UndefVarError(:som_other_var)
Para poder asignar nombres de esta manera es necesario escrbir el simbolo de LaTeX seguido de la tecla tab.
δ = 4.5
println(δ)
println(pi)
pi = "Ya no es un numero"
println(pi)
4.5 π = 3.1415926535897... Ya no es un numero
WARNING: imported binding for pi overwritten in module Main
WORD_SIZE
64
g = 1.0
m = 4.
e = .5
G = 1e10
M = 2.5e-4
E = -.4e-10
println(g)
println(m)
println(e)
println(G)
println(M)
println(E)
1.0 4.0 0.5 1.0e10 0.00025 -4.0e-11
println(typeof(WORD_SIZE))
println(typeof(g))
println(typeof(E))
Int64 Float64 Float64
println(1/Inf)
println(1/0)
println(0/0)
0.0 Inf NaN
println(sqrt(complex(-4)))
println((1+2im)*(-3+2im))
0.0 + 2.0im -7 - 4im
println(2//3)
println(6//9)
println(-4//8)
println(-4//-12)
println(den(2//3))
println(num(2//3))
2//3 2//3 -1//2 1//3 3 2
println(1+2+3)
println(1-2)
println(3*2/12)
6 -1 0.5
println(~123)
println(123&234)
println(123|234)
-124 106 251
x = 1
x += 3
println(x)
4
println(1==1)
println(1==2)
println(1!=1)
println(1==1.0)
true false false true
println(Int8(127))
println(Int8(128))
println(Int8(127.0))
println(Int8(3.14165))
127
LoadError: InexactError() while loading In[17], in expression starting on line 2 in call at essentials.jl:56
println("---Redondeo")
println(round(3.14165))
println(ceil(3.14165))
println(floor(3.14165))
println("---Divisiones")
println(fld(3,4))
println(cld(3,4))
println("---Valor absoluto")
println(abs(-3.14165))
println(abs2(-3.14165))
println("---Funciones matemáticas")
println(123456789^2)
println(sqrt(15241578750190521))
println(log2(1024))
---Redondeo 3.0 4.0 3.0 ---Divisiones 0 1 ---Valor absoluto 3.14165 9.869964722499999 ---Funciones matemáticas 15241578750190521 1.23456789e8 10.0
a = Int64[] # arreglo vacio
push!(a,1) # => [1]
push!(a,2) # => [1,2]
a[1]
1
b = [4, 5, 6] # => Int64 [4, 5, 6]
b = [4; 5; 6] # => Int64 [4, 5, 6]
b = Int8[4, 5, 6] # => Int8 [4, 5, 6]
println(b[1]) # => 4
println(b[end]) # => 6
4 6
arr = [5,4,6]
arr1 = [4,5,6]
println(sort(arr))
println(sort!(arr1))
[4,5,6] [4,5,6]
a = [1:5;] # => Int64 [1,2,3,4,5]
println(a)
println(a[1:3])
println(a[2:end])
[1,2,3,4,5] [1,2,3] [2,3,4,5]
tupla = (1, 2, 3)
otra_tupla = 1, 2, 3
(1,2,3)
empty_dict = Dict() # instanciación vacía
filled_dict = Dict("one"=> 1, "two"=> 2, "three"=> 3) # instanciación con elementos
Dict{ASCIIString,Int64} with 3 entries: "two" => 2 "one" => 1 "three" => 3
println(keys(filled_dict))
println(values(filled_dict))
ASCIIString["two","one","three"] [2,1,3]
println(in(("one" => 1), filled_dict)) # => true
println(in(("two" => 3), filled_dict)) # => false
println(haskey(filled_dict, "one")) # => true
println(haskey(filled_dict, 1)) # => false
true false true false
empty_set = Set() # => Set{Any}()
filled_set = Set([1,2,2,3,4]) # => Set{Int64}(1,2,3,4)
Set([4,2,3,1])
function f(x,y)
x + y
end
f_prod(x,y) = x * y # => "f_prod (generic function with 1 method)"
5 6
println(f(2,3))
5
println(f_prod(2,3))
6
g = f;
println(g(2,3))
5
En Principio las funciones retoRnan el valor de la última expresión evaluada, sin embargo si hay un return, se va a retornar este valor
function prod(x,y)
return x * y
x + y
end
prod (generic function with 1 method)
prod(3,5)
15
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 (generic function with 1 method)
hypot(3,4)
5.0
hypot(5,2)
5.385164807134505
hypot(3,0)
3.0
1 + 2 + 3 + 4 + 5 + 6
21
+(1,2,3,4,5,6)
21
f_aritmeticas(x, y) = x + y, x - y, x * y, x / y
f_aritmeticas(3, 4) # => (7, -1)
(7,-1,12,0.75)
function varargs(args...)
println(args)
end
varargs(1,2)
varargs(1,2,3,4)
(1,2) (1,2,3,4)
function defaults(a,b,x=5,y=6)
return "$a $b and $x $y"
end
defaults(1,2)
defaults(1,2,3)
function keyword_args(;k1=4,name2="hello") # importante el ;
return Dict("k1"=>k1,"name2"=>name2)
end
keyword_args(name2="ness") # => ["name2"=>"ness","k1"=>4]
Dict{ASCIIString,Any} with 2 entries: "name2" => "ness" "k1" => 4
function test(x,y)
if x < y
relation = "less than"
elseif x == y
relation = "equal to"
else
relation = "greater than"
end
println("x is ", relation, " y.")
end
end
LoadError: syntax: unexpected end while loading In[82], in expression starting on line 11
test(1,2)
x is less than y.
test(2979,2514)
x is greater than y.
test(32.0,32)
x is equal to y.
f_otro_test(x,y) = println(x < y ? "less than" : "not less than")
f_otro_test (generic function with 1 method)
f_otro_test(1,2)
less than
f_otro_test(3,1)
not less than
i = 1;
while i <= 5
println(i)
i += 1
end
1 2 3 4 5
for i = 1:5
println(i)
end
1 2 3 4 5
for s in ["foo","bar","baz"]
println(s)
end
foo bar baz
try
some_other_var # => ERROR: some_other_var no definida
catch e
println(e)
end
UndefVarError(:some_other_var)
function producer()
produce("start")
for n=1:4
produce(2n)
end
produce("stop")
end;
p = Task(producer);
consume(p)
consume(p)
consume(p)