five

Learn how to set up a programming environment from scratch

收藏
Zenodo2026-02-23 更新2026-05-26 收录
下载链接:
https://zenodo.org/doi/10.5281/zenodo.18742767
下载链接
链接失效反馈
官方服务:
资源简介:
# Learn how to set up a programming environment from scratch ## Introduction I know, as someone just starting out in programming, the most important thing isn't actually learning how to use function interfaces. It's understanding the logic of the programming language itself. As for me, I won't bore you with all that basic stuff like how to use functions—that's all in the official documentation. And honestly, you'll never memorize every single function out there. You should just see functions as a means to an end. All you really need is to master the core, fundamental syntax. For example, if I were to say the phrase “hello world” in English, I'd probably have to learn things like the International Phonetic Alphabet, along with the pronunciation of letters—including vowels and consonants. Similarly, if you learn programming, you'll find that different programming languages have different syntaxes and different ways of configuring environments. ## Core Logic For beginners in programming, grasping the core logic of coding is essential. Let's revisit the classic example of writing “Hello World” in English. If you're just starting out, you've likely encountered code snippets like this in your textbooks. Take Lua as an example: ```luaprint("hello world")``` Simply writing constants directly inside functions is acceptable if you're writing a script for practice. However, I don't recommend this approach in actual development, especially for large-scale projects. Here's why:- Inconvenient for semantic understanding- Significantly reduced maintainability- Difficult to locate and reproduce issues Therefore, the most recommended approach in actual development is to first store the constant in a variable—essentially giving it a name—and then import it into the function via that variable. For example: ```lualocal print_hello_world = "hello world"print(print_hello_world)``` Great! If you've made it this far, congratulations—you've got the basics down! Next, you might start writing code that looks something like this: ```luaprint("hello")print("hi")print("nice")print("to")print("meet")print("you")``` In actual development, if you need to call an interface once per line of output, it's manageable when the code is brief. However, as the code grows, you'll find many features require repeatedly implementing the same method multiple times. Copying and pasting becomes cumbersome and difficult to debug. Therefore, there are two optimization approaches here. One involves using a for loop to iterate through the data, while the other is to directly encapsulate it into a function. First, let's discuss the first type: iteration. The keyword commonly used for this is `for`. Generally, when iterating over parameters, we need to establish a data type first. For example, a table. In Lua, the syntax for implementing a table is very simple. You just need to wrap it with `value = {}`. Of course, this is a global variable. In actual development, we often use something called a local variable, like this: `local value = {}`. Lua tables can store many data types, such as integers, floating-point numbers, strings, functions, and so on. So if you don't want to repeat certain operations, you can write it like this: ```lualocal print_string = {  "hello",  "hi",  "nice",  "to",  "meet",  "you"} for _, string in ipairs(print_string) do  print(string)end``` Of course, in actual engineering project development, the more commonly used approach is the second one—that is, directly encapsulating a function to achieve the implementation. ```lualocal function Batch_print_characters(print_strings)  for _, string in ipairs(print_strings) do    print(string)  endend Batch_print_characters({  "hello",  "hi",  "nice",  "to",  "meet",  "you"})``` The above are merely the basics. In actual development, to ensure the security of program execution, we perform data type checks on program elements. Take a password field, for example. If its type isn't locked to string before input, it could cause irreparable damage in a production environment. If you're using the Lua programming language, you can use `type(value) == "string"` to determine whether it's a string. For example, this is a program used to enter passwords during login (though the actual logic is far more complex in practice, involving things like string encryption, which we won't go into here). ```lualocal user_info = {  name = "none",  password = "ksjdinxw",} if type(user_info.password) == "string" then  returnend``` ## How to Use the Terminal In practical development, the terminal is often an unavoidable topic. Many operations are debugged within the terminal, and numerous software packages are compiled using it. Terminals come in two main types: Windows Terminal and Linux distributions, which are widely used by programmers. They are compact and take up minimal space. If you're engaged in long-term programming development, Linux distributions are the preferred choice because resolving dependency issues is more straightforward than on Windows. Speaking of dependencies, we must mention package managers. Think of them as the equivalent of the Windows 11 Store, except Linux package managers also handle system upgrades. The commands used depend on your distribution, as they vary across different versions. For example, Arch Linux uses `sudo pacman -S nodejs` for installing and upgrading software, while Ubuntu employs `sudo apt install nodejs`. We inevitably need to perform operations like creating files and folders in the terminal, including deleting files. Therefore, we still need to master some basic command operations. Take creating files and folders, for instance. In Windows, we right-click and select “New.” In Linux, we type `touch file.txt` to create a file and `mkdir directory` to create a folder. To delete files and folders, we use the `rm` command. However, deletion in Linux has two scenarios: single files and entire directories. For a single file, simply use `rm file.txt`. To delete an entire directory, enter `rm -r directory`. To view the contents of the current directory, use the `ls` command. To display the current working directory, use `pwd`. If you want detailed information about each file in the current directory, enter `ls -la` to view the full list. The above describes operations within the current directory. To change directories, you must use the `cd` command, which can navigate up or down one level. To **move up one directory** level, enter `cd ..`. To **move down** one level, combine it with `ls`. For example, to navigate to the “test” directory, enter `cd test`. When booting a Linux distribution, the **default directory** is the `$HOME` directory. To quickly switch to it, enter `cd ~`. You can also use `echo $HOME` to view the location of the `$HOME` directory in your operating system. As for the commands mentioned earlier, they actually all have specific meanings. Essentially, they're just abbreviations in English. - `cd` - change directory- `ls` - list files- `rm` - remove- `print` - print working directory ## Run the program With the theoretical knowledge covered above, let's now learn how to run programs. Since Lua is a **scripting language**, you'll need to create a `test.lua` file first. Write your program inside it, save the file, and that's the basic logic. However, in practice, you must first verify that Lua and Vim or Neovim are properly installed. Since you'll be editing text files, you absolutely cannot do without Vim or Neovim. As for how to use Vim, here's the process: 1. First, open the file (using neovim as an example here), which is `nvim test.lua`.2. Upon entering the interface, you are in normal mode. You can type `dd` to delete an entire line, or type `o` to insert a new line below the current cursor position. Similarly, pressing `<shift>o` inserts a new line above the current cursor position. Pressing `gg` quickly moves the cursor to the first line, while `<shift>g` moves it to the last line. Below are operations for quick cursor positioning in normal mode. 3. Here's how to quickly position the cursor in normal mode. To start editing text—that is, to begin writing code—press the `i` key on your keyboard to enter insert mode.4. After writing your code, to save it, first press the `<esc>` key on your keyboard to exit insert mode and return to normal mode. Then press the `:` key to enter command line mode and type `wq`, which means write the current content to the file and exit. If you want to run a Lua program in the terminal, ensure your Lua environment is installed, then enter `lua test.lua` to see the program's output.

# 从零搭建编程环境 ## 简介 作为编程新手,你最先需要掌握的绝非函数接口的使用方法,而是编程语言本身的核心逻辑。至于函数使用这类基础内容,我将不再赘述——官方文档中已有完整说明。况且,你也不可能记住所有函数,只需将函数视作达成目标的工具即可。真正需要掌握的,是编程的核心基础语法。 举例而言,若要学会用英语说出"hello world",你或许需要先学习国际音标,以及元音、辅音的发音规则。同理,学习编程时你会发现,不同编程语言拥有各异的语法规则与环境配置方式。 ## 核心逻辑 对于编程初学者而言,掌握编码的核心逻辑至关重要。让我们重温编写"Hello World"这一经典示例。作为新手,你大概率在教材中见过类似的代码片段,以Lua语言为例: lua print("hello world") 若仅为编写练习脚本,直接在函数中写入常量尚可接受,但在实际开发尤其是大型项目中,我并不推荐这种写法,原因如下: - 语义理解不便 - 可维护性大幅下降 - 难以定位与复现问题 因此,实际开发中最推荐的做法是:先将常量存入变量(本质上即为其命名),再通过该变量将其传入函数。示例如下: lua local print_hello_world = "hello world" print(print_hello_world) 很棒!若你顺利掌握至此,恭喜你已经掌握了基础内容!接下来,你可能会开始编写类似如下的代码: lua print("hello") print("hi") print("nice") print("to") print("meet") print("you") 在实际开发中,若仅需每行输出调用一次接口,代码简短时尚可应付。但随着代码体量增长,你会发现诸多功能需要反复实现同一逻辑,此时复制粘贴的方式不仅繁琐,还难以调试。对此,有两种优化方案:一是使用for循环遍历数据,二是将逻辑直接封装为函数。 首先来看第一种方案:遍历。实现遍历常用的关键字为`for`。通常在遍历参数前,我们需要先定义一种数据类型,例如表(table)。在Lua中,定义表的语法十分简单,只需通过`value = {}`即可完成。当然,这属于全局变量,而在实际开发中,我们更常使用局部变量,写法如下:`local value = {}`。 Lua表可以存储多种数据类型,包括整数、浮点数、字符串、函数等。 因此,若你希望避免重复执行某些操作,可以按照如下方式编写代码: lua local print_string = { "hello", "hi", "nice", "to", "meet", "you"} for _, string in ipairs(print_string) do print(string) end 当然,在实际工程项目开发中,更常用的是第二种方案:直接封装函数来实现对应逻辑。 lua local function Batch_print_characters(print_strings) for _, string in ipairs(print_strings) do print(string) end end Batch_print_characters({ "hello", "hi", "nice", "to", "meet", "you"}) 以上仅为基础内容。在实际开发中,为保障程序运行安全,我们需要对程序元素进行数据类型校验。以密码字段为例,若在输入前未将其类型限定为字符串,在生产环境中可能会造成无法挽回的损失。若使用Lua语言,可通过`type(value) == "string"`来判断其是否为字符串类型。 例如,如下是一段用于登录时输入密码的程序(实际逻辑要复杂得多,例如涉及字符串加密,此处不再展开): lua local user_info = { name = "none", password = "ksjdinxw",} if type(user_info.password) == "string" then return end ## 终端使用指南 在实际开发中,终端往往是无法绕开的工具:诸多操作需在终端中调试,大量软件包也需通过终端编译。程序员常用的终端主要分为两类:Windows Terminal与Linux发行版终端,二者均小巧轻便、占用资源少。若你从事长期编程开发,Linux发行版终端是更优选择,因为其依赖问题的解决流程比Windows更为顺畅。谈及依赖管理,就不得不提及包管理器:它相当于Windows 11应用商店,但Linux包管理器还可承担系统升级的功能。具体使用的命令取决于你的发行版,不同版本的命令存在差异。例如,Arch Linux使用`sudo pacman -S nodejs`来安装与升级软件,而Ubuntu则使用`sudo apt install nodejs`。 我们不可避免地需要在终端中执行创建文件、文件夹以及删除文件等操作,因此还需掌握一些基础命令。以创建文件与文件夹为例:在Windows系统中,可通过右键菜单选择「新建」;在Linux系统中,输入`touch file.txt`即可创建文件,输入`mkdir directory`则可创建文件夹。删除文件与文件夹需使用`rm`命令,但Linux下的删除操作分为两种场景:单个文件与整个目录。删除单个文件时,只需执行`rm file.txt`;若要删除整个目录,则需输入`rm -r directory`。查看当前目录内容可使用`ls`命令,查看当前工作目录则使用`pwd`命令。若需要获取当前目录下每个文件的详细信息,可输入`ls -la`查看完整列表。 以上均为当前目录下的操作。若要切换目录,需使用`cd`命令,该命令可实现向上或向下一级目录的跳转。**向上跳转一级目录**:输入`cd ..`;**向下跳转一级目录**:可结合`ls`命令使用。例如,要进入「test」目录,输入`cd test`即可。Linux发行版启动时,**默认目录**为`$HOME`目录,若要快速切换至该目录,输入`cd ~`。你也可以通过`echo $HOME`查看当前系统中`$HOME`目录的具体路径。 前文提及的这些命令,实际上都有明确的含义,本质上均为英文缩写: - `cd` — 变更目录(change directory) - `ls` — 列出文件(list files) - `rm` — 删除(remove) - `print` — 打印工作目录(print working directory) ## 程序运行 掌握上述理论知识后,接下来我们学习如何运行程序。由于Lua属于**脚本语言**,你需要先创建一个`test.lua`文件,将程序写入其中并保存,这便是基础运行逻辑。但在实际操作前,你需要先确认Lua与Vim或Neovim已正确安装——因为编辑文本文件离不开这两款工具。 至于Vim的使用流程如下(以Neovim为例): 1. 首先打开文件,命令为`nvim test.lua`。 2. 进入界面后,你将处于普通模式:输入`dd`可删除整行,输入`o`可在光标当前位置下方插入新行;按下`<shift>o`则可在光标上方插入新行。输入`gg`可将光标快速跳转至第一行,`<shift>g`可跳转至最后一行。以下为普通模式下的光标快速定位操作。 3. 若要开始编辑文本(即编写代码),按下键盘上的`i`键即可进入插入模式。 4. 编写完代码后,按下`<esc>`键退出插入模式并返回普通模式,随后输入`:`进入命令行模式,键入`wq`即可将当前内容写入文件并退出。 若要在终端中运行Lua程序,确认你的Lua环境已安装后,输入`lua test.lua`即可查看程序运行输出。
提供机构:
Zenodo
创建时间:
2026-02-23
二维码
社区交流群
二维码
科研交流群
商业服务