📜  朱莉娅——REPL

📅  最后修改于: 2022-05-13 01:55:36.588000             🧑  作者: Mango

朱莉娅——REPL

REPL代表读取-评估-打印-循环。 Julia 在 Julia 可执行文件中内置了一个功能齐全的命令行,称为 REPL。 Julia 程序默认启动 repl,或者可以通过简单地调用/编写 Julia 来启动 REPL,无需任何参数。它具有许多功能,我们将在本文中讨论。同样要退出交互式会话,只需键入 exit() 后跟 enter 键。

REPL 提示模式

Julia REPL 提供了不同类型的提示模式,让我们一一讨论:

朱莉娅模式

第一种模式称为 Julian prompt,它是 Julia 的默认操作模式。在 Julia 中,每一行都以

julia>

你可以写下你的表达式,一旦你在写完表达式后按下回车键,它将被评估并显示表达式的结果/输出。

例子:

julia> string(1+3)

REPL-01

Julia 提示模式为您提供了交互式工作所独有的各种功能。它还将结果绑定到变量,即“ans”。行上的分号可以用作标志来抑制显示结果。

例子:

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

repl-02

帮助模式

通过输入“?”在提示符中,您将模式更改为帮助模式,并且在帮助模式中输入的任何内容 Julia 将显示与该表达式相关的帮助或文档。
您只需在行首按退格键即可退出帮助模式。

例子:

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模式提示符将变为 shell 模式,退出时只需在行首按退格键。

例子:

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

repl-04

键绑定

Juila REPL 为您提供键绑定的最佳使用。上面已经介绍了一些控制键绑定,例如 ^D 和 ^R。还有许多元键绑定。这些因平台而异,但大多数终端默认使用 alt 或选项按住键来发送元键或按 Esc 然后键。

键绑定的一些示例是:

程序控制

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

光标移动

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

编辑

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
制表符补全

使用 Julia 和 REPL 的帮助模式,您可以输入函数或类型的初始字符,然后按 tab 键以获取列表全部匹配:

例子:

julia> stri[TAB]
stride strides string strip

在进行数学运算时非常有帮助。 Tab 键可用于将 LaTex 数学符号替换为其 Unicode 等效符号。

例子:

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

repl-05

终端菜单

例子:

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

RadioMenu:它允许用户从列表中选择一个选项。请求函数显示交互式菜单并返回索引。如果用户选择/按下 'q' 或 ^c,请求将返回 -1。

例子:

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

输出:

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

repl-06

朱莉娅和数学

Julia 使用 REPL 提供了一个强大的计算器。

例子:

julia> 1000000/7
                 142857.148571

要输入科学计数法,请输入“e”并且不要按任何空格:

例子 :

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

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

运算符作为函数:

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

另一种方法是:

julia> +(2, 2)

repl-07

特征:

因此,最后 Julia REPL 提供了许多功能,可以快速轻松地评估 Julia 的语句。它还提供可搜索的历史记录、制表符补全和许多键绑定。它还提供了专用的 shell 模式。