Linux Mint に Julia をインストールする

julialang.org
Linux Mint 19.3 に Julia をインストールしてみます。

インストールはここを参考にしました。まず、Snap をインストールします。

$ sudo apt update
$ sudo apt install snapd

Julia をインストールします。

$ sudo snap install julia --classic
2020-03-05T12:14:16+09:00 INFO Waiting for restart...
julia 1.0.4 from The Julia Language (julialang✓) installed
$ /snap/bin/julia -version
julia version 1.0.4

これでインストールされましたが、/snap/bin にPATHが通っていないので、面倒です。PATHを通すか、自分は .bashrc に alias julia='/snap/bin/julia' としておきました。

これで、

$ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.0.4 (2019-05-16)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> 2020*3
6060

julia> exit()
$ 

こんな感じで REPL が立ち上がります。
 

ちょっとREPLで遊んでみた

julia> a = 10
10

julia> 2a
20

julia> 4.5a
45.0

julia> typeof(a)
Int32

julia> typeof(4.5a)
Float64

julia> cube(x) = x^3
cube (generic function with 1 method)

julia> cube(a)
1000

julia> f(x) = 3x - a
f (generic function with 1 method)

julia> f(20)
50

julia> sphere(x) = 4/3 * pi * cube(x)
sphere (generic function with 1 method)

julia> sphere(10)
4188.790204786391

julia> b = [x for x = 1:4]
4-element Array{Int32,1}:
 1
 2
 3
 4

julia> c = b * 3
4-element Array{Int32,1}:
  3
  6
  9
 12

julia> b + c
4-element Array{Int32,1}:
  4
  8
 12
 16

julia> d = vcat(b, c)
8-element Array{Int32,1}:
  1
  2
  3
  4
  3
  6
  9
 12

julia> filter(iseven, d)
4-element Array{Int32,1}:
  2
  4
  6
 12

julia> str = map(x -> repeat("*", x), b)
4-element Array{String,1}:
 "*"   
 "**"  
 "***" 
 "****"

julia> join(str, " ")
"* ** *** ****"

julia> a = 45
45

julia> f(20)
15

julia> g(x) = 2x - j
g (generic function with 1 method)

julia> g(3)
ERROR: UndefVarError: j not defined
Stacktrace:
 [1] g(::Int32) at ./REPL[22]:1
 [2] top-level scope at none:0

julia> j = 100
100

julia> g(3)
-94

julia> j = 10
10

julia> g(3)
-4

関数はクロージャなのだな。それから、関数内に未定義変数があっても、実行するまでは怒られないのか。