Open In App

Julia – REPL

Last Updated : 31 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

REPL stands for read-eval-print-loop. Julia has an inbuilt full-featured command-line built into Julia executable know as REPL. The Julia program starts the repl by default or REPL can be started by simply calling/writing the Julia without any argument. It comes with a lot of features in it which we will discuss in this article. Also to exit the interactive session just type exit() followed by the enter key.

REPL Prompt Modes

Julia REPL provides different types of prompt modes lets discusses it one by one:

Julia mode

The first mode is known as Julian prompt and it is the default operation mode in Julia. In Julia each line starts with the

julia>

You can write down your expression and once you hit enter after writing the expression it will be evaluated and it will show the result/output of the expression. Example:

julia> string(1+3)

REPL-01 Julia prompt mode provides you with the various features which are unique for interactive work. It also binds the result to the variable i.e “ans”. A semicolon on the line can be used as a flag to suppress the showing result. Example:

julia> string(3*5)
julia> ans "15"

repl-02

Help mode

By typing “?” in the prompt you will change the mode to help mode and whatever typed inside the help mode Julia will display the help or documentation related to that expression. You can exit from the help mode by just pressing the backspace at the starting of the line. Example:

julia> ?    *it will enter in the help as soon as you press enter : Help*
help?> string
search: string String Cstring substring RevString bystring 
string(xs....)
create a string from any values using the print function

repl-03

Shell mode

Just like help mode, you can enter the shell mode just by typing “;” and the prompt will change into the shell mode and for exit just press backspace at the beginning of the line. Example:

julia?> ;  *it will enter the shell mode : Shell*
shell> x=4
hello

repl-04

Key Binding

Julia REPL provides you the best use of the key binding. Some of the controls-key bindings are already introduced above like ^D and ^R. There are also many meta-key bindings. These vary from platform to platform, but most of the terminal default using the alt or option-held down with key to send the meta-key or by pressing Esc and then the key. Some of the examples of the key-bindings are: Program Control

Keybinding            Description

^D                     Exit
^C                     Interrupt or cancel  
^L                     Clear console screen
? and ;                Enter help or shell mode
^R, ^S                  Incremental history search, described above

Cursor movement

Keybinding            Description

^F                      Move right one character
^B                      Move left one character
meta-F                  Move right one word
^A                      Move to the beginning of the line
^E                      Move to end of the line

Editing

Keybinding            Description

meta-d                  Forward delete the previous word
meta-backspace          Delete the previous word
Delete, ^D               Forward delete one character
^W                      Delete previous text up to the nearest whitespace
^K                      "Kill" to end of the line, placing the text in the kill ring

Tab completion

With Julia and the help mode of the REPL, you can enter the initial character of a function or type and then press the tab key to get the list all match : Example:

julia> stri[TAB]
stride strides string strip

It is very helpful while performing mathematics. The tab key can be used to substitute LaTex math symbols with their Unicode equivalent. Example:

julia> \pi[TAB]
julia> ?
?=3.1459

repl-05

TerminalMenus

Example:

import REPL
using REPL.TerminalMenus
options = ["apple", "orange", "grape", "strawberry", "blueberry", "peach", "lemon", "lime"]

RadioMenu: It allows user to select one option from the list. The request function displays the interactive menu and returns the index. If the user select/press ‘q’ or ^c, the request will return -1. Example:

menu = RadioMenu(options, pagesize=4)
choice = request("Choose your fav fruit:", menu)
if choice !=-1
    println("Your fav fruit:", options[choice], "!")
else
    println("Menu Cancelled.")
end

Output:

choose your fruit:
grape
strawberry
blueberry
peach
Your fav fruit is blueberry

repl-06

Julia and mathematics

Julia provides a powerful calculator using REPL. Example:

julia> 1000000/7
                 142857.148571

To type scientific notation, type “e” and don’t press any space: Example :

julia> palnck_length = 1.6161997e-34
To type imaginary no. use im :

julia> (1 + 0.5) * 2
2.0 + 1.0im

Operator as function:

julia> 4+4
julia> 3+4+1

Another way is :

julia> +(2, 2)

repl-07

Features:

So, at the last Julia REPL comes with lots of features that allow quick and easy evaluation of Julia’s statement. It also provides searchable history, tab-completion, and many key-binding. It also provides a dedicated shell mode.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads