<!DOCTYPE html>
<html lang="zh-Hans">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=5">
    <title>一起来学习 Lisp 编程语言吧！ | 云图网</title>
    <meta name="keywords" content="Fedora,GitHub,go,https,macOS,shell,spring,windows,开源,游戏,程序员">
<meta name="description" content="作者： Seth Kenlon 译者： LCTT&nbsp;六开箱 | 2022-06-24 12:40&nbsp;&nbsp; 评论: 1&nbsp;&nbsp;&nbsp;&nbsp; 许多大型代码库中都有 Lisp 代码的身影，因此，熟悉一下这门语言是一个明智之举。 早在 1958 年，Lisp 就被发明出来了，…">
<meta property="og:type" content="article">
<meta property="og:url" content="https://blog.ytso.com/tech/pnotes/270263.html">
<meta property="og:site_name" content="云图网">
<meta property="og:title" content="一起来学习 Lisp 编程语言吧！">
<meta property="og:image" content="http://ytso-blog-oss-img.oss-accelerate.aliyuncs.com/wp-content/uploads/2022/06/28/20220628132942772.svg">
<meta property="og:description" content="作者： Seth Kenlon 译者： LCTT&nbsp;六开箱 | 2022-06-24 12:40&nbsp;&nbsp; 评论: 1&nbsp;&nbsp;&nbsp;&nbsp; 许多大型代码库中都有 Lisp 代码的身影，因此，熟悉一下这门语言是一个明智之举。 早在 1958 年，Lisp 就被发明出来了，…">
<link rel="canonical" href="https://blog.ytso.com/tech/pnotes/270263.html">
<meta name="applicable-device" content="pc,mobile">
<meta http-equiv="Cache-Control" content="no-transform">
<link rel="shortcut icon" href="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg">
<link rel='dns-prefetch' href='//cdnjs.cloudflare.com' />
<style id='wp-img-auto-sizes-contain-inline-css' type='text/css'>
img:is([sizes=auto i],[sizes^="auto," i]){contain-intrinsic-size:3000px 1500px}
/*# sourceURL=wp-img-auto-sizes-contain-inline-css */
</style>
<link rel='stylesheet' id='stylesheet-css' href='https://blog.ytso.com/wp-content/themes/justnews/css/style.css?ver=6.21.5' type='text/css' media='all' />
<link rel='stylesheet' id='material-icons-css' href='https://blog.ytso.com/wp-content/themes/justnews/themer/assets/css/material-icons.css?ver=6.21.5' type='text/css' media='all' />
<link rel='stylesheet' id='remixicon-css' href='https://cdnjs.cloudflare.com/ajax/libs/remixicon/4.2.0/remixicon.min.css?ver=6.9.4' type='text/css' media='all' />
<link rel='stylesheet' id='font-awesome-css' href='https://blog.ytso.com/wp-content/themes/justnews/themer/assets/css/font-awesome.css?ver=6.21.5' type='text/css' media='all' />
<style id='wp-block-library-inline-css' type='text/css'>
:root{--wp-block-synced-color:#7a00df;--wp-block-synced-color--rgb:122,0,223;--wp-bound-block-color:var(--wp-block-synced-color);--wp-editor-canvas-background:#ddd;--wp-admin-theme-color:#007cba;--wp-admin-theme-color--rgb:0,124,186;--wp-admin-theme-color-darker-10:#006ba1;--wp-admin-theme-color-darker-10--rgb:0,107,160.5;--wp-admin-theme-color-darker-20:#005a87;--wp-admin-theme-color-darker-20--rgb:0,90,135;--wp-admin-border-width-focus:2px}@media (min-resolution:192dpi){:root{--wp-admin-border-width-focus:1.5px}}.wp-element-button{cursor:pointer}:root .has-very-light-gray-background-color{background-color:#eee}:root .has-very-dark-gray-background-color{background-color:#313131}:root .has-very-light-gray-color{color:#eee}:root .has-very-dark-gray-color{color:#313131}:root .has-vivid-green-cyan-to-vivid-cyan-blue-gradient-background{background:linear-gradient(135deg,#00d084,#0693e3)}:root .has-purple-crush-gradient-background{background:linear-gradient(135deg,#34e2e4,#4721fb 50%,#ab1dfe)}:root .has-hazy-dawn-gradient-background{background:linear-gradient(135deg,#faaca8,#dad0ec)}:root .has-subdued-olive-gradient-background{background:linear-gradient(135deg,#fafae1,#67a671)}:root .has-atomic-cream-gradient-background{background:linear-gradient(135deg,#fdd79a,#004a59)}:root .has-nightshade-gradient-background{background:linear-gradient(135deg,#330968,#31cdcf)}:root .has-midnight-gradient-background{background:linear-gradient(135deg,#020381,#2874fc)}:root{--wp--preset--font-size--normal:16px;--wp--preset--font-size--huge:42px}.has-regular-font-size{font-size:1em}.has-larger-font-size{font-size:2.625em}.has-normal-font-size{font-size:var(--wp--preset--font-size--normal)}.has-huge-font-size{font-size:var(--wp--preset--font-size--huge)}.has-text-align-center{text-align:center}.has-text-align-left{text-align:left}.has-text-align-right{text-align:right}.has-fit-text{white-space:nowrap!important}#end-resizable-editor-section{display:none}.aligncenter{clear:both}.items-justified-left{justify-content:flex-start}.items-justified-center{justify-content:center}.items-justified-right{justify-content:flex-end}.items-justified-space-between{justify-content:space-between}.screen-reader-text{border:0;clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal!important}.screen-reader-text:focus{background-color:#ddd;clip-path:none;color:#444;display:block;font-size:1em;height:auto;left:5px;line-height:normal;padding:15px 23px 14px;text-decoration:none;top:5px;width:auto;z-index:100000}html :where(.has-border-color){border-style:solid}html :where([style*=border-top-color]){border-top-style:solid}html :where([style*=border-right-color]){border-right-style:solid}html :where([style*=border-bottom-color]){border-bottom-style:solid}html :where([style*=border-left-color]){border-left-style:solid}html :where([style*=border-width]){border-style:solid}html :where([style*=border-top-width]){border-top-style:solid}html :where([style*=border-right-width]){border-right-style:solid}html :where([style*=border-bottom-width]){border-bottom-style:solid}html :where([style*=border-left-width]){border-left-style:solid}html :where(img[class*=wp-image-]){height:auto;max-width:100%}:where(figure){margin:0 0 1em}html :where(.is-position-sticky){--wp-admin--admin-bar--position-offset:var(--wp-admin--admin-bar--height,0px)}@media screen and (max-width:600px){html :where(.is-position-sticky){--wp-admin--admin-bar--position-offset:0px}}
/*wp_block_styles_on_demand_placeholder:69cacab2396d3*/
/*# sourceURL=wp-block-library-inline-css */
</style>
<style id='classic-theme-styles-inline-css' type='text/css'>
/*! This file is auto-generated */
.wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none}
/*# sourceURL=/wp-includes/css/classic-themes.min.css */
</style>
<link rel='stylesheet' id='wpcom-member-css' href='https://blog.ytso.com/wp-content/plugins/wpcom-member/css/style.css?ver=1.7.19' type='text/css' media='all' />
<script type="text/javascript" src="https://blog.ytso.com/wp-includes/js/jquery/jquery.min.js?ver=3.7.1" id="jquery-core-js"></script>
<script type="text/javascript" src="https://blog.ytso.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=3.4.1" id="jquery-migrate-js"></script>
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="https://blog.ytso.com/xmlrpc.php?rsd" />
<meta name='description' content='作者： Seth Kenlon 译者：LCTT&nbsp;六开箱 | 2022-06-24 12:40&nbsp;&nbsp;评论: 1&nbsp;&nbsp;&nbsp;&nbsp; 许多大型代码库中都有 Lisp 代码的身影，因此，熟悉一下这门语言是一个明智之举。 早在 1958 年，Lisp…' />
<meta name='keywords' content='Fedora,GitHub,go,https,macOS,shell,spring,windows,开源,游戏,程序员' />
<style>:root{--theme-color: #08c; --theme-hover: #07c; --logo-height: 32px; --logo-height-mobile: 26px; --menu-item-gap: 28px; --member-login-bg: url('https://blog.ytso.com/loginwall.jpg'); --header-bg-color: #fff; --header-bg-image: none; --theme-border-radius-s: 3px; --theme-border-radius-m: 5px; --theme-border-radius-l: 8px; --theme-border-radius-xl: 12px; --thumb-ratio-default: 480 / 300; --thumb-ratio-post: 480 / 300; --post-video-ratio: 860 / 482;}</style>
<link rel="icon" href="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg" sizes="32x32" />
<link rel="icon" href="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg" sizes="192x192" />
<link rel="apple-touch-icon" href="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg" />
<meta name="msapplication-TileImage" content="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg" />
    <!--[if lte IE 11]><script src="https://blog.ytso.com/wp-content/themes/justnews/js/update.js"></script><![endif]-->
</head>
<body class="wp-singular post-template-default single single-post postid-270263 single-format-standard wp-theme-justnews lang-cn el-boxed header-fixed">
<header class="header header-fluid">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse" aria-label="menu">
                <span class="icon-bar icon-bar-1"></span>
                <span class="icon-bar icon-bar-2"></span>
                <span class="icon-bar icon-bar-3"></span>
            </button>
                        <div class="logo">
                <a href="https://blog.ytso.com/" rel="home">
                    <img src="https://imgcdn.ytso.com/wp-content/uploads/2021/12/20211207181156143.png" alt="云图网">
                </a>
            </div>
        </div>
        <div class="collapse navbar-collapse mobile-style-0">
            <nav class="primary-menu"><ul id="menu-justnews-footer-menu" class="nav navbar-nav wpcom-adv-menu"><li class="menu-item"><a href="https://blog.ytso.com/category/industrynews">业界资讯</a></li>
<li class="menu-item current-post-ancestor active menu-item-style menu-item-style-1 dropdown"><a target="_blank" href="https://blog.ytso.com/category/tech" class="dropdown-toggle">技术专区</a>
<ul class="dropdown-menu menu-item-wrap menu-item-col-5">
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/cloud">云计算</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/ai">人工智能</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/safety">信息安全</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/bigdata">大数据</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/dev">研发管理</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/webdev">大前端</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/opensource">开源</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/aiops">智能运维</a></li>
	<li class="menu-item current-post-ancestor current-post-parent active"><a href="https://blog.ytso.com/category/tech/pnotes">编程笔记</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/wp">WordPress</a></li>
</ul>
</li>
<li class="menu-item"><a href="https://blog.ytso.com/category/enterprise-strategic-planning">企业战略规划</a></li>
<li class="menu-item"><a href="https://blog.ytso.com/category/download">下载专区</a></li>
<li class="menu-item"><a href="https://blog.ytso.com/category/it%e6%b1%9f%e6%b9%96%e5%8f%b2">江湖史</a></li>
<li class="menu-item"><a href="https://blog.ytso.com/category/notes">随笔记录</a></li>
</ul></nav>            <div class="navbar-action">
                <div class="navbar-search-icon j-navbar-search"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-search"></use></svg></i></div><form class="navbar-search" action="https://blog.ytso.com/" method="get" role="search"><div class="navbar-search-inner"><i class="wpcom-icon wi navbar-search-close"><svg aria-hidden="true"><use xlink:href="#wi-close"></use></svg></i><input type="text" name="s" class="navbar-search-input" autocomplete="off" maxlength="100" placeholder="输入关键词搜索..." value=""><button class="navbar-search-btn" type="submit" aria-label="搜索"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-search"></use></svg></i></button></div></form>                    <div id="j-user-wrap">
                        <a class="login" href="https://blog.ytso.com/login">登录</a>
                        <a class="login register" href="https://blog.ytso.com/register">注册</a>
                    </div>
                                            <a class="wpcom-btn btn-primary btn-xs publish" href="https://blog.ytso.com/contribute">
                            <i class="fa fa-edit"></i> 投稿                        </a>
                                </div>
        </div>
    </div><!-- /.container -->
</header>

<div id="wrap">    <div class="wrap container">
        <ol class="breadcrumb" vocab="https://schema.org/" typeof="BreadcrumbList"><li class="home" property="itemListElement" typeof="ListItem"><a href="https://blog.ytso.com" property="item" typeof="WebPage"><span property="name" class="hide">云图网</span>首页</a><meta property="position" content="1"></li><li property="itemListElement" typeof="ListItem"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-arrow-right-3"></use></svg></i><a href="https://blog.ytso.com/category/tech" property="item" typeof="WebPage"><span property="name">技术专区</span></a><meta property="position" content="2"></li><li property="itemListElement" typeof="ListItem"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-arrow-right-3"></use></svg></i><a href="https://blog.ytso.com/category/tech/pnotes" property="item" typeof="WebPage"><span property="name">编程笔记</span></a><meta property="position" content="3"></li></ol>        <main class="main">
                            <article id="post-270263" class="post-270263 post type-post status-publish format-standard hentry category-pnotes tag-fedora tag-github tag-go tag-https tag-macos tag-shell tag-spring tag-windows tag-199 tag-222 tag-76 entry">
                    <div class="entry-main">
                                                                        <div class="entry-head">
                            <h1 class="entry-title">一起来学习 Lisp 编程语言吧！</h1>
                            <div class="entry-info">
                                                                <time class="entry-date published" datetime="2022-06-29T00:10:47+08:00" pubdate>
                                    2022年6月29日 00:10                                </time>
                                <span class="dot">•</span>
                                <a href="https://blog.ytso.com/category/tech/pnotes" rel="category tag">编程笔记</a>                                                            </div>
                        </div>
                        
                                                <div class="entry-content text-indent text-justify">
                            <div id="toc">
<p class="toc-title"><strong>文章目录</strong><span class="toc-controller toc-controller-show">[隐藏]</span></p>
<ul>
<li class="toc-level3"><a href="#%E5%AE%89%E8%A3%85-lisp">安装 Lisp</a></li>
<li class="toc-level3"><a href="#%E5%88%97%E8%A1%A8%E5%A4%84%E7%90%86">列表处理</a></li>
<li class="toc-level3"><a href="#%E5%87%BD%E6%95%B0">函数</a></li>
<li class="toc-level3"><a href="#%E5%8F%98%E9%87%8F">变量</a></li>
<li class="toc-level3"><a href="#%E5%88%97%E8%A1%A8">列表</a></li>
<li class="toc-level3"><a href="#%E9%80%80%E5%87%BA-repl">退出 REPL</a></li>
<li class="toc-level3"><a href="#%E7%BC%96%E5%86%99%E8%84%9A%E6%9C%AC">编写脚本</a></li>
<li class="toc-level3"><a href="#%E5%AD%A6%E4%B9%A0-lisp">学习 Lisp</a></li>
</ul>
</div>
<div class="ph" id="article_copyright">
<span>作者： </p>
<p>    		    			<span class="textcut"><a href="https://opensource.com/article/21/5/learn-lisp" target="_blank" rel="noopener">Seth Kenlon</a></span><br />    		    	    	<span>译者：<br /><a href="https://linux.cn/lctt/" target="_blank" rel="noopener">LCTT</a>&nbsp;<a href="https://linux.cn/lctt/lkxed" target="_blank" rel="noopener">六开箱</a></span></p>
<p id="article_date">| 2022-06-24 12:40&nbsp;&nbsp;<br />评论: <a href="https://linux.cn/portal.php?mod=comment&id=14751&idtype=aid" title="查看全部评论"><em id="_commentnum">1</em></a>&nbsp;&nbsp;&nbsp;&nbsp;</p>
</div>
<blockquote>
<p>许多大型代码库中都有 Lisp 代码的身影，因此，熟悉一下这门语言是一个明智之举。</p>
</blockquote>
<p>早在 1958 年，Lisp 就被发明出来了，它是世界上第二古老的计算机编程语言（LCTT 译注：最古老的编程语言是 Fortran，诞生于 1957 年）。它有许多现代的衍生品，包括 Common Lisp、Emacs Lisp（Elisp）、Clojure、Racket、Scheme、Fennel 和 GNU Guile 等。</p>
<p>那些喜欢思考编程语言的设计的人，往往都喜欢 Lisp，因为它的语法和数据有着相同的结构：Lisp 代码实际上是<ruby>一个列表的列表<rt>a list of lists</rt></ruby>，它的名字其实是 &ldquo;<ruby>列表处理<rt>LISt Processing</rt></ruby>&rdquo; 的简写。而那些喜欢思考编程语言的美学的人，往往都讨厌 Lisp，因为它经常使用括号来定义范围；事实上，编程界也有一个广为流传的笑话：Lisp 代表的其实是&nbsp;<ruby>&ldquo;大量烦人的多余括号&rdquo;<rt>Lots of Irritating Superfluous Parentheses</rt></ruby>。</p>
<p>不管你是喜欢还是讨厌 Lisp 的设计哲学，你都不得不承认，它都是一门有趣的语言，过去如此，现在亦然（这得归功于现代方言 Clojure 和 Guile）。你可能会惊讶于在任何特定行业的大代码库中潜伏着多少 Lisp 代码，因此，现在开始学习 Lisp，至少熟悉一下它，不失为一个好主意。</p>
<h3 class="mume-header" id="%E5%AE%89%E8%A3%85-lisp">安装 Lisp</h3>
<p>Lisp 有很多不同的实现。比较流行的开源版本有&nbsp;<a href="http://sbcl.org">SBCL</a>、<a href="http://clisp.org">GNU Lisp</a>&nbsp;和&nbsp;<a href="https://www.gnu.org/software/gcl/">GNU Common Lisp</a>（GCL）。你可以使用发行版的包管理器安装它们中的任意一个，在本文中，我是用的是&nbsp;<code>clisp</code>（LCTT 译注：也就是 GNU Lisp，一种 ANSI Common Lisp 的实现）。</p>
<p>以下是在不同的 Linux 发行版中安装&nbsp;<code>clisp</code>&nbsp;的步骤。</p>
<p>在 Fedora Linux 上，使用&nbsp;<code>dnf</code>：</p>
<pre class="prettyprint linenums"><code>$ sudo dnf install clisp
</code></pre>
<p>在 Debian 上，使用&nbsp;<code>apt</code>：</p>
<pre class="prettyprint linenums"><code>$ sudo apt install clisp
</code></pre>
<p>在 macOS 上，使用&nbsp;<a href="https://opensource.com/article/20/11/macports">MacPorts</a>&nbsp;或者&nbsp;<a href="https://opensource.com/article/20/6/homebrew-linux">Homebrew</a>：</p>
<pre class="prettyprint linenums"><code># 使用 MacPorts
$ sudo port install clisp

# 使用 Homebrew
$ brew install clisp
</code></pre>
<p>在 Windows 上，你可以使用&nbsp;<a href="https://cygwin.fandom.com/wiki/Clisp">clisp on Cygwin</a>&nbsp;或者从&nbsp;<a href="http://mirror.lagoon.nc/gnu/gcl/binaries/stable">gnu.org/software/gcl</a>&nbsp;上下载 GCL 的二进制文件。</p>
<p>虽然我使用&nbsp;<code>clisp</code>&nbsp;命令来运行 Lisp 代码，但是本文中涉及到的大多数语法规则，对任何 Lisp 实现都是适用的。如果你选择使用一个不同的 Lisp 实现，除了用来运行 Lisp 代码的命令会和我不一样外（比如，你可能要用&nbsp;<code>gcl</code>&nbsp;或&nbsp;<code>sbcl</code>&nbsp;而不是&nbsp;<code>clisp</code>），其它的所有东西都是相同的。</p>
<h3 class="mume-header" id="%E5%88%97%E8%A1%A8%E5%A4%84%E7%90%86">列表处理</h3>
<p>Lisp 源代码的基本单元是 &ldquo;<ruby>表达式<rt>expression</rt></ruby>&rdquo;，它在形式上是一个列表。举个例子，下面就是一个列表，它由一个操作符（<code>+</code>）和两个整数（<code>1</code>&nbsp;和&nbsp;<code>2</code>）组成：</p>
<pre class="prettyprint linenums"><code>(+ 1 2)
</code></pre>
<p>同时，它也是一个 Lisp 表达式，内容是一个符号（<code>+</code>，会被解析成一个加法函数）和它的两个参数（<code>1</code>&nbsp;和&nbsp;<code>2</code>）。你可以在 Common Lisp 的交互式环境（即 REPL）中运行该表达式和其它表达式。如果你熟悉 Python 的 IDLE，那么你应该会对 Lisp 的 REPL 感到亲切。（LCTT 译注：REPL 的全称是 &ldquo;Read-Eval-Print Loop&rdquo;，意思是 &ldquo;&lsquo;读取-求值-输出&rsquo;循环&rdquo;，这个名字很好地描述了它的工作过程。）</p>
<p>要进入到 REPL 中，只需运行 Common Lisp 即可：</p>
<pre class="prettyprint linenums"><code>$ clisp
[1]&gt;
</code></pre>
<p>在 REPL 提示符中，尝试输入一些表达式：</p>
<pre class="prettyprint linenums"><code>[1]&gt; (+ 1 2)
3
[2]&gt; (- 1 2)
-1
[3]&gt; (- 2 1)
1
[4]&gt; (+ 2 3 4)
9
</code></pre>
<h3 class="mume-header" id="%E5%87%BD%E6%95%B0">函数</h3>
<p>在了解了 Lisp 表达式的基本结构后，你可以使用函数来做更多有用的事。譬如，<code>print</code>&nbsp;函数可以接受任意数量的参数，然后把它们都显示在你的终端上，<code>pprint</code>&nbsp;函数还可以实现格式化打印。还有更多不同的打印函数，不过，<code>pprint</code>&nbsp;在 REPL 中的效果就挺好的：</p>
<pre class="prettyprint linenums"><code>[1]&gt; (pprint "hello world")

"hello world"

[2]&gt;
</code></pre>
<p>你可以使用&nbsp;<code>defun</code>&nbsp;函数来创建一个自定义函数。<code>defun</code>&nbsp;函数需要你提供自定义函数的名称，以及它接受的参数列表：</p>
<pre class="prettyprint linenums"><code>[1]&gt; (defun myprinter (s) (pprint s))
MYPRINTER
[2]&gt; (myprinter "hello world")

"hello world"

[3]&gt;
</code></pre>
<h3 class="mume-header" id="%E5%8F%98%E9%87%8F">变量</h3>
<p>你可以使用&nbsp;<code>setf</code>&nbsp;函数来在 Lisp 中创建变量：</p>
<pre class="prettyprint linenums"><code>[1]&gt; (setf foo "hello world")
"hello world"
[2]&gt; (pprint foo)

"hello world"

[3]&gt;
</code></pre>
<p>你可以在表达式里嵌套表达式（就像使用某种管道一样）。举个例子，你可以先使用&nbsp;<code>string-upcase</code>&nbsp;函数，把某个字符串的所有字符转换成大写，然后再使用&nbsp;<code>pprint</code>&nbsp;函数，将它的内容格式化打印到终端上：</p>
<pre class="prettyprint linenums"><code>[3]&gt; (pprint (string-upcase foo))

"HELLO WORLD"

[4]&gt;
</code></pre>
<p>Lisp 是动态类型语言，这意味着，你在给变量赋值时不需要声明它的类型。Lisp 默认会把整数当作整数来处理：</p>
<pre class="prettyprint linenums"><code>[1]&gt; (setf foo 2)
[2]&gt; (setf bar 3)
[3]&gt; (+ foo bar)
5
</code></pre>
<p>如果你想让整数被当作字符串来处理，你可以给它加上引号：</p>
<pre class="prettyprint linenums"><code>[4]&gt; (setf foo "2")
"2"
[5]&gt; (setf bar "3")
"3"
[6]&gt; (+ foo bar)

*** - +: "2" is not a number
The following restarts are available:
USE-VALUE &nbsp; &nbsp; &nbsp;:R1 &nbsp; &nbsp; &nbsp;Input a value to be used instead.
ABORT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:R2 &nbsp; &nbsp; &nbsp;Abort main loop
Break 1 [7]&gt;
</code></pre>
<p>在这个示例 REPL 会话中，变量&nbsp;<code>foo</code>&nbsp;和&nbsp;<code>bar</code>&nbsp;都被赋值为加了引号的数字，因此，Lisp 会把它们当作字符串来处理。数学运算符不能够用在字符串上，因此 REPL 进入了调试器模式。想要跳出这个调试器，你需要按下&nbsp;<code>Ctrl+D</code>&nbsp;才行（LCTT 译注：就&nbsp;<code>clisp</code>&nbsp;而言，使用&nbsp;<code>quit</code>&nbsp;关键字也可以退出）。</p>
<p>你可以使用&nbsp;<code>typep</code>&nbsp;函数对一些对象进行类型检查，它可以测试对象是否为某个特定数据类型。返回值&nbsp;<code>T</code>&nbsp;和&nbsp;<code>NIL</code>&nbsp;分别代表&nbsp;<code>True</code>&nbsp;和&nbsp;<code>False</code>。</p>
<pre class="prettyprint linenums"><code>[4]&gt; (typep foo 'string)
NIL
[5]&gt; (typep foo 'integer)
T
</code></pre>
<p><code>string</code>&nbsp;和&nbsp;<code>integer</code>&nbsp;前面加上了一个单引号（<code>'</code>），这是为了防止 Lisp（错误地）把这两个单词当作是变量来求值：</p>
<pre class="prettyprint linenums"><code>[6]&gt; (typep foo string)
*** - SYSTEM::READ-EVAL-PRINT: variable STRING has no value
[...]
</code></pre>
<p>这是一种保护某些术语（LCTT 译注：类似于字符串转义）的简便方法，正常情况下它是用&nbsp;<code>quote</code>&nbsp;函数来实现的：</p>
<pre class="prettyprint linenums"><code>[7]&gt; (typep foo (quote string))
NIL
[5]&gt; (typep foo (quote integer))
T
</code></pre>
<h3 class="mume-header" id="%E5%88%97%E8%A1%A8">列表</h3>
<p>不出人意料，你当然也可以在 Lisp 中创建列表：</p>
<pre class="prettyprint linenums"><code>[1]&gt; (setf foo (list "hello" "world"))
("hello" "world")
</code></pre>
<p>你可以使用&nbsp;<code>nth</code>&nbsp;函数来索引列表：</p>
<pre class="prettyprint linenums"><code>[2]&gt; (nth 0 foo)
"hello"
[3]&gt; (pprint (string-capitalize (nth 1 foo)))

"World"
</code></pre>
<h3 class="mume-header" id="%E9%80%80%E5%87%BA-repl">退出 REPL</h3>
<p>要结束一个 REPL 会话，你需要按下键盘上的&nbsp;<code>Ctrl+D</code>，或者是使用 Lisp 的&nbsp;<code>quit</code>&nbsp;关键字：</p>
<pre class="prettyprint linenums"><code>[99]&gt; (quit)
$
</code></pre>
<h3 class="mume-header" id="%E7%BC%96%E5%86%99%E8%84%9A%E6%9C%AC">编写脚本</h3>
<p>Lisp 可以被编译，也可以作为解释型的脚本语言来使用。在你刚开始学习的时候，后者很可能是最容易的方式，特别是当你已经熟悉 Python 或&nbsp;<a href="https://opensource.com/article/20/4/bash-programming-guide">Shell 脚本</a>&nbsp;时。</p>
<p>下面是一个用 Common Lisp 编写的简单的&ldquo;掷骰子&rdquo;脚本：</p>
<pre class="prettyprint linenums"><code>#!/usr/bin/clisp

(defun roller (num) &nbsp;
&nbsp; (pprint (random (parse-integer (nth 0 num))))
)

(setf userput *args*)
(setf *random-state* (make-random-state t))
(roller userput)
</code></pre>
<p>脚本的第一行注释（LCTT 译注：称之为&ldquo;<ruby>释伴<rt>shebang</rt></ruby>&rdquo;）告诉了你的 POSIX 终端，该使用什么可执行文件来运行这个脚本。</p>
<p><code>roller</code>&nbsp;函数使用&nbsp;<code>defun</code>&nbsp;函数创建，它在内部使用&nbsp;<code>random</code>&nbsp;函数来打印一个伪随机数，这个伪随机数严格小于&nbsp;<code>num</code>&nbsp;列表中下标为 0 的元素。在脚本中，这个&nbsp;<code>num</code>&nbsp;列表还没有被创建，不过没关系，因为只有当脚本被调用时，函数才会执行。</p>
<p>接下来的那一行，我们把运行脚本时提供的任意参数，都赋值给一个叫做&nbsp;<code>userput</code>&nbsp;的变量。这个&nbsp;<code>userput</code>&nbsp;变量是一个列表，当它被传递给&nbsp;<code>roller</code>&nbsp;函数后，它就会变成参数&nbsp;<code>num</code>。</p>
<p>脚本的倒数第二行产生了一个&ldquo;随机种子&rdquo;。这为 Lisp 提供了足够的随机性来生成一个几乎随机的数字。</p>
<p>最后一行调用了自定义的&nbsp;<code>roller</code>&nbsp;函数，并将&nbsp;<code>userput</code>&nbsp;列表作为唯一的参数传递给它。</p>
<p>将这个文件保存为&nbsp;<code>dice.lisp</code>，并赋予它可执行权限：</p>
<pre class="prettyprint linenums"><code>$ chmod +x dice.lisp
</code></pre>
<p>最后，运行它，并给它提供一个数字，以作为它选择随机数的最大值：</p>
<pre class="prettyprint linenums"><code>$ ./dice.lisp 21

13
$ ./dice.lisp 21

7
$ ./dice.lisp 21

20
</code></pre>
<p>看起来还不错！</p>
<p>你或许注意到，你的模拟骰子有可能会是 0，并且永远达不到你提供给它的最大值参数。换句话说，对于一个 20 面的骰子，这个脚本永远投不出 20（除非你把 0 当作 20）。有一个简单的解决办法，它只需要用到在本文中介绍的知识，你能够想到吗？</p>
<h3 class="mume-header" id="%E5%AD%A6%E4%B9%A0-lisp">学习 Lisp</h3>
<p>无论你是想将 Lisp 作为个人脚本的实用语言，还是为了助力你的职业生涯，抑或是仅仅作为一个有趣的实验，你都可以去看看一年一度（LCTT 译注：应该是两年一度）的&nbsp;<a href="https://itch.io/jam/spring-lisp-game-jam-2021">Lisp&nbsp;<ruby>游戏果酱<rt>Game Jam</rt></ruby></a>，从而收获一些特别有创意的用途（其中的大多数提交都是开源的，因此你可以查看代码以从中学习）。</p>
<p>Lisp 是一门有趣而独特的语言，它有着不断增长的开发者用户群、足够悠久的历史和新兴的方言，因此，它有能力让从事各个行业的程序员都满意。</p>
<hr />
<p>via:&nbsp;<a href="https://opensource.com/article/21/5/learn-lisp">https://opensource.com/article/21/5/learn-lisp</a></p>
<p>作者：<a href="https://opensource.com/users/seth">Seth Kenlon</a>&nbsp;选题：<a href="https://github.com/lkxed">lkxed</a>&nbsp;译者：<a href="https://github.com/lkxed">lkxed</a>&nbsp;校对：<a href="https://github.com/wxy">wxy</a></p>
<p>本文由&nbsp;<a href="https://github.com/LCTT/TranslateProject">LCTT</a>&nbsp;原创编译，<a href="https://linux.cn/">Linux中国</a>&nbsp;荣誉推出</p>
<section><noscript><img decoding="async" style="width:24px;height: 24px;float: right;" alt="一起来学习 Lisp 编程语言吧！" src="http://ytso-blog-oss-img.oss-accelerate.aliyuncs.com/wp-content/uploads/2022/06/28/20220628132942772.svg"></noscript><img decoding="async" style="width:24px;height: 24px;float: right;" class="j-lazy" alt="一起来学习 Lisp 编程语言吧！" src="https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png" data-original="http://ytso-blog-oss-img.oss-accelerate.aliyuncs.com/wp-content/uploads/2022/06/28/20220628132942772.svg"><br class="clear"></section>
<div class="entry-readmore"><div class="entry-readmore-btn"></div></div>                                                        <div class="entry-copyright"><p>原创文章，作者：ItWorker，如若转载，请注明出处：https://blog.ytso.com/tech/pnotes/270263.html</p></div>                        </div>

                        <div class="entry-tag"><a href="https://blog.ytso.com/tag/fedora" rel="tag">Fedora</a><a href="https://blog.ytso.com/tag/github" rel="tag">GitHub</a><a href="https://blog.ytso.com/tag/go" rel="tag">go</a><a href="https://blog.ytso.com/tag/https" rel="tag">https</a><a href="https://blog.ytso.com/tag/macos" rel="tag">macOS</a><a href="https://blog.ytso.com/tag/shell" rel="tag">shell</a><a href="https://blog.ytso.com/tag/spring" rel="tag">spring</a><a href="https://blog.ytso.com/tag/windows" rel="tag">windows</a><a href="https://blog.ytso.com/tag/%e5%bc%80%e6%ba%90" rel="tag">开源</a><a href="https://blog.ytso.com/tag/%e6%b8%b8%e6%88%8f" rel="tag">游戏</a><a href="https://blog.ytso.com/tag/%e7%a8%8b%e5%ba%8f%e5%91%98" rel="tag">程序员</a></div>
                        <div class="entry-action">
                            <div class="btn-zan" data-id="270263"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-thumb-up-fill"></use></svg></i> 赞 <span class="entry-action-num">(0)</span></div>
                                                    </div>

                        <div class="entry-bar">
                            <div class="entry-bar-inner">
                                                                <div class="entry-bar-info entry-bar-info2">
                                    <div class="info-item meta">
                                                                                    <a class="meta-item j-heart" href="javascript:;" data-id="270263"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-star"></use></svg></i> <span class="data">0</span></a>                                        <a class="meta-item" href="#comments"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-comment"></use></svg></i> <span class="data">0</span></a>                                                                            </div>
                                    <div class="info-item share">
                                                                                    <a class="meta-item mobile j-mobile-share" href="javascript:;" data-id="270263" data-qrcode="https://blog.ytso.com/tech/pnotes/270263.html">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-share"></use></svg></i> 生成海报                                            </a>
                                                                                    <a class="meta-item wechat" data-share="wechat" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-wechat"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item weibo" data-share="weibo" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-weibo"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item qq" data-share="qq" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-qq"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item qzone" data-share="qzone" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-qzone"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item douban" data-share="douban" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-douban"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item linkedin" data-share="linkedin" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-linkedin"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item facebook" data-share="facebook" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-facebook"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item twitter" data-share="twitter" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-twitter"></use></svg></i>                                            </a>
                                                                            </div>
                                    <div class="info-item act">
                                        <a href="javascript:;" id="j-reading"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-article"></use></svg></i></a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                                            <div class="entry-page">
                    <div class="entry-page-prev j-lazy" style="background-image: url('https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png');" data-original="http://ytso-blog-oss-img.oss-accelerate.aliyuncs.com/wp-content/uploads/2022/06/28/20220628182306318.jpg">
                <a href="https://blog.ytso.com/tech/pnotes/270658.html" title="安全工具之Appscan" rel="prev">
                    <span>安全工具之Appscan</span>
                </a>
                <div class="entry-page-info">
                    <span class="pull-left"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-arrow-left-double"></use></svg></i> 上一篇</span>
                    <span class="pull-right">2022年6月28日 20:03</span>
                </div>
            </div>
                            <div class="entry-page-next j-lazy" style="background-image: url('https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png');" data-original="http://ytso-blog-oss-img.oss-accelerate.aliyuncs.com/wp-content/uploads/2022/06/28/20220628142939389.png">
                <a href="https://blog.ytso.com/tech/pnotes/266878.html" title="如何双启动 Ubuntu 22.04 LTS 和 Windows 11" rel="next">
                    <span>如何双启动 Ubuntu 22.04 LTS 和 Windows 11</span>
                </a>
                <div class="entry-page-info">
                    <span class="pull-right">下一篇 <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-arrow-right-double"></use></svg></i></span>
                    <span class="pull-left">2022年6月29日 00:16</span>
                </div>
            </div>
            </div>
                                        <error>
    <code>wp_die</code>
    <title><![CDATA[WordPress &amp;rsaquo; Error]]></title>
    <message><![CDATA[&lt;h1&gt;Error establishing a Redis connection&lt;/h1&gt;
&lt;p&gt;To disable Redis, delete the &lt;code&gt;object-cache.php&lt;/code&gt; file in the &lt;code&gt;/wp-content/&lt;/code&gt; directory.&lt;/p&gt;
]]></message>
    <data>
        <status>500</status>
    </data>
</error>
<!--
Performance optimized by Redis Object Cache. Learn more: https://wprediscache.com

Retrieved 1882 objects (406 KB) from Redis using Predis (v2.4.0).
-->
