We started with Julia's REPL in the previous section to verify the correctness of the installation by issuing the julia command in a Terminal session. The REPL is Julia's working environment, where you can interact with the just in time (JIT) compiler to test out pieces of code. When satisfied, you can copy and paste this code into a file with a .jl extension, such as program.jl. Alternatively, you can continue to work on this code from within a text editor or an IDE, such as the ones we will point out later in this chapter. After the banner with Julia's logo has appeared, you will get a julia> prompt for the input. To end this session and get to the OS Command Prompt, type Ctrl + D, and hit Enter. To evaluate an expression, type it and press Enter to show the result, as shown in the following screenshot:
If, for some reason, you don't need to see the result, end the expression with a ; (semicolon) such as 8 * 5; In both the cases, the resulting value is stored, for convenience, in a variable named ans that can be used in expressions, but only inside the REPL. You can bind a value to a variable by entering an assignment as a = 3. Julia is dynamic, and we don't need to enter a type for a, but we do need to enter a value for the variable so that Julia can infer its type. Using a variable b that is not bound to the a value results in the ERROR: UndefVarError: b not defined message. Strings are delineated by double quotes (" "), as in b = "Julia". The following screenshot illustrates this in the REPL:
Previous expressions can be retrieved in the same session by working with the up and down arrow keys. The following key bindings are also handy:
- To clear or interrupt a current command, press Ctrl + C
- To clear the screen, press Ctrl + L (variables are kept in memory)
Commands from the previous sessions can still be retrieved, because they are stored (with a timestamp) in a repl_history.jl file (in /home/$USER/.julia/logs on Ubuntu, C:\Users\username\.julia\logs on Windows, or ~/.julia/logs/repl_history on OS X). Ctrl + R (produces a reverse-i-search prompt) searches through these commands.
Typing ? starts up the help mode (help?>) to give you quick access to Julia's documentation. Information on function names, types, macros, and so on, is given when typing in their names. Alternatively, to get more information on a variable, for example, a, type ?a, and to get more information on a function such as sort, type ?sort. To find all the places where a function such as println is defined or used, type apropos("println"), which gives the following output:
Base.Pair
Base.any
Base.@isdefined
Base.eachindex
Base.all
Base.Generator
Base.Timer
…
Printf.@sprintf
REPL.TerminalMenus.request
Thus, we can see that it is defined in the Base module, and that it is used in several other functions.
Different complete expressions on the same line have to be separated by a ; (semicolon), and only the last result is shown. You can enter multiline expressions, as shown in the following screenshot. If the shell detects that the statement is syntactically incomplete, it will not attempt to evaluate it. Rather, it will wait for the user to enter additional lines until the multiline statement can be evaluated:
A handy autocomplete feature also exists. Type one or more letters, press the Tab key twice, and then a list of functions starting with these letters appears. For example, type so, press the Tab key twice, and then you will get the list as sort sort! sortcols sortperm sortperm! sortrows.
If you start a line with ;, the rest of the line is interpreted as a system shell command (try, for example, ls, cd, mkdir, whoami on Linux). The Backspace key returns to the Julia prompt.
A Julia script can be executed in the REPL by calling it with include. For example, for hello.jl, which contains the println("Hello, Julia World!") statement, the command is as follows:
julia> include("hello.jl")
The preceding command prints the output as follows:
Hello, Julia World!
Experiment a bit with different expressions to get a feeling for this environment.