println("Hello, world!") function sayhello(name) println("Hello, ", name) end sayhello("friend!") print(1) print(" 2") i = 2.0 e = 1+1im supercalifragilisticexpialidocious = pi supercalifragilisticexpialidocious/2 end=0.5im supercalifragilisticexpialidocious == pi pi == 3.2 # Sorry, Indiana pi < 3.2 isapprox(exp(im * pi) + 1, 0) 'a' == char(97) # ASCII 97 is 'a' println("1\n2") #Newline println("1\t2") #Tab println("1\\2") #backslash println("\$") println("$pi") println("$α") α = 7.29735257E-3 z = 3+4im ξ = 1/z アルコール = 0.1337 アルコール^2 print(char(0x1f355)) print(" = ") print('\U263A') re = r".*(brown fox).*(lazy dog)" m = match(re, "The quick brown fox jumped over the lazy dog.") m.captures[1] m.captures[2] if false println("Twilight zone...") elseif ( 1 == 2 ) println("We can actually do math, right?") else println("All is well. Both conditions are false.") end for i in 1:10 print(i, " ") end for i in 1:10 (i > 5 && i < 9) && continue print(i, " ") end # opening a file: vecfile = open("/tmp/myvector.txt", "w") function writenumbers(fhandle) # implementation goes here end writenumbers(vecfile) close(vecfile) f_in = open("/tmp/myvector.txt", "r") myvec1 = split( readall(f_in), "," ) seekstart(f_in) myvec1 = readcsv( f_in ) allmixedup = { 1, pi, "foo", myvec1, "biggles", sayhello } allmixedup[1] allmixedup[5] allmixedup[6] push!(allmixedup, "another random string") mymap = { 1 => "one", "two" => 2, "sayhello" => sayhello, sayhello => "function called `sayhello`"} mymap[7.3] = "seven point 3" mymap[1] mymap["two"] mymap["sayhello"] mymap[sayhello] mymap[7.3] bitstype 64 Int64 <: Signed 0b10 == 2 # There are 10 kinds of programmers... [ 0 bits(0); 1 bits(1); 2 bits(2); 3 bits(3); 7 bits(7); 8 bits(8); 16 bits(16); 17 bits(17); 32 bits(32); 33 bits(33); 35 bits(35); 64 bits(64);] x = 1 typeof(x) [ typemin(Uint8) typemax(Uint8) ; typemin(Int8) typemax(Int8) ; typemin(Int64) typemax(Int64) ; ] y = 2.0 [ 2. typeof(2.); .5 typeof(.5); ] 0 == -0 [ " 0" bits(0.0) ; "-0" bits(-0.0) ; ] [ 1.0 bits(1.0) repr(1.0 + eps(1.0)) bits(1.0 + eps(Float64)) ] typemax(Int64) typemax(Int64) + 1 z=3+4im z+z z*z' # z' denotes the conjugate of z (complex, of course) x + y typeof(x + y) 0x1 + 2 [ typeof(0x1) typeof(0x1 + 2) ] rtmc = [ 1+1 -2 1/2 2/3 3\2 2^3 x%y y^4 ] [ 2/3 typeof(2/3) ] [ 4/2 typeof(4/2) ] a = ones(5) b = [ 5:9 ] # 5-9, inclusive c = eye(4) # returns the 4x4 identity matrix d = ones(4,4) # returns a 4x4 matrix of ones a[1] + b[1] a + b 5 * b b .^ 2 rtmc = [ 1 1+1 -2 1/2 2/3 3\2 2^3 x%y y^4 ] typeof('a') typeof("Quick brown fox") typeof(1) typeof(1.0) typeof([1.0 2.0 3.0]) typeof([ 1 => "2", 2 => "2"]) typeof(allmixedup) int_to_string = Dict{Int,String}() int_to_string = [ 1 => "1", 2 => "2"] typeof(int_to_string) int_to_string[3] = "foo" int_to_string["item3"] = "baz" type LP c # Types are optional A::Matrix{Float64} b::Vector{Float64} end randlp(n,m) = LP(rand(n),rand(n,m),rand(m)) mylp = randlp(10,5) println(mylp.c) type LP2{T} c::Vector{T} A::Matrix{T} b::Vector{T} end lp = LP2{Float64}(mylp.c,mylp.A,mylp.b) # dbl precision sayhello(friendnumber::Number) = println("Hello, numerical friend ", friendnumber) sayhello("Bob") sayhello(3) sayhello(4.0) sayhello(fpfriend::FloatingPoint) = println("Hello, floating point friend ", fpfriend) sayhello(4) sayhello(5.0) super(Float64) issubtype(Float64, Number) function hierarchy(t::Type) end hierarchy(Int64) function array_sum(x::Array{Float64, 1}) y = 0 for i in 1:length(x) y += x[i] + 2 end return y end function array_sum(x::Array{Int64, 1}) y = 0 for i in 1:length(x) y += x[i] + 1 end return y end array_sum([ 1, 2, 3, 4, 5 ]) # array of integers array_sum([ 1.0, 2, 3, 4, 5 ]) # array of floats function vec_norm(ar::Array{Float64,1}) end path = ccall( (:getenv, "libc"), Ptr{Uint8}, (Ptr{Uint8},), "PATH") bytestring(path) using PyPlot x = -3pi:.01:3pi plot(x, sin(x)) begin # and "hello" end expression = 1 ( this = 1; is = 2; println(a[1]); valid = expression ) function plus(x::Float64, y::Float64) return x + y end code_typed(plus, (Float64, Float64)) code_llvm(plus, (Float64, Float64)) code_native(plus, (Float64, Float64))