<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tony Bai &#187; Clisp</title>
	<atom:link href="http://tonybai.com/tag/clisp/feed/" rel="self" type="application/rss+xml" />
	<link>https://tonybai.com</link>
	<description>一个程序员的心路历程</description>
	<lastBuildDate>Wed, 15 Apr 2026 00:30:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>通过Go示例理解函数式编程思维</title>
		<link>https://tonybai.com/2024/08/11/understand-functional-programming-in-go/</link>
		<comments>https://tonybai.com/2024/08/11/understand-functional-programming-in-go/#comments</comments>
		<pubDate>Sun, 11 Aug 2024 13:46:57 +0000</pubDate>
		<dc:creator>bigwhite</dc:creator>
				<category><![CDATA[技术志]]></category>
		<category><![CDATA[Clisp]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[Either]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[first-class]]></category>
		<category><![CDATA[fold]]></category>
		<category><![CDATA[FP]]></category>
		<category><![CDATA[fp-go]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[Go]]></category>
		<category><![CDATA[Golang]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[high-order]]></category>
		<category><![CDATA[Immutability]]></category>
		<category><![CDATA[imperitive]]></category>
		<category><![CDATA[Lamport]]></category>
		<category><![CDATA[LazyEvaluation]]></category>
		<category><![CDATA[logic]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[Martin]]></category>
		<category><![CDATA[monad]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[option]]></category>
		<category><![CDATA[PureFunction]]></category>
		<category><![CDATA[reduce]]></category>
		<category><![CDATA[Result]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[side-effect]]></category>
		<category><![CDATA[TLA+]]></category>
		<category><![CDATA[一等公民]]></category>
		<category><![CDATA[不可变性]]></category>
		<category><![CDATA[元组]]></category>
		<category><![CDATA[函数]]></category>
		<category><![CDATA[函数式]]></category>
		<category><![CDATA[函数式编程思维]]></category>
		<category><![CDATA[函数式设计]]></category>
		<category><![CDATA[函数组合]]></category>
		<category><![CDATA[列表]]></category>
		<category><![CDATA[副作用]]></category>
		<category><![CDATA[命令式]]></category>
		<category><![CDATA[惰性求值]]></category>
		<category><![CDATA[抽象]]></category>
		<category><![CDATA[接口]]></category>
		<category><![CDATA[映射]]></category>
		<category><![CDATA[模式匹配]]></category>
		<category><![CDATA[泛型编程]]></category>
		<category><![CDATA[纯函数]]></category>
		<category><![CDATA[递归]]></category>
		<category><![CDATA[逻辑编程]]></category>
		<category><![CDATA[闭包]]></category>
		<category><![CDATA[面向对象编程]]></category>
		<category><![CDATA[高阶函数]]></category>

		<guid isPermaLink="false">https://tonybai.com/?p=4244</guid>
		<description><![CDATA[本文永久链接 &#8211; https://tonybai.com/2024/08/11/understand-functional-programming-in-go 一个孩子要尝试10次、20次才肯接受一种新的食物，我们接受一种新的范式，大概不会比这个简单。&#8211; 郭晓刚 《函数式编程思维》译者 函数式编程(Functional Programming, 简称fp)是一种编程范式，与命令式编程(Imperative Programming)、面向对象编程(OOP)、泛型编程(Generics Programming)、逻辑编程(logic Programming)等是一类的概念。 注：尽管面向对象范式引入了新的编程思想和技术，但它本质上与命令式编程一样，都关注程序的状态和如何通过改变状态来控制程序的执行流程，因此，OOP仍然属于命令式编程的一个分支。OOP可以看作是命令式编程的一种扩展和补充，它增强了代码的模块化、复用性和可维护性。在接下来，我会统一使用命令式编程范式来指代它们。 但几十年的编程语言的演进和实践已经证明：函数式编程并非银弹，它有优势，更有不足。从编程社区的实际反映来看，纯函数式编程语言(比如：CLisp、Haskell、Scala、Clojure、Erlang等)始终处于小众地位。此外，即便很多主流命令式编程语言在近些年融入了一些函数式编程的语法特性，采用函数式风格的代码依旧比例极低，且不易被广泛接受。许多程序员在面对复杂的状态管理和副作用时，依然倾向于使用传统的命令式编程风格（包括OOP)。。 注：Go就原生提供了一些支持函数式范式编程的语法特性，比如：函数是一等公民(first-class)、高阶函数、闭包、函数迭代器以及泛型等。 造成这种局面的原因众说纷纭，但我认为有如下几个： 首先从人类这个物种的大脑的认知和思维方式来看，命令式编程更接近于人类的自然思维方式，其逻辑与人类解决问题时的逻辑思维相似，即都是以步骤的形式理解问题，且有明确的控制流：命令式语言的控制结构（如条件语句、选择语句和循环）使得程序的执行路径清晰可见，符合人类的直觉理解，这也使得命令式语言更容易被人类大脑所掌握。 其次，命令式编程强调状态的变化，程序员可以直接看到和控制变量的变化，这与人类处理现实世界事物的方式相似。 在上面原因的驱使下，久而久之，程序员便形成习惯与传统，有了积淀，便可以促进命令式编程语言在教育和产业中的广泛应用，使得大多数程序员习惯于这种编程方式（间接挤压了函数式编程的使用空间）。进而使得命令式语言有更丰富的学习资源和社区支持，程序员也更容易找到帮助和示例。 也就是说，命令式编程范式占据主流的根本原因是人类的大脑本身就是命令式的，而不是函数式的。不过也有极少数大脑是函数式思维的，比如发明了TLA+这门形式化建模和验证语言的Leslie Lamport老先生。 那么问题来了！既然学习函数式编程思维是违反人类大脑直觉的，且较为困难，那为什么还是有很多人学习函数式编程思维，并在实际开发中应用函数式编程范式呢？关于这个问题，我们可以从两方面来看。 从主观上说，程序员经常有探索新技术和新范式的内在动力，这种好奇心驱使他们尝试函数式编程，也就是我们俗称的“玩腻了，尝尝鲜儿”。并且，许多程序员视学习函数式编程为一种智力挑战，一种来自舒适区之外的挑战，这种挑战能带来成就感和个人成长。此外，在竞争激烈的IT行业，掌握多种编程范式可以使得个人技能多样化，增加个人的职业竞争力。 从客观上看，函数式编程也确实能帮助程序员提高抽象思维和系统设计能力，这种能力的提升不仅限于函数式编程，还能应用到其他编程范式中。并且，函数式编程为程序员提供了一个新的解决问题的视角和方法，特别是在处理并发和并行计算、复杂数据转换和流处理方面。 学习函数式编程范式，并不是说抛弃命令式范式(或其他范式)，而是融合，从主流编程语言对函数式编程的语法特性的支持也可窥见这般。 那么，到底什么是函数式编程范式？它与命令式范式对比又有怎么样的差异与优劣呢？在这篇文章中，我就来说说我的体会，并辅以Go示例来帮助大家理解。 1. 思维差异：命令式编程 vs. 函数式编程 在看过很多函数式编程的资料后（见文后的参考资料一节），我问了自己一个问题：面对同一个实际的问题，用命令式编程范式和用函数式编程范式的核心思维差异在哪里？为此，我基于现实世界的一个典型问题模型(数据输入 -> 数据处理 -> 处理结果输出)，并根据自己的理解画了下面两幅图： 命令式编程范式的思维 函数式编程范式的思维 我们先来描述一下上面两幅图中的数据处理流程： 命令式编程：通过I/O操作获取数据，然后解码为自定义类型进行处理，再编码为自定义类型以便I/O操作输出。处理过程中使用函数、带方法的类型和控制流结构（如for、if、switch等）。 函数式编程：通过带有副作用的操作（如I/O操作）获取数据，然后解码数据放入通用数据结构（如列表、元组、映射）进行处理，再放入通用数据结构以便通过副作用操作输出。处理过程中会使用纯函数、高阶函数以及它们的函数组合。 基于上述流程的说明，我们可以看出两种范式核心关注点的差异： 命令式编程范式：更关注类型的封装、类型间的耦合关系、行为集合的抽象(接口)以及对数据在类型实例间的传递的显式控制(if/for/switch)。 函数式编程范式：弱化类型的概念，使用通用数据结构，专注于通过纯函数/高阶函数、不可变数据和函数组合来实现对数据的处理逻辑。“控制流”更加隐含，比如会通过递归、模式匹配和惰性求值等方式实现。建立专门的抽象来应对与真实世界交互时的带有副作用(side effect)的操作。 下面我们通过一个具体的问题来大致体会一下不同编程泛型在解决问题的实现上的思维差异。这个问题很简单：编写一个程序从input.txt文件中读取数字(每行一个数字)，将每个数字乘以2，然后将结果写入output.txt文件中。 我们先来用命令式编程范式实现： // fp-in-go/double/go/main.go // NumberData represents the input data type [...]]]></description>
			<content:encoded><![CDATA[<p><img src="https://tonybai.com/wp-content/uploads/understand-functional-programming-in-go-1.png" alt="" /></p>
<p><a href="https://tonybai.com/2024/08/11/understand-functional-programming-in-go">本文永久链接</a> &#8211; https://tonybai.com/2024/08/11/understand-functional-programming-in-go</p>
<blockquote>
<p>一个孩子要尝试10次、20次才肯接受一种新的食物，我们接受一种新的范式，大概不会比这个简单。&#8211; 郭晓刚 《函数式编程思维》译者</p>
</blockquote>
<p>函数式编程(Functional Programming, 简称fp)是一种编程范式，与命令式编程(Imperative Programming)、面向对象编程(OOP)、泛型编程(Generics Programming)、<a href="https://tonybai.com/2012/05/08/translate-seven-languages-in-seven-weeks/">逻辑编程(logic Programming)</a>等是一类的概念。</p>
<blockquote>
<p>注：尽管面向对象范式引入了新的编程思想和技术，但它本质上与命令式编程一样，都关注程序的状态和如何通过改变状态来控制程序的执行流程，因此，OOP仍然属于命令式编程的一个分支。OOP可以看作是命令式编程的一种扩展和补充，它增强了代码的模块化、复用性和可维护性。在接下来，我会统一使用命令式编程范式来指代它们。</p>
</blockquote>
<p>但几十年的编程语言的演进和实践已经证明：<strong>函数式编程并非银弹</strong>，它有优势，更有不足。从编程社区的实际反映来看，纯函数式编程语言(比如：<a href="https://tonybai.com/2011/08/30/c-programers-tame-common-lisp-series-introduction/">CLisp</a>、<a href="https://www.haskell.org">Haskell</a>、Scala、Clojure、Erlang等)始终处于小众地位。此外，即便很多主流命令式编程语言在近些年融入了一些函数式编程的语法特性，采用函数式风格的代码依旧比例极低，且不易被广泛接受。许多程序员在面对复杂的状态管理和副作用时，依然倾向于使用传统的命令式编程风格（包括OOP)。。</p>
<blockquote>
<p>注：Go就原生提供了一些支持函数式范式编程的语法特性，比如：函数是一等公民(first-class)、高阶函数、<a href="https://tonybai.com/2021/08/09/when-variables-captured-by-closures-are-recycled-in-go">闭包</a>、<a href="https://tonybai.com/2024/06/24/range-over-func-and-package-iter-in-go-1-23/">函数迭代器</a>以及<a href="https://tonybai.com/2022/05/20/solving-problems-in-generic-function-implementation-using-named-return-values">泛型</a>等。</p>
</blockquote>
<p>造成这种局面的原因众说纷纭，但我认为有如下几个：</p>
<p>首先从人类这个物种的大脑的认知和思维方式来看，命令式编程更接近于人类的自然思维方式，其逻辑与人类解决问题时的逻辑思维相似，即都是以步骤的形式理解问题，且有明确的控制流：命令式语言的控制结构（如条件语句、选择语句和循环）使得程序的执行路径清晰可见，符合人类的直觉理解，这也使得命令式语言更容易被人类大脑所掌握。</p>
<p>其次，命令式编程强调状态的变化，程序员可以直接看到和控制变量的变化，这与人类处理现实世界事物的方式相似。</p>
<p>在上面原因的驱使下，久而久之，程序员便形成习惯与传统，有了积淀，便可以促进命令式编程语言在教育和产业中的广泛应用，使得大多数程序员习惯于这种编程方式（间接挤压了函数式编程的使用空间）。进而使得命令式语言有更丰富的学习资源和社区支持，程序员也更容易找到帮助和示例。</p>
<p>也就是说，命令式编程范式占据主流的根本原因是<strong>人类的大脑本身就是命令式的</strong>，而不是函数式的。不过也有极少数大脑是函数式思维的，比如发明了<a href="https://tonybai.com/2024/08/05/formally-verify-concurrent-go-programs-using-tla-plus/">TLA+这门形式化建模和验证语言</a>的<a href="https://lamport.azurewebsites.net">Leslie Lamport老先生</a>。</p>
<p>那么问题来了！既然学习函数式编程思维是违反人类大脑直觉的，且较为困难，那为什么还是有很多人学习函数式编程思维，并在实际开发中应用函数式编程范式呢？关于这个问题，我们可以从两方面来看。</p>
<p>从主观上说，程序员经常有探索新技术和新范式的内在动力，这种好奇心驱使他们尝试函数式编程，也就是我们俗称的“玩腻了，尝尝鲜儿”。并且，许多程序员视学习函数式编程为一种智力挑战，一种来自舒适区之外的挑战，这种挑战能带来成就感和个人成长。此外，在竞争激烈的IT行业，掌握多种编程范式可以使得个人技能多样化，增加个人的职业竞争力。</p>
<p>从客观上看，函数式编程也确实能帮助程序员提高抽象思维和系统设计能力，这种能力的提升不仅限于函数式编程，还能应用到其他编程范式中。并且，函数式编程为程序员提供了一个新的解决问题的视角和方法，特别是在处理并发和并行计算、复杂数据转换和流处理方面。</p>
<p>学习函数式编程范式，并不是说抛弃命令式范式(或其他范式)，而是<strong>融合</strong>，从主流编程语言对函数式编程的语法特性的支持也可窥见这般。</p>
<p>那么，到底什么是函数式编程范式？它与命令式范式对比又有怎么样的差异与优劣呢？在这篇文章中，我就来说说我的体会，并辅以Go示例来帮助大家理解。</p>
<h2>1. 思维差异：命令式编程 vs. 函数式编程</h2>
<p>在看过很多函数式编程的资料后（见文后的参考资料一节），我问了自己一个问题：面对同一个实际的问题，用命令式编程范式和用函数式编程范式的核心思维差异在哪里？为此，我基于现实世界的一个典型问题模型(数据输入 -> 数据处理 -> 处理结果输出)，并根据自己的理解画了下面两幅图：</p>
<p><img src="https://tonybai.com/wp-content/uploads/understand-functional-programming-in-go-3.png" alt="" /><br />
<center>命令式编程范式的思维</center></p>
<p><img src="https://tonybai.com/wp-content/uploads/understand-functional-programming-in-go-2.png" alt="" /><br />
<center>函数式编程范式的思维</center></p>
<p>我们先来描述一下上面两幅图中的数据处理流程：</p>
<ul>
<li>
<p>命令式编程：通过I/O操作获取数据，然后解码为自定义类型进行处理，再编码为自定义类型以便I/O操作输出。处理过程中使用函数、带方法的类型和控制流结构（如for、if、switch等）。</p>
</li>
<li>
<p>函数式编程：通过带有副作用的操作（如I/O操作）获取数据，然后解码数据放入通用数据结构（如列表、元组、映射）进行处理，再放入通用数据结构以便通过副作用操作输出。处理过程中会使用纯函数、高阶函数以及它们的函数组合。</p>
</li>
</ul>
<p>基于上述流程的说明，我们可以看出两种范式核心关注点的差异：</p>
<ul>
<li>命令式编程范式：更关注类型的封装、类型间的耦合关系、行为集合的抽象(接口)以及对数据在类型实例间的传递的显式控制(if/for/switch)。</li>
<li>函数式编程范式：弱化类型的概念，使用通用数据结构，专注于通过纯函数/高阶函数、不可变数据和函数组合来实现对数据的处理逻辑。“控制流”更加隐含，比如会通过递归、模式匹配和惰性求值等方式实现。建立专门的抽象来应对与真实世界交互时的带有副作用(side effect)的操作。</li>
</ul>
<p>下面我们通过一个具体的问题来大致体会一下不同编程泛型在解决问题的实现上的思维差异。这个问题很简单：编写一个程序从input.txt文件中读取数字(每行一个数字)，将每个数字乘以2，然后将结果写入output.txt文件中。</p>
<p>我们先来用命令式编程范式实现：</p>
<pre><code>// fp-in-go/double/go/main.go

// NumberData represents the input data
type NumberData struct {
    numbers []int
}

// ProcessedData represents the processed output data
type ProcessedData struct {
    numbers []int
}

// NewNumberData creates and returns a new NumberData instance
func NewNumberData() *NumberData {
    return &amp;NumberData{numbers: []int{}}
}

// AddNumber adds a number to NumberData
func (nd *NumberData) AddNumber(num int) {
    nd.numbers = append(nd.numbers, num)
}

// Process doubles all numbers in NumberData and returns ProcessedData
func (nd *NumberData) Process() ProcessedData {
    processed := ProcessedData{numbers: make([]int, len(nd.numbers))}
    for i, num := range nd.numbers {
        processed.numbers[i] = num * 2
    }
    return processed
}

// FileProcessor handles file operations and data processing
type FileProcessor struct {
    inputFile  string
    outputFile string
}

// NewFileProcessor creates and returns a new FileProcessor instance
func NewFileProcessor(input, output string) *FileProcessor {
    return &amp;FileProcessor{
        inputFile:  input,
        outputFile: output,
    }
}

// ReadAndDeserialize reads data from input file and deserializes it into NumberData
func (fp *FileProcessor) ReadAndDeserialize() (*NumberData, error) {
    file, err := os.Open(fp.inputFile)
    if err != nil {
        return nil, fmt.Errorf("error opening input file: %w", err)
    }
    defer file.Close()

    data := NewNumberData()
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        num, err := strconv.Atoi(scanner.Text())
        if err != nil {
            return nil, fmt.Errorf("error converting to number: %w", err)
        }
        data.AddNumber(num)
    }

    if err := scanner.Err(); err != nil {
        return nil, fmt.Errorf("error reading input file: %w", err)
    }

    return data, nil
}

// SerializeAndWrite serializes ProcessedData and writes it to output file
func (fp *FileProcessor) SerializeAndWrite(data ProcessedData) error {
    file, err := os.Create(fp.outputFile)
    if err != nil {
        return fmt.Errorf("error creating output file: %w", err)
    }
    defer file.Close()

    writer := bufio.NewWriter(file)
    defer writer.Flush()

    for _, num := range data.numbers {
        _, err := writer.WriteString(fmt.Sprintf("%d\n", num))
        if err != nil {
            return fmt.Errorf("error writing to output file: %w", err)
        }
    }

    return nil
}

// Process orchestrates the entire data processing workflow
func (fp *FileProcessor) Process() error {
    // Read and deserialize input data
    inputData, err := fp.ReadAndDeserialize()
    if err != nil {
        return err
    }

    // Process data
    processedData := inputData.Process()

    // Serialize and write output data
    err = fp.SerializeAndWrite(processedData)
    if err != nil {
        return err
    }

    return nil
}

func main() {
    processor := NewFileProcessor("input.txt", "output.txt")
    if err := processor.Process(); err != nil {
        fmt.Fprintf(os.Stderr, "Error: %v\n", err)
        os.Exit(1)
    }
    fmt.Println("Processing completed successfully.")
}
</code></pre>
<p>这段代码十分容易理解，在这段代码中，我们建立了三个类型：NumberData、ProcessedData和FileProcessor。前两个分别代表解码后的输入数据和编码前的输出数据，FileProcessor则是封装了文件操作和数据处理的逻辑的自定义类型。这段代码将文件I/O、数据处理和主要流程控制分离到不同的方法中。在读取和写入过程中，数据经历了字符串 -> NumberData -> ProcessedData -> 字符串的转换过程，同时数据也是<strong>在不同类型的方法间传递和变换状态</strong>。</p>
<p>接下来我们再来看看函数式范式版本，Go虽然提供了一些函数式编程的基础支持，比如一等公民的函数、支持高阶函数、闭包等，但一些像monad、monoid等高级概念还需要手工实现。<a href="https://github.com/IBM/fp-go">IBM开源了一个Go的函数式编程基础库fp-go</a>，这里就借用fp-go的便利实现上面的同等功能，我们看看风格上有何不同：</p>
<pre><code>// fp-in-go/double/fp-go/main.go

package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"

    "github.com/IBM/fp-go/either"
    "github.com/IBM/fp-go/ioeither"
)

// 读取文件内容
func readFile(filename string) ioeither.IOEither[error, string] {
    return ioeither.TryCatchError(func() (string, error) {
        content, err := os.ReadFile(filename)
        return string(content), err
    })
}

// 将字符串转换为数字列表
func parseNumbers(content string) either.Either[error, []int] {
    numbers := []int{}
    scanner := bufio.NewScanner(strings.NewReader(content))
    for scanner.Scan() {
        num, err := strconv.Atoi(scanner.Text())
        if err != nil {
            return either.Left[[]int](err)
        }
        numbers = append(numbers, num)
    }
    return either.Right[error](numbers)
}

// 将数字乘以2
func multiplyBy2(numbers []int) []int {
    result := make([]int, len(numbers))
    for i, num := range numbers {
        result[i] = num * 2
    }
    return result
}

// 将结果写入文件
func writeFile(filename string, content string) ioeither.IOEither[error, string] {
    return ioeither.TryCatchError(func() (string, error) {
        return "", os.WriteFile(filename, []byte(content), 0644)
    })
}

func main() {
    program := ioeither.Chain(func(content string) ioeither.IOEither[error, string] {
        return ioeither.FromEither(
            either.Chain(func(numbers []int) either.Either[error, string] {
                multiplied := multiplyBy2(numbers)
                result := []string{}
                for _, num := range multiplied {
                    result = append(result, strconv.Itoa(num))
                }
                return either.Of[error](strings.Join(result, "\n"))
            })(parseNumbers(content)),
        )
    })(readFile("input.txt"))

    program = ioeither.Chain(func(content string) ioeither.IOEither[error, string] {
        return writeFile("output.txt", content)
    })(program)

    result := program()
    err := either.ToError(result)

    if err != nil {
        fmt.Println("Program failed:", err)
    } else {
        fmt.Println("Program completed successfully")
    }
}
</code></pre>
<p>相对于前面使用命令式范式风格的代码，这段函数式范式的代码理解起来就要难上不少。</p>
<p>不过，这段代码很好地诠释了函数式编程中的函数组合理念，我们看到函数被当作值来传递和使用。例如，在ioeither.Chain中，我们传递了匿名函数作为参数，这体现了函数式编程中函数作为一等公民的概念。multiplyBy2函数是一个纯函数的例子，它没有副作用，对于相同的输入总是产生相同的输出。这种纯函数更容易测试和推理。</p>
<p>代码中最明显的函数组合例子是在main函数中，我们使用ioeither.Chain来组合多个函数操作。并且在这里，我们将文件读取、内容处理和文件写入操作串联在一起，形成一个更大的操作。而ioeither.Chain和either.Chain又都是高阶函数的例子，它们接受其他函数作为参数并返回新的函数。Either和IOEither类型也是函数式编程中用于错误处理的主流方式，允许我们以更函数式的方式处理错误，将错误处理集成到函数组合中。</p>
<p>很多人好奇如果用纯函数式编程语言实现这个示例会是什么样子的，下面我就贴一段Haskell语言的代码，大家简单了解一下，这里就不对代码进行解释了：</p>
<pre><code>// fp-in-go/double/fp-haskell/Main.hs

import System.IO
import Control.Monad (when)
import Text.Read (readMaybe)
import Data.Maybe (catMaybes)

-- Define a custom type for the result
data DoubledNumbers = DoubledNumbers { doubledNumbers :: [Int] } deriving (Show)

-- Function to read numbers from a file
readNumbers :: FilePath -&gt; IO (Either String [Int])
readNumbers filePath = do
    content &lt;- readFile filePath
    let numbers = catMaybes (map readMaybe (lines content))
    return $ if null numbers
             then Left "No valid numbers found."
             else Right numbers

-- Function to write result to a file
writeResult :: FilePath -&gt; DoubledNumbers -&gt; IO (Either String ())
writeResult filePath result = do
    let resultString = unlines (map show (doubledNumbers result))
    writeFile filePath resultString
    return $ Right ()

-- Function to double the numbers
doubleNumbers :: [Int] -&gt; DoubledNumbers
doubleNumbers numbers = DoubledNumbers { doubledNumbers = map (* 2) numbers }

main :: IO ()
main = do
    -- Read numbers from input.txt
    readResult &lt;- readNumbers "input.txt"
    case readResult of
        Left err -&gt; putStrLn $ "Error: " ++ err
        Right numbers -&gt; do
            let result = doubleNumbers numbers
            -- Write result to output.txt
            writeResultResult &lt;- writeResult "output.txt" result
            case writeResultResult of
                Left err -&gt; putStrLn $ "Error: " ++ err
                Right () -&gt; putStrLn "Successfully written the result to output.txt."
</code></pre>
<blockquote>
<p>注：安装ghc后，执行ghc &#8211;make Main就可以将上面Main.hs编译为一个可执行程序。更多关于haskell编译器的信息可以到<a href="https://www.haskell.org">haskell官网</a>查看。</p>
</blockquote>
<p>从上面的示例我们大致也能感受到两种范式在思维层面的差异，正如Robert Martin在《函数式设计》一书中说道的那样：<strong>函数式程序更倾向于铺设调节数据流转换的管道结构，而可变的命令式程序更倾向于迭代地处理一个个类型对象</strong>。</p>
<p>我们很难在一个例子中体现出函数式编程的所有概念和思维特点，接下来，我们就来逐个说说函数式编程范式中的要素，你也可以对应前面的图中的内容，反复感受函数式编程的思维特点。</p>
<h2>2. 函数式编程的要素</h2>
<blockquote>
<p>面向对象的编程通过封装不确定因素来使代码能被人理解，而函数式编程通过尽量减少不确定因素来使代码能被人理解。—— Michael Feathers 《<a href="https://book.douban.com/subject/2248759/">修改代码的艺术</a>》一书作者</p>
</blockquote>
<p>函数式编程建立在几个核心要素之上，这些要素共同构成了函数式编程的基础。让我们逐一探讨这些要素。</p>
<h3>2.1 纯函数 (Pure Functions)</h3>
<p>纯函数是函数式编程的基石。一个纯函数具有以下特性:</p>
<ul>
<li>对于相同的输入，总是产生相同的输出；</li>
<li>不会产生副作用(不会修改外部状态)；</li>
<li>不依赖外部状态。</li>
</ul>
<p>例如，前面fp-go示例中的multiplyBy2就是一个纯函数:</p>
<pre><code>func multiplyBy2(numbers []int) []int {
    result := make([]int, len(numbers))
    for i, num := range numbers {
        result[i] = num * 2
    }
    return result
}
</code></pre>
<p>这个函数总是为相同的输入返回相同的结果，并且不会修改任何外部状态。</p>
<h3>2.2 不可变性 (Immutability)</h3>
<p>Robert Martin在《函数式设计》一书为函数式编程下一个理想的定义：没有赋值语句的编程。实质是其强调了不可变性在函数式编程范式中的重要意义。在没有赋值语句的情况下，代码通常基于对原状态的计算而得到新的状态，而对原状态没有任何修改。</p>
<p>在Go语言中，由于不支持不可变变量(很多语言用val关键字来声明不可变变量，但Go并不支持)，我们通常通过复制对象来实现不可变性，这可以帮助我们避免状态变化带来的复杂性，但也因为复制而增加了内存开销和性能成本。</p>
<pre><code>// 定义一个不可变的结构体
type Point struct {
    x, y int
}

// 创建一个新的 Point，模拟不可变性
func NewPoint(x, y int) Point {
    return Point{x, y}
}

// 移动Point的方法，返回一个新的Point
func (p Point) Move(dx, dy int) Point {
    return NewPoint(p.x+dx, p.y+dy)
}
</code></pre>
<h3>2.3 高阶函数 (Higher-Order Functions)与函数组合(Function Composition)</h3>
<p>Go语言的一个内置特性让它具备了使用函数式编程范式的前提，那就是<strong>在Go中，函数是一等公民</strong>。这意味着函数可以像其他类型变量一样，被赋值、传参和返回。</p>
<p>而接受其他函数作为参数或返回函数的函数，被称为<strong>高阶函数</strong>，这也是函数式编程的基石，如下面的applyOperation函数就是一个高阶函数：</p>
<pre><code>func applyOperation(x int, operation func(int) int) int {
    return operation(x)
}

func double(x int) int {
    return x * 2
}

result := applyOperation(5, double) // 结果为10
</code></pre>
<p>而有了对高阶函数的支持，我们才能运用函数式思维中的核心思维：函数组合，来铺设调节数据流转换的管道结构：</p>
<pre><code>// fp-in-go/high-order-func/main.go

package main

import (
    "fmt"
)

// 定义一个类型为函数的别名
type IntTransformer func(int) int

// 将多个转换函数组合成一个管道
func pipe(value int, transformers ...IntTransformer) int {
    for _, transformer := range transformers {
        value = transformer(value)
    }
    return value
}

// 定义一些转换函数
func addOne(x int) int {
    return x + 1
}

func square(x int) int {
    return x * x
}

func main() {
    // 使用管道处理数据
    result := pipe(3, addOne, square)
    fmt.Println("Result:", result) // 输出 Result: 16
}
</code></pre>
<p>这个示例中的pipe函数接受一个初始值和多个转换函数，并将其串联执行。main函数调用pipe函数，将addOne和square两个转换函数连接起来并执行输出结果。</p>
<p>前面那个使用fp-go编写的示例中，使用ioeither.Chain构建的program也是一个函数调用组合。</p>
<p>此外，链式调用也是一种在日常开发中常见的函数组合的使用形式，它<strong>融合了命令式的类型和函数式编程的函数组合</strong>，特别适用于集合类型数据的处理，通过链式调用，可以以更简洁和直观的方式进行数据转换和处理。下面是一个基于泛型实现的通用的链式调用(filter -> map -> reduce)的示例：</p>
<pre><code>// fp-in-go/func-composition/main.go

package main

import "fmt"

// Collection 接口定义了通用的集合操作
type Collection[T any] interface {
    Filter(predicate func(T) bool) Collection[T]
    Map(transform func(T) T) Collection[T]
    Reduce(initialValue T, reducer func(T, T) T) T
}

// SliceCollection 是基于切片的集合实现
type SliceCollection[T any] struct {
    data []T
}

// NewSliceCollection 创建一个新的 SliceCollection
func NewSliceCollection[T any](data []T) *SliceCollection[T] {
    return &amp;SliceCollection[T]{data: data}
}

// Filter 实现了 Collection 接口的 Filter 方法
func (sc *SliceCollection[T]) Filter(predicate func(T) bool) Collection[T] {
    result := make([]T, 0)
    for _, item := range sc.data {
        if predicate(item) {
            result = append(result, item)
        }
    }
    return &amp;SliceCollection[T]{data: result}
}

// Map 实现了 Collection 接口的 Map 方法
func (sc *SliceCollection[T]) Map(transform func(T) T) Collection[T] {
    result := make([]T, len(sc.data))
    for i, item := range sc.data {
        result[i] = transform(item)
    }
    return &amp;SliceCollection[T]{data: result}
}

// Reduce 实现了 Collection 接口的 Reduce 方法
func (sc *SliceCollection[T]) Reduce(initialValue T, reducer func(T, T) T) T {
    result := initialValue
    for _, item := range sc.data {
        result = reducer(result, item)
    }
    return result
}

// SetCollection 是基于 map 的集合实现
type SetCollection[T comparable] struct {
    data map[T]struct{}
}

// NewSetCollection 创建一个新的 SetCollection
func NewSetCollection[T comparable]() *SetCollection[T] {
    return &amp;SetCollection[T]{data: make(map[T]struct{})}
}

// Add 向 SetCollection 添加元素
func (sc *SetCollection[T]) Add(item T) {
    sc.data[item] = struct{}{}
}

// Filter 实现了 Collection 接口的 Filter 方法
func (sc *SetCollection[T]) Filter(predicate func(T) bool) Collection[T] {
    result := NewSetCollection[T]()
    for item := range sc.data {
        if predicate(item) {
            result.Add(item)
        }
    }
    return result
}

// Map 实现了 Collection 接口的 Map 方法
func (sc *SetCollection[T]) Map(transform func(T) T) Collection[T] {
    result := NewSetCollection[T]()
    for item := range sc.data {
        result.Add(transform(item))
    }
    return result
}

// Reduce 实现了 Collection 接口的 Reduce 方法
func (sc *SetCollection[T]) Reduce(initialValue T, reducer func(T, T) T) T {
    result := initialValue
    for item := range sc.data {
        result = reducer(result, item)
    }
    return result
}

// ToSlice 实现了 Collection 接口的 ToSlice 方法
func (sc *SetCollection[T]) ToSlice() []T {
    result := make([]T, 0, len(sc.data))
    for item := range sc.data {
        result = append(result, item)
    }
    return result
}

func main() {
    // 使用 SliceCollection
    numbers := NewSliceCollection([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
    result := numbers.
        Filter(func(n int) bool { return n%2 == 0 }).
        Map(func(n int) int { return n * 2 }).
        Reduce(0, func(acc, n int) int { return acc + n })
    fmt.Println(result) // 输出: 60

    // 使用 SetCollection
    set := NewSetCollection[int]()
    for _, n := range []int{1, 2, 2, 3, 3, 3, 4, 5} {
        set.Add(n)
    }
    uniqueSum := set.
        Filter(func(n int) bool { return n &gt; 2 }).
        Map(func(n int) int { return n * n }).
        Reduce(0, func(acc, n int) int { return acc + n })
    fmt.Println(uniqueSum) // 输出: 50 (3^2 + 4^2 + 5^2)
}
</code></pre>
<p>这段代码定义的泛型接口类型Collection包含三个方法：</p>
<ul>
<li>Filter：根据条件过滤集合中的元素。</li>
<li>Map：对集合中的每个元素应用转换函数。</li>
<li>Reduce：对集合中的元素进行归约操作，比如求和。</li>
</ul>
<p>其中Filtre、Map都是返回集合自身，这样便允许实现Collection接口的集合类型(如上面的SetCollection和SliceCollection)使用链式调用，代码看起来也十分易于理解。</p>
<h3>2.4 递归(Recursion)</h3>
<p>递归是函数式编程中常用的控制结构，常用来替代循环。例如下面是计算阶乘的函数实现：</p>
<pre><code>func factorial(n int) int {
    if n &lt;= 1 {
        return 1
    }
    return n * factorial(n-1)
}
</code></pre>
<p>递归的优点十分明显，代码简洁，易于理解(相对于循环)，特别适合处理分解问题（如树结构、图遍历等）。但不足也很突出，比如可能导致栈溢出(尤其是对那些不支持尾递归优化的语言，比如Go)，特别是对于较大的输入。此外，由于每次递归调用都需要创建新栈帧，维护栈状态，递归会有额外的性能开销。调试递归函数也可能比循环更复杂，因为需要跟踪多个函数调用。</p>
<h3>2.5 惰性求值 (Lazy Evaluation)</h3>
<p>惰性求值是指延迟计算表达式的值，直到真正需要它的时候。这样可以避免不必要的计算并有效管理内存，特别是在处理大集合或无限集合时。下面是用惰性求值实现迭代集合元素的示例：</p>
<blockquote>
<p>注：Go原生并不支持惰性求值的语法，但我们可以使用闭包来模拟。</p>
</blockquote>
<pre><code>// fp-in-go/lazy-evaluation/lazy-range/main.go

package main

import "fmt"

func lazyRange(start, end int) func() (int, bool) {
    current := start
    return func() (int, bool) {
        if current &gt;= end {
            return 0, false
        }
        result := current
        current++
        return result, true
    }
}
func main() {
    next := lazyRange(1, 5)
    for {
        value, hasNext := next()
        if !hasNext {
            break
        }
        fmt.Println(value)
    }
}
</code></pre>
<p>我们看到这段代码通过惰性求值方式生成从1到4的数字，避免了预先生成整个范围的集合元素，节省了内存，并避免了不必要的计算。</p>
<p>我们再来看一个用惰性求值生成前N个斐波那契数列的示例：</p>
<pre><code>// fp-in-go/lazy-evaluation/fibonacci/main.go

package main

import (
    "fmt"
)

// Fibonacci 返回一个生成无限斐波那契数列的函数
func Fibonacci() func() int {
    a, b := 0, 1
    return func() int {
        a, b = b, a+b
        return a
    }
}

func main() {
    fib := Fibonacci()
    for i := 0; i &lt; 10; i++ { // 打印前10个斐波那契数
        fmt.Println(fib())
    }
}
</code></pre>
<p>我们看到Fibonacci函数返回一个闭包，每次调用时生成下一个斐波那契数，这样我们在需要时生成下一个斐波那契数，而无需生成所有。</p>
<p>虽然函数式编程强调纯函数和不可变性，但在实际应用中，我们不可避免地需要处理副作用，如I/O操作、数据库交互等。接下来，我们就来看看在函数式编程范式中是如何处理带有副作用的操作的。</p>
<h2>3. 函数式编程对副作用操作的处理</h2>
<h3>3.1 理解副作用</h3>
<p>在函数式编程中，副作用是指函数或表达式在执行过程中对其周围环境产生的任何可观察到的变化。这些变化包括但不限于：</p>
<ul>
<li>修改全局变量或静态局部变量</li>
<li>修改函数参数</li>
<li>执行I/O操作（读写文件、网络通信等）</li>
<li>抛出异常或错误</li>
<li>调用其他具有副作用的函数</li>
</ul>
<p>副作用使得程序的行为变得难以预测和测试，因为函数的输出不仅依赖于其输入，还依赖于程序的状态和外部环境。函数式编程通过最小化副作用来提高程序的可预测性和可测试性。</p>
<h3>3.2 Monad: 函数式编程中处理副作用的核心抽象</h3>
<p>在函数式编程中，Monad是一种用于处理副作用的核心抽象。它提供了一种结构化的方式来处理计算中的状态、异常、输入输出等副作用，使得程序更加模块化和可组合。</p>
<p>在范畴论中，Monad被定义为一个自函子(endofunctor)加上两个自然变换(有点抽象了)：</p>
<ul>
<li>return (也称为unit)：将一个值封装到Monad中。</li>
<li>bind (也称为flatMap或>>=)：将一个Monad中的值应用到一个函数中，并返回一个新的Monad。</li>
</ul>
<blockquote>
<p>注：要入门范畴论，可以参考《<a href="https://book.douban.com/subject/30357114/">Category Theory for Programmers</a>》这本书。</p>
</blockquote>
<p>Monad可以通过以下策略来处理副作用：</p>
<ul>
<li>延迟执行：将副作用操作封装在Monad中，但不立即执行，这样可以将副作用推迟到程序的边缘。</li>
<li>显式表示：使副作用成为类型系统的一部分，迫使开发者显式地处理这些效果。</li>
<li>组合性：提供了一种方式来组合包含副作用的操作，而不破坏函数的纯粹性。</li>
<li>错误处理：提供了一种统一的方式来处理可能失败的操作。</li>
<li>状态管理：允许在一系列操作中传递和修改状态，而不需要使用可变变量。</li>
</ul>
<p>在实际应用中，我们可以根据具体需求选择使用不同的Monad实现。每种Monad都有其适用场景，比如：</p>
<ul>
<li>使用Option(Maybe) Monad处理可能缺失的值，避免空指针异常。</li>
<li>使用Result(Either) Monad 处理可能失败的操作，提供更丰富的错误信息。</li>
<li>使用IO Monad封装所有的I/O操作，将副作用推迟到程序的边缘。</li>
</ul>
<p>接下来，我们就结合Go示例来逐一探讨这三种Monad实现。</p>
<h3>3.3 Option (Maybe)</h3>
<p>Option 用于表示一个值可能存在或不存在，避免了使用null或undefined带来的问题。</p>
<pre><code>// fp-in-go/side-effect/option/main.go

package main

import "fmt"

type Option[T any] struct {
    value   T
    present bool
}

func Some[T any](x T) Option[T] {
    return Option[T]{value: x, present: true}
}

func None[T any]() Option[T] {
    return Option[T]{present: false}
}

func (o Option[T]) Bind(f func(T) Option[T]) Option[T] {
    if !o.present {
        return None[T]()
    }
    return f(o.value)
}

// 使用示例
func safeDivide(a, b int) Option[int] {
    if b == 0 {
        return None[int]()
    }
    return Some(a / b)
}

func main() {
    result := Some(10).Bind(func(x int) Option[int] {
        return safeDivide(x, 2)
    })
    fmt.Println(result) // {5 true}

    result = Some(10).Bind(func(x int) Option[int] {
        return safeDivide(x, 0)
    })
    fmt.Println(result) // {0 false}
}
</code></pre>
<p>这段示例程序定义了一个Option结构体：包含一个值和一个表示值是否存在的布尔变量。Some和None函数是Option的创建函数，Some函数：返回一个包含值的Option。None函数返回一个不包含值的Option。Bind方法对Option中的值应用一个函数，如果值不存在则返回None。</p>
<h3>3.4 Result (Either)</h3>
<p>Result可用于处理可能产生错误的操作，它比Option提供了更多的信息，它可以可以携带错误信息。</p>
<pre><code>// fp-in-go/side-effect/result/main.go

package main

import (
    "fmt"
    "os"
    "strings"
)

type Result[T any] struct {
    value T
    err   error
    isOk  bool
}

func Ok[T any](value T) Result[T] {
    return Result[T]{value: value, isOk: true}
}

func Err[T any](err error) Result[T] {
    return Result[T]{err: err, isOk: false}
}

func (r Result[T]) Bind(f func(T) Result[T]) Result[T] {
    if !r.isOk {
        return Err[T](r.err)
    }
    return f(r.value)
}

// 使用示例
func readFile(filename string) Result[string] {
    content, err := os.ReadFile(filename)
    if err != nil {
        return Err[string](err)
    }
    return Ok(string(content))
}

func processContent(content string) Result[string] {
    // 处理内容...
    return Ok(strings.ToUpper(content))
}

func main() {
    result := readFile("input.txt").Bind(processContent)
    fmt.Println(result) // {HELLO, GOLANG &lt;nil&gt; true}
    result = readFile("input1.txt").Bind(processContent)
    fmt.Println(result) // { 0xc0000a0420 false}
}
</code></pre>
<p>这段示例程序定义了一个Result结构体：包含一个值、一个错误信息和一个表示操作是否成功的布尔变量。Ok和Err函数是Result的创建函数，Ok函数返回一个成功的Result。Err函数返回一个失败的Result。Bind方法对成功的Result中的值应用一个函数，如果操作失败则返回错误。</p>
<p>在示例中，我们分别用读取input.txt和不存在的input1.txt来演示成功和错误的两个情况，具体输出结果见上面代码中的注释。</p>
<h3>3.5 IO Monad</h3>
<p>IO Monad用于封装所有的带有副作用的输入/输出操作，使得这些操作在类型系统中可见，并且可以被推迟执行。</p>
<pre><code>// fp-in-go/side-effect/io-monad/main.go

package main

import (
    "fmt"
    "os"
    "strings"
)

// IO represents an IO operation that, when run, produces a value of type any or an error
type IO struct {
    run func() (any, error)
}

// NewIO creates a new IO monad
func NewIO(f func() (any, error)) IO {
    return IO{run: f}
}

// Bind chains IO operations, allowing for type changes
func (io IO) Bind(f func(any) IO) IO {
    return NewIO(func() (any, error) {
        v, err := io.run()
        if err != nil {
            return nil, err
        }
        return f(v).run()
    })
}

// Map transforms the value inside IO
func (io IO) Map(f func(any) any) IO {
    return io.Bind(func(v any) IO {
        return NewIO(func() (any, error) {
            return f(v), nil
        })
    })
}

// Pure lifts a value into the IO context
func Pure(x any) IO {
    return NewIO(func() (any, error) { return x, nil })
}

// ReadFile is an IO operation that reads a file
func ReadFile(filename string) IO {
    return NewIO(func() (any, error) {
        content, err := os.ReadFile(filename)
        if err != nil {
            return nil, fmt.Errorf("failed to read file: %w", err)
        }
        return string(content), nil
    })
}

// WriteFile is an IO operation that writes to a file
func WriteFile(filename string, content string) IO {
    return NewIO(func() (any, error) {
        err := os.WriteFile(filename, []byte(content), 0644)
        if err != nil {
            return nil, fmt.Errorf("failed to write file: %w", err)
        }
        return true, nil
    })
}

// Print is an IO operation that prints to stdout
func Print(x any) IO {
    return NewIO(func() (any, error) {
        fmt.Println(x)
        return x, nil
    })
}

func main() {
    // Example: Read a file, transform its content, and write it back
    program := ReadFile("input.txt").
        Map(func(v any) any {
            return strings.ToUpper(v.(string))
        }).
        Bind(func(v any) IO {
            return WriteFile("output.txt", v.(string))
        }).
        Bind(func(v any) IO {
            success := v.(bool)
            if success {
                return Pure("File processed successfully")
            }
            return Pure("Failed to process file")
        }).
        Bind(func(v any) IO {
            return Print(v)
        })

    // Run the IO operation
    result, err := program.run()
    if err != nil {
        fmt.Printf("An error occurred: %v\n", err)
    } else {
        fmt.Printf("Program completed: %s\n", result)
    }
}
</code></pre>
<p>这个示例提供了一个非泛型版本的IO Monad的Go实现，它允许我们链式组合带有副作用的IO操作，同时保持了一定程度的类型安全（尽管需要类型断言）。在实际使用中，你完全不用自己实现IO Monad，可以直接使用IBM/fp-go中的ioeither，就像本文初那个示例那样。</p>
<h2>4. 小结</h2>
<p>到这里，关于函数式编程思维的入门介绍就告一段落了！</p>
<p>通过上面的介绍，我们看到函数式编程提供了一种不同于传统命令式编程的思维方式。它强调不可变性、纯函数和函数的组合，为数据流的处理搭建管道，这些特性使得代码更易于理解、测试和并行化。然而，函数式编程也带来了一些挑战，如处理副作用和状态管理的复杂性和难于理解。</p>
<p>学习函数式编程不仅可以扩展我们的编程技能，还能帮助我们以新的方式思考问题和设计解决方案。正如《函数式编程思维》一书中译者所说，接受一种新的编程范式可能需要时间和耐心，但最终会带来新的见解和能力。</p>
<p>在实际应用中，纯粹的函数式编程并不常见，更常见的是将函数式编程的概念和技术与其他编程范式(主要就是命令式范式)相结合。</p>
<p>Go语言虽然不是一个纯函数式语言，但它提供了足够的特性来支持函数式编程风格，如一等公民的函数、闭包和高阶函数等。</p>
<p>最后要记住，编程范式是工具，而不是教条。好的程序员应该能够根据具体问题和场景，灵活地选择和组合不同的编程范式，以创造出最优雅、高效的解决方案。</p>
<p>本文涉及的源码可以在<a href="https://github.com/bigwhite/experiments/blob/master/fp-in-go">这里</a>下载 &#8211; https://github.com/bigwhite/experiments/blob/master/fp-in-go</p>
<p>本文部分源代码由Claude 3.5 sonnet和GPT-4o生成。</p>
<h2>5. 参考资料</h2>
<ul>
<li>《<a href="https://book.douban.com/subject/36974785/">函数式设计：原则、模式与实践</a>》- https://book.douban.com/subject/36974785/</li>
<li>《<a href="https://book.douban.com/subject/26587213/">函数式编程思维</a>》- https://book.douban.com/subject/26587213/</li>
<li>《<a href="https://book.douban.com/subject/36787585/">计算机程序的构造和解释</a>》- https://book.douban.com/subject/36787585/</li>
<li>《<a href="https://book.douban.com/subject/30165168/">Learning Functional Programming in Go</a>》 &#8211; https://book.douban.com/subject/30165168/</li>
<li><a href="https://www.youtube.com/watch?v=Jif3jL6DRdw">Introduction to fp-go, functional programming for golang</a> &#8211; https://www.youtube.com/watch?v=Jif3jL6DRdw</li>
<li><a href="https://betterprogramming.pub/investigate-functional-programming-concepts-in-go-1dada09bc913">Investigate Functional Programming Concepts in Go</a> &#8211; https://betterprogramming.pub/investigate-functional-programming-concepts-in-go-1dada09bc913</li>
<li><a href="https://medium.com/better-programming/investigating-the-i-o-monad-in-go-3c0fabbb4b3d">Investigating the I/O Monad in Go</a> &#8211; https://medium.com/better-programming/investigating-the-i-o-monad-in-go-3c0fabbb4b3d</li>
</ul>
<hr />
<p><a href="https://public.zsxq.com/groups/51284458844544">Gopher部落知识星球</a>在2024年将继续致力于打造一个高品质的Go语言学习和交流平台。我们将继续提供优质的Go技术文章首发和阅读体验。同时，我们也会加强代码质量和最佳实践的分享，包括如何编写简洁、可读、可测试的Go代码。此外，我们还会加强星友之间的交流和互动。欢迎大家踊跃提问，分享心得，讨论技术。我会在第一时间进行解答和交流。我衷心希望Gopher部落可以成为大家学习、进步、交流的港湾。让我相聚在Gopher部落，享受coding的快乐! 欢迎大家踊跃加入！</p>
<p><img src="http://image.tonybai.com/img/tonybai/gopher-tribe-zsxq-small-card.png" alt="img{512x368}" /><br />
<img src="http://image.tonybai.com/img/tonybai/go-programming-from-beginner-to-master-qr.png" alt="img{512x368}" /></p>
<p><img src="http://image.tonybai.com/img/tonybai/go-first-course-banner.png" alt="img{512x368}" /><br />
<img src="http://image.tonybai.com/img/tonybai/imooc-go-column-pgo-with-qr.jpg" alt="img{512x368}" /></p>
<p>著名云主机服务厂商DigitalOcean发布最新的主机计划，入门级Droplet配置升级为：1 core CPU、1G内存、25G高速SSD，价格5$/月。有使用DigitalOcean需求的朋友，可以打开这个<a href="https://m.do.co/c/bff6eed92687">链接地址</a>：https://m.do.co/c/bff6eed92687 开启你的DO主机之路。</p>
<p>Gopher Daily(Gopher每日新闻) &#8211; https://gopherdaily.tonybai.com</p>
<p>我的联系方式：</p>
<ul>
<li>微博(暂不可用)：https://weibo.com/bigwhite20xx</li>
<li>微博2：https://weibo.com/u/6484441286</li>
<li>博客：tonybai.com</li>
<li>github: https://github.com/bigwhite</li>
<li>Gopher Daily归档 &#8211; https://github.com/bigwhite/gopherdaily</li>
</ul>
<p><img src="http://image.tonybai.com/img/tonybai/iamtonybai-wechat-qr.png" alt="" /></p>
<p>商务合作方式：撰稿、出书、培训、在线课程、合伙创业、咨询、广告合作。</p>
<p style='text-align:left'>&copy; 2024, <a href='https://tonybai.com'>bigwhite</a>. 版权所有. </p>
]]></content:encoded>
			<wfw:commentRss>https://tonybai.com/2024/08/11/understand-functional-programming-in-go/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C程序员驯服Common Lisp &#8211; 函数</title>
		<link>https://tonybai.com/2011/09/23/c-programers-tame-common-lisp-series-functions/</link>
		<comments>https://tonybai.com/2011/09/23/c-programers-tame-common-lisp-series-functions/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 13:55:00 +0000</pubDate>
		<dc:creator>bigwhite</dc:creator>
				<category><![CDATA[技术志]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Blogger]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Clisp]]></category>
		<category><![CDATA[Common-Lisp]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Opensource]]></category>
		<category><![CDATA[Programmer]]></category>
		<category><![CDATA[函数]]></category>
		<category><![CDATA[函数语言]]></category>
		<category><![CDATA[博客]]></category>
		<category><![CDATA[变量]]></category>
		<category><![CDATA[开源]]></category>
		<category><![CDATA[程序员]]></category>

		<guid isPermaLink="false">http://tonybai.com/2011/09/23/c%e7%a8%8b%e5%ba%8f%e5%91%98%e9%a9%af%e6%9c%8dcommon-lisp-%e5%87%bd%e6%95%b0/</guid>
		<description><![CDATA[<p>Common Lisp是函数式编程语言，其基本组成单元自然是函数。对Common Lisp函数的理解也是学习Common Lisp语言的关键。另外与C语言以内存单元修改为主要编程方法不同，Common Lisp的主要编程方法是将函数应用于参数。这里我们分别用两种范式风格实现同一个函数，该函数...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://tonybai.com/2011/06/21/hello-common-lisp/" target="_blank">Common Lisp</a>是<a href="http://en.wikipedia.org/wiki/Functional_programming" target="_blank">函数式编程</a>语言，其基本组成单元自然是函数。对Common Lisp函数的理解也是学习Common Lisp语言的关键。另外与C语言以内存单元修改为主要编程方法不同，Common Lisp的主要编程方法是将函数应用于参数。这里我们分别用两种范式风格实现同一个函数，该函数用于取得第n个fibonacci数（n从0开始）：</p>
<p>;; 命令式风格<br />
	(defun imperative-fibonacci (n)<br />
	&nbsp;&nbsp;&nbsp; (let ((first 0)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (second 1)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (sum 0))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (dotimes (i n)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (progn<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setf sum (+ first second))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setf first second)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setf second sum)))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; first))</p>
<p>;; 函数式风格<br />
	(defun functional-fibonacci (n)<br />
	&nbsp;&nbsp;&nbsp; (cond&nbsp;<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((= 0 n) 0)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((= 1 n) 1)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (t (+ (functional-fibonacci (- n 1)) (functional-fibonacci (- n 2))))))</p>
<p>对比一下我们可以看出函数式风格代码更加简洁，可读性更强，更易于理解。虽然使用Common Lisp也可以写出<a href="http://en.wikipedia.org/wiki/Imperative_programming" target="_blank">命令式风格</a>的代码，不过我们强烈建议你使用函数式风格，这才是Common Lisp的首选范式 &#8211; 即用自然而然的函数嵌套调用或递归调用，而不是堆砌一些修改变量值的语句。C语言中也有函数，但与Common Lisp语言中函数的区别就在于其内部实现均为对变量的修改操作，就像上面例子中imperative-fibonacci函数定义的那样。</p>
<p>一、定义新函数<br />
	Common Lisp使用defun宏来定义一个新函数，其语法形式如下：<br />
	(defun function-name (param*)<br />
	&nbsp;&nbsp;&nbsp; &quot;Optional documentation string.&quot;<br />
	&nbsp;&nbsp;&nbsp; expr1<br />
	&nbsp;&nbsp;&nbsp; expr2<br />
	&nbsp;&nbsp;&nbsp; expr3<br />
	&nbsp;&nbsp;&nbsp; &#8230; )</p>
<p>其实我在之前的几篇文章以及上面的例子中已经多次用到了defun宏，与C语言的函数原型相比，defun定义的函数缺少了一些类型信息，包括返回值类型信息和参数列表中参数的类型信息。</p>
<p>defun定义的函数在全局作用域可见，即使这个定义是嵌套在另外一个函数定义中的(标准C是不允许函数嵌套定义的)：<br />
	[1]&gt; (defun foo (x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (defun bar (y)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print (1+ y))))<br />
	FOO<br />
	[2]&gt; (foo 1)<br />
	1<br />
	BAR<br />
	[3]&gt; (bar 2)<br />
	3</p>
<p>注意：嵌套在其他函数定义中函数定义，如bar，其生命周期起始于外围函数执行后，例如例子中foo函数未执行前，bar是未定义的。</p>
<p>对于C程序员来说，也许Common Lisp函数定义与C函数定义最大的不同在函数参数列表上。C语言的函数只支持两种参数列表形式：定长参数列表和<a href="http://tonybai.com/2008/05/07/also-talk-about-c-variable-length-args/" target="_blank">变长参数</a>列表，比如：<br />
	int main(int argc, char* argv[]); /* 定长参数列表 */<br />
	int printf( const char* format, &#8230;);&nbsp; /* 变长参数列表 */</p>
<p>而Common Lisp函数对参数列表的支持更加灵活，参数的类型更加丰富，功能也更为强大。下面我们逐一来看。</p>
<p>Common Lisp函数参数列表默认都是定长的，参数也是必选的(required)，也就是说参数列表中有几个形式参数，你在调用该函数时就需要传入等量的实际参数，不能多，也不能少，例如：<br />
	[1]&gt; (defun foo (x y) (print (+ x y)))<br />
	FOO<br />
	[2]&gt; (foo 1)<br />
	*** &#8211; EVAL/APPLY: too few arguments given to FOO<br />
	[3]&gt; (foo 1 2 3)<br />
	*** &#8211; EVAL/APPLY: too many arguments given to FOO<br />
	[4]&gt; (foo 1 2)<br />
	3</p>
<p>除了参数列表的必选参数外，Common Lisp还支持可选参数(Optional Parameter)。参数列表中的可选参数由&#038;optional指定，例如：<br />
	(defun foo (a b &#038;optional c d)<br />
	&nbsp;&nbsp;&nbsp; (print a)<br />
	&nbsp;&nbsp;&nbsp; (print b)<br />
	&nbsp;&nbsp;&nbsp; (print c)<br />
	&nbsp;&nbsp;&nbsp; (print d))</p>
<p>其中位于&#038;optional后面的c，d为可选参数；如果未显式指定默认值，则其值为NIL。<br />
	[1]&gt; (foo 1 2)<br />
	1<br />
	2<br />
	NIL<br />
	NIL</p>
<p>我们可以为可选参数指定默认值，例如：<br />
	(defun foo (a b &#038;optional (c 10) (d 11))<br />
	&nbsp;&nbsp;&nbsp; &#8230; &#8230;)<br />
	[2]&gt; (foo 1 2)<br />
	1<br />
	2<br />
	10<br />
	11</p>
<p>可以看出对于指定了默认值的可选参数，如果调用时没有为可选参数传入实际参数，则可选参数将绑定默认值。可选参数的默认值不仅仅可以是常量值，还可以是全局变量或该可选参数左侧的某个必选参数，例如：<br />
	(defvar *x* 13)<br />
	(defun foo (&#038;optional a b c (d *x*))<br />
	&nbsp;&nbsp;&nbsp; &#8230; &#8230;)<br />
	和<br />
	(defun foo (&#038;optional a b c (d a))<br />
	&nbsp;&nbsp;&nbsp; &#8230; &#8230;)<br />
	&nbsp;<br />
	如果有必选参数，那可选参数必须放在必选参数的后面，但我们可以将一个函数的所有参数都定义为可选参数，如：<br />
	(defun foo (&#038;optional a b (c 10) (d 11))<br />
	&nbsp;&nbsp;&nbsp; &#8230; &#8230;)</p>
<p>在带有可选参数的函数体内我们如何知道该函数被调用时外面是否给可选参数传入实际参数了呢？Common Lisp提供了一个指示器，你可以将该指示器放在可选参数默认值的后面，就像这样：<br />
	(defun foo (&#038;optional a b (c 10) (d 11 d-supplied-p))<br />
	&nbsp;&nbsp;&nbsp; (print a)<br />
	&nbsp;&nbsp;&nbsp; (print b)<br />
	&nbsp;&nbsp;&nbsp; (print c)<br />
	&nbsp;&nbsp;&nbsp; (print d)<br />
	&nbsp;&nbsp;&nbsp; (print d-supplied-p))</p>
<p>如果函数在执行时可选参数绑定了实际参数而不是默认值，则该指示器的值将为T，否则为NIL。<br />
	[1]&gt; (foo 1 2 3 4)<br />
	1<br />
	2<br />
	3<br />
	4<br />
	T<br />
	[2]&gt; (foo 1 2)<br />
	1<br />
	2<br />
	10<br />
	11<br />
	NIL</p>
<p>Common Lisp语言引入可选参数至少有两个用途，一是为了适应某些场合的需求：一些场合的确不需要显式为所有参数传递实际参数；二是我们通过可选参数可以为某些参数显式地设置默认值。</p>
<p>在Common Lisp中，与<a href="http://tonybai.com/2008/05/07/also-talk-about-c-variable-length-args/" target="_blank">C语言变长参数</a>列表对应的是函数列表中的rest参数。rest参数通过在参数前面的&#038;rest关键字修饰。通过rest参数，我们可以定义出类似format这样接受不定个数参数的函数，例如：<br />
	[1]&gt; (defun foo (x y &#038;optional z &#038;rest others)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print y)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print z)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (mapcar #&#039;print others))<br />
	[2]&gt; (foo 1 2 3 4 &quot;hello lisp&quot;)<br />
	1<br />
	2<br />
	3<br />
	4<br />
	&quot;hello lisp&quot;<br />
	(4 &quot;hello lisp&quot;)</p>
<p>在函数定义内部rest参数是以一个list的形式存在的，例如上面例子中，传入函数体内的参数others的值实际上是(4 &quot;hello lisp&quot;)。</p>
<p>我们知道C语言虽然支持变长参数列表，但其参数列表中至少需要有一个必选参数，例如：int printf( const char* format, &#8230;)中的format参数是无法省略的；但是Common Lisp就不同，Common Lisp支持完全的变长参数列表，例如：(defun my-add (&#038;rest addends) &#8230;)</p>
<p>Common Lisp还提供一种C语言中没有的参数类型 &#8211; keyword参数。keyword参数允许你只为参数列表中的某个特定参数传入实际参数，这种能力是rest和optional参数所不具备的。我们可以通过&#038;key来指示keyword参数，如：<br />
	[1]&gt; (defun foo (&#038;key x y z)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print y)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print z))</p>
<p>作为keyword参数，如果在函数调用时没有为其显式赋值，那么该参数的值将为NIL。我们可以通过如下方式为特定的keyword参数赋值：<br />
	[2]&gt; (foo :y 2)<br />
	NIL<br />
	2<br />
	NIL</p>
<p>Keyword参数赋值是不用考虑赋值先后的顺序的，例如：<br />
	[3]&gt; (foo :z 11 <img src='https://tonybai.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' />  13)<br />
	13<br />
	NIL<br />
	11</p>
<p>对于带有keyword参数的函数，调用该函数时要么不为任何keyword参数传参，要么至少为其中某一个keyword参数传参，不能只为必选参数传参，如：<br />
	[1]&gt; (defun foo (&#038;key x y z)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print y)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print z))<br />
	[2]&gt; (foo 1)<br />
	*** &#8211; FOO: keyword arguments in (1) should occur pairwise</p>
<p>与Optional参数类似，keyword参数也可以指定默认值，也可以通过指示器来标定keyword参数到底是否绑定了外面传入的实际参数，其中默认值既可以是常量也可以是其左侧其他keyword参数组成的表达式，例如：<br />
	[1]&gt; (defun foo (&#038;key (x 17) (y 15 y-supplied-p) z)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print y)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print y-supplied-p)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print z))</p>
<p>[2]&gt; (foo :z 23)<br />
	17<br />
	15<br />
	NIL<br />
	23</p>
<p>在之前有关keyword参数的例子中我们使用的形式参数多以x，y这样的简单名字命名，这些名字虽便于函数定义内部使用，但是对于这个函数的调用者来说，这些名字却没有什么实际含义。keyword参数支持通过别名方式解决这个问题：<br />
	[1]&gt; (defun foo (&#038;key ((:name a)) ((:age b)) ((:gender c) &quot;Unknown&quot;))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print a)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print b)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print c))<br />
	[2]&gt; (foo :name &quot;tony&quot; :age 29)<br />
	&quot;tony&quot;<br />
	29<br />
	&quot;Unknown&quot;</p>
<p>Common Lisp中函数的返回值默认为函数体中最后执行的那个表达式的求值结果，上面举的例子也都是这种情况。在C语言中我们通过return语句可以从函数体内的任何位置主动退出该函数，在Common Lisp中我们同样可以用return-from达到这一目的，例如：<br />
	[1]&gt; (defun foo (x y)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (&lt; x 0)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (return-from foo &quot;IIlegal X Value&quot;))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (&lt; y 0)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (return-from foo))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (+ x y))<br />
	FOO<br />
	[2]&gt; (foo 1 2)<br />
	3<br />
	[3]&gt; (foo -1 2)<br />
	&quot;IIlegal X Value&quot;<br />
	[4]&gt; (foo 1 -2)<br />
	NIL</p>
<p>对于函数而言，return-from的语法形式为：(return-from func-name optional-value)，若不指定返回值，那么默认return-from的返回值为NIL。</p>
<p>二、匿名函数<br />
	与C语言不同，Common Lisp支持定义匿名函数，例如：<br />
	[1]&gt; (funcall #&#039;(lambda (x) (print (1+ x))) 2)<br />
	3</p>
<p>上面例子中这行语句既包含了函数定义，也包含了函数调用。与之前使用defun定义有名函数不同的是，这次我们定义出来的函数没有指定函数名，这种使用lambda关键字定义的函数被称作为匿名函数。</p>
<p>从例子中也可以看出，匿名函数的定义也很简单，其一般形式为：<br />
	(lambda (args*) body-form*)</p>
<p>我们也称这种表达式为lambda表达式。lambda表达式定义的匿名函数与有名函数一样，也支持使用optional，rest和keyword参数。</p>
<p>三、高阶函数<br />
	函数式编程语言与命令式语言除了在风格方面的不同之外，最大的不同点之一在于函数式语言中函数已经成为了<a href="http://en.wikipedia.org/wiki/First-class_citizen" target="_blank">一等公民</a>(first-class citizen)，与整型、字符串等原生类型具有同等的地位。更具体地说，函数成为一等公民意味着我们可以像对待整型数、字符串那样将函数当作数据对待：将函数赋值给变量、将函数作为参数传递给其他函数以及将函数作为返回值返回给函数调用者等等。作为C程序员你也许会说这似乎与C语言中的函数指针很类似啊，但别忘了C语言真正原生支持的是类型是指针，而不是函数。</p>
<p>有了一等公民地位的函数，我们就得到了<a href="http://en.wikipedia.org/wiki/Higher-order_function" target="_blank">高阶函数</a>。高阶函数就是那些接受其他函数为函数或将其他函数作为返回值的函数。例如Common Lisp提供的标准函数sort就接受一个比较函数作为参数：<br />
	[1]&gt; (defun integer-over-than (x y) (&gt; x y))<br />
	INTEGER-OVER-THAN<br />
	[2]&gt; (sort &#039;(5 2 98) (function integer-over-than))<br />
	(98 5 2)<br />
	[3]&gt; (sort &#039;(&quot;hello&quot; &quot;world&quot;) (function string&gt;=))<br />
	(&quot;world&quot; &quot;hello&quot;)</p>
<p>标准库中的sort函数接受一个自定义的比较函数作为参数，并在内部将传入的函数应用于参数list。例子中我们没有直接将integer-over-than传给sort，而是使用了(function integer-over-than)。function是Common Lisp提供的一个特殊操作符，将其应用于函数名可以得到该函数名对应的函数对象。比如通过(function foo)，我们可以得到名字为foo的内部函数对象。如果没有foo这个函数定义，解释器会提示&quot;undefined function FOO&quot;。可以看出真正被当作一等公民对待的不是foo这个符号，而是foo这个符号名字背后所对应的那个函数对象，也就是函数在Common Lisp中的内部表示形式。我们在将函数绑定到某个变量或将函数传递给某个函数作为实际参数时，我们都需要使用这个内部函数对象，而不是foo这个符号，例如：<br />
	[1]&gt; (sort &#039;(5 2 98) integer-over-than)<br />
	*** &#8211; EVAL: variable MY-OVER-THAN has no value<br />
	[2]&gt; (setf *sort-func* integer-over-than)<br />
	*** &#8211; EVAL: variable MY-OVER-THAN has no value<br />
	[3]&gt; (setf *func* (function my-over-than))<br />
	# FUNCTION MY-OVER-THAN (X Y) (DECLARE (SYSTEM::IN-DEFUN MY-OVER-THAN)) (BLOCK MY-OVER-THAN (&gt; X Y))&gt;</p>
<p>Common Lisp提供了一个语法糖用于简化function的使用，即我们可以用#&#039;代替function操作符，比如：#&#039;foo就等价于(function foo)。</p>
<p>那么在接受函数作为参数的函数定义内部我们如何使用函数对象呢？Common Lisp提供了两个函数funcall和apply用来执行函数对象对应的函数。我们先以funcall为例，funcall的语法形式如下：<br />
	(funcall function-obj args*)</p>
<p>例如：<br />
	[1]&gt; (defun foo (x y) (print (+ x y)))<br />
	FOO<br />
	[2]&gt; (defun my-add (x y f) (funcall f x y))<br />
	MY-ADD<br />
	[3]&gt; (my-add 1 2 #&#039;foo)<br />
	3</p>
<p>apply与funcall的不同之处在于其接受的参数格式有不同，apply的语法形式如下：<br />
	(apply function-obj args* other-args-list)</p>
<p>直观地比较：(apply #&#039;+ &#039;(1 2 3))就等价于(funcall #&#039;+ 1 2 3)，不同的是使用apply需要将各个参数打包到一个list中，或至少保证最后一个参数为list。下面几种调用方式与(apply #&#039;+ &#039;(1 2 3))都是等价的：<br />
	(apply #&#039;+ 1 &#039;(2 3))<br />
	(apply #&#039;+ 1 2 &#039;(3))</p>
<p>lambda表达式用于定义一个匿名函数，我们同样可以通过#&#039;来获得这个匿名函数对应的函数对象，例如：<br />
	#&#039;(lambda (x y) (print (+ x y)))</p>
<p>匿名函数对象可以直接作为实际参数传递给函数，我们也可以通过funcall来直接执行匿名函数，例如：<br />
	[1]&gt; (my-add 1 2 #&#039;(lambda (x y) (print (+ x y))))<br />
	3<br />
	[2]&gt; (funcall #&#039;(lambda (x y) (print (+ x y))) 1 2)<br />
	3</p>
<p>以上是推荐的标准用法，下面方法（去掉了lambda前面的#&#039;）虽然也可以达到相同效果，但不推荐使用：<br />
	[1]&gt; (my-add 1 2 (lambda (x y) (print (+ x y))))<br />
	3<br />
	[2]&gt; (funcall (lambda (x y) (print (+ x y))) 1 2)<br />
	3<br />
	[3]&gt; ((lambda (x y) (print (+ x y))) 1 2)<br />
	3</p>
<p>在C语言中我们通过函数指针和回调手法也可以模拟一些高阶函数的行为，这里就不赘述了。</p>
<p>四、闭包(Closure)<br />
	市面上有很多编程语言都支持<a href="http://en.wikipedia.org/wiki/Closure_(computer_science)" target="_blank">闭包</a>，比如JavaScript，Python，Perl，Ruby等。这里所说的闭包不是离散数学里的那个闭包，而是编程语言引入的一种机制，目前对于编程语言中的闭包尚未有一个精确的定义，但一般认为闭包是引用了外部作用域(但不是全局作用域)的变量的函数，这个被引用的变量与这个函数一同存在，即使是脱离了定义它们的上下文环境。</p>
<p>Common Lisp支持闭包，关于Common Lisp闭包的一个最典型例子是这样的：<br />
	[1]&gt; (setf *fn* (let ((i 0))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #&#039;(lambda () (setf i (+ i 1)))))<br />
	#FUNCTION :LAMBDA NIL (SETF I (+ I 1))&gt;<br />
	[2]&gt; (funcall *fn*)<br />
	1<br />
	[3]&gt; (funcall *fn*)<br />
	2<br />
	[4]&gt; (funcall *fn*)<br />
	3</p>
<p>按照之前我们对变量的理解，let引入的i只是一个局部变量，在离开定义的环境后，该变量生命周期将终结。理论上我们三次调用*fn*所对应的你们函数得到的结果应该是相同的才对。但就是由于在let构造的局部作用域内的那个匿名函数引用了外部的变量i，导致变量i可以脱离其原生作用域的束缚，让其生命周期等同于了其内部的那个匿名函数，这个内部的匿名函数就被称为闭包，而那个被引用的外部变量被成为<a href="http://en.wikipedia.org/wiki/Free_variables_and_bound_variables" target="_blank">自由变量</a>(free variable)。当我们连续调用函数*fn*时，i就像一个全局变量一样，每次值都加一。</p>
<p>引用了自由变量的闭包似乎是终结了自由变量的局部绑定关系，将自由变量从局部作用域环境中取出，并重新放入一个与闭包同生命周期的新作用域。自由变量会常驻内存中，这也是闭包的常用场景之一。闭包的另外一个用途可能就是出于保护自由变量的考虑，让自由变量只有通过闭包函数才能访问到。</p>
<p style='text-align:left'>&copy; 2011, <a href='https://tonybai.com'>bigwhite</a>. 版权所有. </p>
]]></content:encoded>
			<wfw:commentRss>https://tonybai.com/2011/09/23/c-programers-tame-common-lisp-series-functions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C程序员驯服Common Lisp &#8211; 变量</title>
		<link>https://tonybai.com/2011/09/20/c-programers-tame-common-lisp-series-variables/</link>
		<comments>https://tonybai.com/2011/09/20/c-programers-tame-common-lisp-series-variables/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 15:24:00 +0000</pubDate>
		<dc:creator>bigwhite</dc:creator>
				<category><![CDATA[技术志]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Blogger]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Clisp]]></category>
		<category><![CDATA[Common-Lisp]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Opensource]]></category>
		<category><![CDATA[Programmer]]></category>
		<category><![CDATA[函数]]></category>
		<category><![CDATA[博客]]></category>
		<category><![CDATA[变量]]></category>
		<category><![CDATA[开源]]></category>
		<category><![CDATA[程序员]]></category>

		<guid isPermaLink="false">http://tonybai.com/2011/09/20/c%e7%a8%8b%e5%ba%8f%e5%91%98%e9%a9%af%e6%9c%8dcommon-lisp-%e5%8f%98%e9%87%8f/</guid>
		<description><![CDATA[<p>变量是C语言中最常用的、不可或缺的语言元素。C语言是命令式编程语言（imperative programming language），其基本编程方法是基于对内存单元的修改，而变量又恰是对内存单元的抽象表示，比如：&#34;int a = 0xff&#34;这行语句告诉我们在内存中有一块大小为4个字节...</p>]]></description>
			<content:encoded><![CDATA[<p>变量是<a href="http://tonybai.com/tag/C" target="_blank">C语言</a>中最常用的、不可或缺的语言元素。C语言是命令式编程语言（<a href="http://en.wikipedia.org/wiki/Imperative_programming" target="_blank">imperative programming language</a>），其基本编程方法是基于对内存单元的修改，而变量又恰是对内存单元的抽象表示，比如：&quot;int a = 0xff&quot;这行语句告诉我们在内存中有一块大小为4个字节的区域，该区域可以通过a这个变量直接访问，该区域初始时存储的值为0xff。由此看来C语言的主要操作就是变量操作。</p>
<p>C语言中变量的使用有着严格要求：<br />
	第一，在使用一个变量之前必须先声明(或定义)这个变量；<br />
	第二，变量声明(或定义)时必须显式指出这个变量的数据类型；<br />
	最后，变量类型一旦确定，则在其生命周期内不能改变。<br />
	这些也是C语言作为静态编译型语言的特质所决定的。下面代码中关于变量使用的语句在C语言中都是不被允许的：</p>
<p>void foo(void) {<br />
	&nbsp;&nbsp;&nbsp; printf(&quot;%d\n&quot;, x); /* x变量未被声明或定义 */<br />
	}</p>
<p>void bar(void) {<br />
	&nbsp;&nbsp;&nbsp; x;<br />
	&nbsp;&nbsp;&nbsp; printf(&quot;%d\n&quot;, x); /* 未显式指定x的数据类型，变量声明语句不合法 */<br />
	}</p>
<p>与C语言不同，<a href="http://tonybai.com/2011/06/21/hello-common-lisp/" target="_blank">Common Lisp</a>是一门通用的<a href="http://en.wikipedia.org/wiki/Functional_programming" target="_blank">函数式编程语言</a>，其基本编程方法是基于对函数的求值，并要求尽量避免引入可改变的(mutable)变量和状态。另外再加上Common Lisp还是一门动态类型语言，这些都决定了其在变量的使用方面与C语言有着较大的差异。</p>
<p>一、赋值<br />
	在C语言中，我们用等号(=)来为变量进行赋值，如：<br />
	int a = 13;<br />
	char *str = &quot;hello c&quot;;<br />
	struct foo f = {1, 5, &quot;foo&quot;};</p>
<p>但Common Lisp程序是基于<a href="http://tonybai.com/2011/09/02/c-programers-tame-common-lisp-series-expressions/" target="_blank">S-expressions</a>的，等号(=)只是一个相等性比较的逻辑判断谓词(predicate)，无法为变量进行赋值。在Common Lisp中，宏setf才是最通用也是最常使用的赋值方法。setf的语法形式如下：<br />
	(setf var-expression value-expression)</p>
<p>例如：<br />
	[1]&gt; (setf x 5)<br />
	5<br />
	[2]&gt; (print x)<br />
	5<br />
	[3]&gt; (setf x &quot;hello lisp&quot;)<br />
	&quot;hello lisp&quot;<br />
	[4]&gt; (print x)<br />
	&quot;hello lisp&quot;</p>
<p>从这个例子中我们可以看出三点与C语言的不同之处：<br />
	1、Common Lisp变量在使用前是无需显式声明的(当然显式声明也是可以的哦，详见后面说明);<br />
	2、变量无类型信息；<br />
	3、变量在运行时可以被赋值为多种类型的值，比如此例中x先被赋值为一个整数，后又被赋值为一个字符串。</p>
<p>C语言的变量是内存单元的直接抽象，但Common Lisp中的变量想必不是这样的，它似乎更像是一个void*指针，被赋值为各种类型的对象地址。没错，其实Common Lisp变量的实质是一个引用(reference)，对这类变量赋值的实质就是将引用与存储了真实值的内存块地址绑定起来，setf这个宏达到的实际效果就是改变了引用与值的绑定关系而已。通过这里我们似乎还可以引伸出一点，那就是Common Lisp中的变量本身并不包含类型信息，相反，值才是类型信息的载体，变量的类型取决于变量所绑定的值的类型。</p>
<p>二、局部变量(Local Variable)<br />
	作用域是变量的最重要属性之一。根据变量的作用域不同，在C语言中我们可以将变量简单地划分为全局变量(Global Variable)和局部变量。顾名思义，全局变量就是在程序的所有作用域内均可以访问的变量；局部变量恰与之相反，只是在某一特定作用域才可以访问的变量，比如某一函数或代码块内部。Common Lisp也支持这两种类型的变量，我们先从局部变量说起。</p>
<p>与C类似，Common Lisp的局部变量也是在函数内部或代码块内部定义和使用的。Common Lisp通过let宏定义一个局部变量，let宏的语法形式如下：<br />
	(let (var*)<br />
	&nbsp;&nbsp;&nbsp;&nbsp; expr1<br />
	&nbsp;&nbsp;&nbsp;&nbsp; expr2<br />
	&nbsp;&nbsp;&nbsp;&nbsp; &#8230; )</p>
<p>其中每个var的语法形式为：(name initial-value)，如果未指定initial-value，则变量初值将被设置为nil。最后一个expr的求值结果将作为let的返回值。例如：</p>
<p>[1]&gt; (defun bar ()<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (let ((x 0)) (setf x 4)))<br />
	BAR<br />
	[2]&gt; (bar)<br />
	4<br />
	[3]&gt; (print x)<br />
	*** &#8211; EVAL: variable X has no value</p>
<p>let引入的局部变量x的作用域仅限于bar的内部，在bar外部无法访问到这些变量，变量x的生命周期也是从bar的执行开始，直到bar执行结束为止。</p>
<p>let一次可声明多个局部变量，并可嵌套使用：<br />
	[1]&gt; (defun foo ()<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (let ((x 1) (y 2))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print y)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setf x 3)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (let ((z 4))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print z))))<br />
	FOO<br />
	[2]&gt; (foo)<br />
	1<br />
	2<br />
	3<br />
	4</p>
<p>局部变量的默认作用域属于静态作用域(lexical scope或static scope)，静态作用域是指变量的作用域在执行前即可确定下来，每个函数或代码块中的局部变量均可以在当前函数(或代码块)中或其外层函数(或外层代码块)中找到对应的声明或定义。如上例中，函数foo内有两层let嵌套。外层let引入的变量x和y在当前函数内即可找到对应的定义；最里层的let代码块中使用的变量x在外层的foo函数定义中可以找到相应的定义。一旦找到绑定关系，变量的值就不会因执行环境不同而发生变化了。</p>
<p>和C语言类似，如果局部作用域中的不同层次定义了相同名字的局部变量，那么内层的局部变量将遮盖外层的变量，如：<br />
	[1]&gt; (defun bar ()<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (let ((x 1))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (let ((x 11))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setf x (1+ x))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x)))<br />
	BAR<br />
	[2]&gt;<br />
	1<br />
	11<br />
	12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
	1</p>
<p>求值内层x的值时，Common Lisp找到的是最内层的x定义，最内层的变量x当前绑定值为11，我们看到的输出结果也的确是11。在外层，变量x对应的定义为(x &lt;&#8211; 1)，这样外层输出的x的值就为1。</p>
<p>前面例子中定义的函数都是不带参数的，这是故意为之，因为下面我要说函数的形式参数。在C语言中，函数的形式参数与函数内的局部变量是等价的，其作用域也仅局限在函数内部。Common Lisp也是这样的，函数的形式参数本身就可以理解为函数内部的局部变量，在函数被求值时，Common Lisp在函数的形式参数与实际参数间建立绑定关系：<br />
	[1]&gt; (defun foo (x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setf x 13)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x))<br />
	FOO<br />
	[2]&gt; (foo 4)<br />
	4<br />
	13</p>
<p>从例子中我们可以看出foo函数在求值时，形式参数x与实际参数4建立绑定，第一个(print x)输出结果为4；在foo内部，我们利用setf改变了x的绑定关系后，x值变为13；函数foo内部使用的变量x对应的定义就是foo的形式参数。无论日后foo在什么上下文环境下执行，该x对应的定义都是固定的了，如：<br />
	[3]&gt; (let ((x 5)) (foo 4))<br />
	4<br />
	13</p>
<p>与C语言不同的是，Common Lisp还支持动态作用域(dynamic scope)，这种作用域在如今主流编程语言中已经不常见了。与静态作用域相反，具有动态作用域的变量在执行之前无法确定其定义，也就是说变量的定义不取决于其所在函数或代码块定义时的环境，而是取决于其所在函数或代码块执行时所处的上下文环境，因此其定义或者说绑定关系只能在运行时确定。</p>
<p>对于局部变量，Common Lisp通过(declare (special var-name))来显式声明var-name这个变量为动态作用域变量，又称动态变量(dynamic variable)。动态变量理解起来很不容易，但若明白其实现原理，理解起来就相对轻松许多了。每个动态变量都会对应一个全局绑定关系栈(stack)，在某作用域中遇到一个局部变量定义或新绑定，解释器就会将该新绑定关系压入堆栈；当离开该作用域后，绑定关系被弹出栈。这样当试图确定某个变量的绑定关系时，我们可以直接从栈顶获得绑定关系。如果对应该变量的栈为空时，即判定该变量未绑定任何值，解释器会报错。我们再来通过一个例子来加深一下了解吧：</p>
<p>[1]&gt; (defun foo ()<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (declare (special x))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x))<br />
	FOO<br />
	[2]&gt; (let ((x 6)) (foo))<br />
	*** &#8211; EVAL: variable X has no value</p>
<p>我们定义了一个函数foo，在foo中我们声明了x为动态变量。当我们执行(let ((x 6)) (foo))时，解释器提示X没有绑定任何值。这是怎么回事儿呢？我们还是用原理来一步一步分析。</p>
<p>按照执行顺序，解释器先遇到局部变量x的定义，将x与数值6建立绑定，同时确定x为局部变量，但并非动态变量。执行foo时，foo中的变量x为动态变量，解释器为其建立全局绑定关系栈，但是在foo中并没有变量x的定义，所以x对应的全局绑定关系栈为空，导致执行(print x)时出错。</p>
<p>我们修改一下代码：<br />
	[1]&gt; (defun foo ()<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (declare (special x))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print x))<br />
	FOO<br />
	[2]&gt; (let ((x 6)) (declare (special x)) (foo))<br />
	6</p>
<p>按照执行顺序，解释器先遇到局部变量x的定义，将x与数值6建立绑定，同时发现x被显式声明为动态变量，解释器为x建立全局绑定关系栈，并将其绑定关系(x &lt;- 6)压入栈；接下来执行foo函数，foo中的变量x为动态变量，且当前已经建立全局绑定关系栈，继续执行(print x)，从栈顶得到绑定关系(x &lt;- 6)，得到求值结果，最后解释器离开该作用域，将绑定关系弹出栈。</p>
<p>三、全局变量<br />
	与局部变量对应的是全局变量。全局变量拥有全局作用域，即在一个程序内部的任何地方都可以访问到全局变量。Common Lisp通过defvar或defparameter宏显式定义一个全局变量（虽然这不是必须的）：<br />
	[1]&gt; (defvar *x* 13)<br />
	*X*<br />
	[2]&gt; (print *x*)<br />
	13<br />
	[3]&gt; (defparameter *y* 14)<br />
	*Y*<br />
	[4]&gt; (print *y*)<br />
	14</p>
<p>defvar与defparameter的语法形式如下：<br />
	(defvar var-symbol optional-initial-value<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; optional-documentation-string)<br />
	(defparameter var-symbol initial-value<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; optional-documentation-string)</p>
<p>defvar与defparameter的不同之处在于声明中可以不为全局变量绑定初值，而defparameter则是严格要求必须为声明中的全局变量绑定初值，比如:<br />
	[1]&gt; (defvar *x*)<br />
	*X*<br />
	[2]&gt; (defparameter *y*)<br />
	*** &#8211; The macro DEFPARAMETER may not be called with 1 arguments: (DEFPARAMETER *Y*)</p>
<p>局部变量可以显式地被指定为动态变量，全局变量是否可以呢？我们看看下面的例子：<br />
	[1]&gt; (defvar *x* 11)<br />
	*X*<br />
	[2]&gt; (defun foo ()<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print *x*))<br />
	FOO<br />
	[3]&gt; (foo)<br />
	11<br />
	[4]&gt; (let ((*x* 13)) (foo))<br />
	13<br />
	[3]&gt; (foo)<br />
	11</p>
<p>从例子中我们看出，全局变量本身似乎就是一个动态变量。我们猜的没错。对于使用defvar或defparameter显式定义的全局变量，Common Lisp同时为其赋予了动态属性，也就是说defvar或defparameter定义的全局变量本身就是一个动态变量。其在动态作用域中的工作原理与上面在局部变量中提到的动态变量的原理是相同的。就拿上面的例子来说：<br />
	1) 我们使用defvar定义*x*时，解释器就为*x*建立了全局绑定关系栈，并将绑定关系(*x* &lt;- 11)压入栈；<br />
	2) 执行(foo)时，解释器从栈顶得到*x*的当前值为11；<br />
	3) 执行(let ((*x* 13)) (foo))时，解释器又将动态变量*x*的新绑定关系(*x* &lt;- 13)压入栈，这样在执行后面的(foo)时，栈顶的绑定关系就为(*x* &lt;- 13)，即执行结果为13；当解释器执行体离开该作用域时，绑定关系(*x* &lt;- 13)从栈中弹出；<br />
	4) 最后再执行(foo)，栈顶的绑定关系已经变成了(*x* &lt;- 11)，所以(foo)的执行结果又变成了11。</p>
<p>使用setf也可以隐式地引入一个全局变量，但与使用defvar或defparameter显式定义的全局变量不同，setf引入的全局变量并非动态变量：<br />
	[1]&gt; (setf *x* 11)<br />
	11<br />
	[2]&gt; (defun foo ()<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print *x*))<br />
	FOO<br />
	[3]&gt; (foo)<br />
	11<br />
	[4]&gt; (let ((*x* 13)) (foo))<br />
	11</p>
<p>在使用源文件编写代码的情况下，我们推荐使用defvar或defparameter定义全局变量。</p>
<p>另外不得不提的是动态变量是把双刃剑，动态变量在函数(子程序)层次之间建立一个隐含的关系，减少了参数传递。但同时也降低了程序的可读性，可靠性以及运行性能。</p>
<p>四、常量<br />
	C程序员更喜欢使用宏来表示常量，但Common Lisp提供了专门的defconstant宏来定义一个常量，其语法形式如下：<br />
	(defconstant var-symbol value<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; optional-documentation-string)</p>
<p>关于常量的内容很简单，但这里还是要注意以下两点：<br />
	1、和全局变量类似，Common Lisp常量也有自己的命名惯例，一般以&quot;+constant-name+&quot;为命名格式，名字两旁各加一个加号(+)，不过这个惯例的受重视程度和遵守程度显然不如全局变量那个;<br />
	2、常量的名字不能用于作函数的形式参数，比如：<br />
	[1]&gt; (defconstant +y+ 2)<br />
	+Y+<br />
	[2]&gt; (defun foo (+y+) (print +y+))<br />
	*** &#8211; FUNCTION: +Y+ is a constant, may not be used as a variable</p>
<p style='text-align:left'>&copy; 2011, <a href='https://tonybai.com'>bigwhite</a>. 版权所有. </p>
]]></content:encoded>
			<wfw:commentRss>https://tonybai.com/2011/09/20/c-programers-tame-common-lisp-series-variables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C程序员驯服Common Lisp &#8211; 控制结构</title>
		<link>https://tonybai.com/2011/09/14/c-programers-tame-common-lisp-series-control-structure/</link>
		<comments>https://tonybai.com/2011/09/14/c-programers-tame-common-lisp-series-control-structure/#comments</comments>
		<pubDate>Wed, 14 Sep 2011 12:31:00 +0000</pubDate>
		<dc:creator>bigwhite</dc:creator>
				<category><![CDATA[技术志]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Blogger]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Clisp]]></category>
		<category><![CDATA[Common-Lisp]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Opensource]]></category>
		<category><![CDATA[Programmer]]></category>
		<category><![CDATA[函数]]></category>
		<category><![CDATA[博客]]></category>
		<category><![CDATA[开源]]></category>
		<category><![CDATA[循环语句]]></category>
		<category><![CDATA[控制结构]]></category>
		<category><![CDATA[条件语句]]></category>
		<category><![CDATA[程序员]]></category>
		<category><![CDATA[表达式]]></category>
		<category><![CDATA[顺序语句]]></category>

		<guid isPermaLink="false">http://tonybai.com/2011/09/14/c%e7%a8%8b%e5%ba%8f%e5%91%98%e9%a9%af%e6%9c%8dcommon-lisp-%e6%8e%a7%e5%88%b6%e7%bb%93%e6%9e%84/</guid>
		<description><![CDATA[<p>光有表达式，我们依旧无法写出实用的程序，我们还缺少控制结构(Control Structures)。<br />
<br />
C语言主要有三种控制结构：顺序结构、条件分支结构和循环结构。Common Lisp也实现了类似的控制结构，我们逐一来看。<br />
<br />
一、顺序结构<br />
<br />
顾名思义，顺序结构中的语句或表达式是按其...</p>]]></description>
			<content:encoded><![CDATA[<p>光有<a href="http://tonybai.com/2011/09/02/c-programers-tame-common-lisp-series-expressions/" target="_blank">表达式</a>，我们依旧无法写出实用的程序，我们还缺少控制结构(Control Structures)。</p>
<p><a href="http://tonybai.com/tag/C/" target="_blank">C语言</a>主要有三种控制结构：顺序结构、条件分支结构和循环结构。<a href="http://tonybai.com/2011/06/21/hello-common-lisp/" target="_blank">Common Lisp</a><br />
	也实现了类似的控制结构，我们逐一来看。</p>
<p>一、顺序结构<br />
	顾名思义，顺序结构中的语句或表达式是按其位置的先后顺序依次执行的，这也是最简单也最容易理解的一种结构。在C语言中，绝大多数代码块(code block)中的代码都是顺序结构的。Common Lisp程序由<a href="http://en.wikipedia.org/wiki/S-expression" target="_blank">S-expressions</a>组成，其本质上的执行过程为自左向右的求值过程。不过Common Lisp的代码编排风格会让给大家一种错觉：Common Lisp似乎也是顺序执行的，例如：</p>
<p>;;以下是来自于《<a href="http://www.gigamonkeys.com/book/" target="_blank">Practical Common Lisp</a>》书中的一段代码<br />
	(defun prompt-for-cd ()<br />
	&nbsp; (make-cd<br />
	&nbsp;&nbsp;&nbsp;&nbsp; (prompt-read &quot;Title&quot;)&nbsp;&nbsp;&nbsp;<br />
	&nbsp;&nbsp;&nbsp;&nbsp; (prompt-read &quot;Artist&quot;)<br />
	&nbsp;&nbsp;&nbsp;&nbsp; (or (parse-integer (prompt-read &quot;Rating&quot;) :junk-allowed t) 0)<br />
	&nbsp;&nbsp;&nbsp;&nbsp; (y-or-n-p &quot;Ripped [y/n]: &quot;)))</p>
<p>Common Lisp确实提供了一个Special operator &#8211; progn(注意progn不是函数)，可用于在一个代码块中真正顺序地执行一组表达式。其语法形式如下：<br />
	(progn<br />
	&nbsp;&nbsp;&nbsp; (form-1)<br />
	&nbsp;&nbsp;&nbsp; (form-2)<br />
	&nbsp;&nbsp;&nbsp; .<br />
	&nbsp;&nbsp;&nbsp; .<br />
	&nbsp;&nbsp;&nbsp; .<br />
	&nbsp;&nbsp;&nbsp; (form-N))</p>
<p>Common Lisp会顺序地执行form-1，form-2，&#8230;. form-N，并将最后一个表达式form-N的求值结果作为progn的返回值，例如：<br />
	[1]&gt; (progn<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print &quot;hello world&quot;)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print &quot;hello lisp&quot;)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print &quot;hello graham&quot;))<br />
	&quot;hello world&quot;<br />
	&quot;hello lisp&quot;<br />
	&quot;hello graham&quot;<br />
	&quot;hello graham&quot;</p>
<p>最后的&quot;hello gramham&quot;即为progn的返回值，并被顶层环境再次输出。progn的行为让我想起了C语言中的逗号表达式&quot;expr1, expr2, &#8230; , exprn&quot;，与progn一样，逗号表达式也是依次执行expr1，expr2，&#8230;，并返回最后一个expression的值。</p>
<p>二、条件分支结构<br />
	C语言中最常见的条件分支结构莫过于if语句了。if语句是一个典型的开关结构或叫二选一结构，即if后面的条件成立，执行一个分支；否则执行另外一个分支。其典型结构如下：<br />
	if (cond expr) {<br />
	&nbsp;&nbsp;&nbsp; &#8230; &#8230;<br />
	} else if (cond expr) {<br />
	&nbsp;&nbsp;&nbsp; &#8230; &#8230;<br />
	} else if (cond expr) {<br />
	&nbsp;&nbsp;&nbsp; &#8230; &#8230;<br />
	} else {<br />
	&nbsp;&nbsp;&nbsp; &#8230; &#8230;<br />
	}</p>
<p>Common Lisp中也有if。与progn一样，Common Lisp中的if也是一个special operator而不是函数。函数的原则是必须对所有参数都进行求值，且对每个参数仅进行一次求值；而if和progn则不一定需要对所有&quot;参数&quot;进行求值。Common Lisp中if的语法形式如下：<br />
	(if cond-form<br />
	&nbsp;&nbsp;&nbsp; then-form<br />
	&nbsp;&nbsp;&nbsp; [else-form])</p>
<p>Common Lisp中的if首先对cond-form进行求值，如果为真，则对then-form求值，并将结果返回；否则返回else-form的求值结果。如果没有else-form分支，则返回nil。这与C语言中的条件表达式：&quot;condition_expression ? then_expression : else_expression&quot;甚为相似。下面是一个例子：</p>
<p>[1]&gt; (if (&gt; 3 2) (+ 4 5) (- 11 3))<br />
	9<br />
	[2]&gt; (if (&lt; 3 2) (+ 4 5) (- 11 3))<br />
	8<br />
	[3]&gt; (if (&lt; 3 2) (+ 4 5))<br />
	NIL<br />
	[4]&gt; (if (= 2 2)&nbsp; ;; if级联示例<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (&gt; 3 2) 4 6)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9)<br />
	4</p>
<p>除了if，Common Lisp还提供了其他一些简便实用的条件分支控制operator。</p>
<p>我们常常会在某个条件分支中顺序地执行多个表达式，这种情况下，我们用if实现的代码如下：<br />
	(if (cond-form)<br />
	&nbsp;&nbsp;&nbsp; (progn<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (form1)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (form2)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (form3)))</p>
<p>Common Lisp提供了操作符when来应对如此需求，并简化你的代码：<br />
	(when (cond-form)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (form1)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (form2)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (form3))</p>
<p>当cond-form求值为真时，when会顺序从form1执行到form3。</p>
<p>Common Lisp还提供了unless，用于否定语义的判断：<br />
	(unless (cond-form)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (form1)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (form2)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (form3))</p>
<p>仅当cond-form求值为nil时，form1到form3才会被顺序执行，否则返回nil。</p>
<p>我们日常还会遇到条件分支特别多的情况，如：<br />
	if (cond-1)<br />
	&nbsp;&nbsp;&nbsp; statments-1<br />
	if (cond-2)<br />
	&nbsp;&nbsp;&nbsp; statments-2<br />
	&#8230; &#8230;<br />
	if (cond-n)<br />
	&nbsp;&nbsp;&nbsp; statments-n</p>
<p>此时如果用if来实现，代码就显得层次太深，不够简洁，可读性不好，也难于后续维护：<br />
	(if (cond-1)<br />
	&nbsp;&nbsp;&nbsp; (statments-1)<br />
	&nbsp;&nbsp;&nbsp; (if (cond-2)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (statments-2)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8230;..<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (cond-n)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (statments-n))))</p>
<p>Common Lisp提供了cond操作符来应对这一情况：</p>
<p>(cond<br />
	&nbsp;&nbsp;&nbsp; ((cond-1) (statments-1))<br />
	&nbsp;&nbsp;&nbsp; ((cond-2) (statments-2))<br />
	&nbsp;&nbsp;&nbsp; &#8230; &#8230;<br />
	&nbsp;&nbsp;&nbsp; ((cond-n) (statments-n)))</p>
<p>C语言中还有一种分支结构switch&#8230;case，可用于将一个变量与诸多常量相比较。变量与哪个case中的常量相等，就继续执行该case所在的分支代码。有些资料中将该结构称为选择结构，这里我把它统一划归在条件分支一类中。因为只有满足case条件，执行权才会进入到这个分支：<br />
	switch (expression) {<br />
	&nbsp;&nbsp;&nbsp; case (const expression):<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; statments;<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8230; &#8230;<br />
	&nbsp;&nbsp;&nbsp; case (const expression):<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; statments;<br />
	&nbsp;&nbsp;&nbsp; default:<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; statments;<br />
	}</p>
<p>Common Lisp中也有与switch&#8230;case对应的结构：case。<br />
	[1] &gt; (defun grade-meaning (grade)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (case grade<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((5) &quot;Excellent&quot;)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((4) &quot;Good&quot;)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((3) &quot;Average&quot;)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((2) &quot;Poor&quot;)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((1) &quot;Failing&quot;)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (otherwise &quot;Illegal grade&quot;)))<br />
	GRADE-MEANING<br />
	[2]&gt; (grade-meaning 5)<br />
	&quot;Excellent&quot;<br />
	[3]&gt; (grade-meaning 1)<br />
	&quot;Failing&quot;<br />
	[4]&gt; (grade-meaning 0)<br />
	&quot;Illegal grade&quot;</p>
<p>case结构中的otherwise类似与C语言中switch&#8230;case中的default分支，用于处理默认情况。我们也可以用t代替otherwise，其语义是一样的：<br />
	[5] &gt; (defun grade-meaning (grade)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (case grade<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((5) &quot;Excellent&quot;)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((4) &quot;Good&quot;)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((3) &quot;Average&quot;)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((2) &quot;Poor&quot;)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((1) &quot;Failing&quot;)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (t &quot;Illegal grade&quot;)))</p>
<p>三、循环结构<br />
	和前两种控制结构相比，循环结构相对更加复杂一些。C语言提供了三种循环结构：for，do-while和while。而在Common Lisp中最通用也最灵活的循环结构为do宏。</p>
<p>do宏的语法形式如下：<br />
	(do ((var init-form step-form)*)<br />
	&nbsp;&nbsp;&nbsp; (end-test-form result-form*)<br />
	&nbsp; statement*)</p>
<p>和C语言中的for语句相似，do宏的执行过程也比较复杂：<br />
	1) 在初始化阶段，即循环未开始前，init-form被求值，求值结果赋给var；<br />
	2) 求值end-test-form，如果为nil，则进入子循环体，执行statement*; 如果为真，则求值result-form，并将求值结果作为do的返回值，循环结束;<br />
	3) 每个子循环执行完毕后，都会求值step-form，并用求值结果更新var；<br />
	4) 重复执行步骤2)</p>
<p>我们用个例子来分析一下这个执行过程，下面是一个求0到2的累加和的例子：<br />
	(do ((i 0 (1+ i))<br />
	&nbsp;&nbsp;&nbsp;&nbsp; (sum 0 (+ sum i)))<br />
	&nbsp;&nbsp; ((&gt; i 2) sum))</p>
<p>1) 初始化：i = 0, sum = 0<br />
	2) 求值end-test-form，判断终止条件是否成立，(&gt; 0 2)为nil，进入子循环;<br />
	3) 循环体为空，求值step-form，即i &lt;- 0 + 1，结果i = 1; sum &lt;- sum + i = 0 + 0(注意：这里的i用的是更新前的旧值)，结果sum = 0;&nbsp;<br />
	4) 求值end-test-form，判断终止条件是否成立，(&gt; 1 2)为nil，进入子循环;<br />
	5) 循环体为空，求值step-form，即i &lt;- 1 + 1，结果i = 2; sum &lt;- sum + i = 0 + 1 = 1;&nbsp;<br />
	6) 求值end-test-form，判断终止条件是否成立，(&gt; 2 2)为nil，进入子循环;<br />
	7) 循环体为空，求值step-form，即i &lt;- 2 + 1，结果i = 3; sum &lt;- sum + i = 1 + 2 = 3;&nbsp;<br />
 <img src='https://tonybai.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> 求值end-test-form，判断终止条件是否成立，(&gt; 3 2)为t，求值result-form，即sum = 3，do循环结束，返回值3。</p>
<p>do宏通用性强，但语法及行为复杂。为了简化代码，方便使用，针对两种常见情况Common Lisp基于do宏又提供了dotimes和dolist两个宏。</p>
<p>dotimes宏顾名思义，适用于多次重复执行某个动作，其语法形式：<br />
	(dotimes (var max-count-form)<br />
	&nbsp; body-form*)</p>
<p>其执行流程照比do宏要简单的多，注意max-count-form求值结果必须为一数值：<br />
	1) var初始化为0<br />
	2) 检查循环结束条件：如果var小于max-count-form的求值结果，则求值body-form；否则返回nil<br />
	3) var &lt;- var + 1<br />
	4) 重复执行步骤2)</p>
<p>例如：<br />
	[1] &gt; (dotimes (i 2) (print i))<br />
	0<br />
	1<br />
	NIL</p>
<p>dolist宏适用于迭代处理一个list中的诸多元素，其语法形式如下：<br />
	(dolist (var list-form)<br />
	&nbsp; body-form*)</p>
<p>其执行流程大致如下：<br />
	1) var初始化为list-form的第一个元素<br />
	2) 检查循环结束条件：如果var不为nil，则求值body-form；否则返回nil<br />
	3) var被赋值为list-form中的下一个元素<br />
	4) 重复执行步骤2)</p>
<p>例如：<br />
	[1]&gt; (dolist (i &#039;(1 2 3))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print (* 2 i)))<br />
	2<br />
	4<br />
	6<br />
	NIL</p>
<p>[2]&gt; (defun integer-list-sum (x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (let ((sum 0))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (dolist (i x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setf sum (+ sum i)))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print sum)))<br />
	INTEGER-LIST-SUM<br />
	[3]&gt; (integer-list-sum &#039;(1 2 3 4))<br />
	10</p>
<p>在C语言中，我们可以通过break从循环中主动退出。Common Lisp同样也提供了&quot;break&quot;特性，不过Common Lisp用的是return，例如：</p>
<p>[1]&gt; (do ((n 0 (1+ n))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (cur 0 next)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (next 1 (+ cur next)))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((= 10 n) cur)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (oddp cur)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (progn<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print cur)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (return))))<br />
	1<br />
	NIL</p>
<p>有了三种控制结构，我们就可以用Common Lisp编写出更加富有表现力的实用代码了。以上只是Common Lisp提供的标准控制结构。别忘了，Common Lisp可是一门可编程的编程语言，我们完全可以根据自己的需要定义出更加简洁方便的控制结构，不过这是高级话题了。等我们学到宏的时候再考虑这些吧。现在的首要任务就是熟练掌握这些基本的控制结构^_^。</p>
<p style='text-align:left'>&copy; 2011, <a href='https://tonybai.com'>bigwhite</a>. 版权所有. </p>
]]></content:encoded>
			<wfw:commentRss>https://tonybai.com/2011/09/14/c-programers-tame-common-lisp-series-control-structure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C程序员驯服Common Lisp &#8211; 表达式</title>
		<link>https://tonybai.com/2011/09/02/c-programers-tame-common-lisp-series-expressions/</link>
		<comments>https://tonybai.com/2011/09/02/c-programers-tame-common-lisp-series-expressions/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 03:45:00 +0000</pubDate>
		<dc:creator>bigwhite</dc:creator>
				<category><![CDATA[技术志]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Blogger]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Clisp]]></category>
		<category><![CDATA[Common-Lisp]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Opensource]]></category>
		<category><![CDATA[Programmer]]></category>
		<category><![CDATA[函数]]></category>
		<category><![CDATA[博客]]></category>
		<category><![CDATA[开源]]></category>
		<category><![CDATA[程序员]]></category>
		<category><![CDATA[表达式]]></category>

		<guid isPermaLink="false">http://tonybai.com/2011/09/02/c%e7%a8%8b%e5%ba%8f%e5%91%98%e9%a9%af%e6%9c%8dcommon-lisp-%e8%a1%a8%e8%be%be%e5%bc%8f/</guid>
		<description><![CDATA[<p>Common Lisp程序由一组表达式构成。在"入门"一文中我提到过：Common Lisp使用S-expressions作为表达式(Expressions)的基本语法格式。S-expressions由原子(atoms)和S-expressions列表组成，或者说原子和列表(List)是组成S-expression的基本元素。复杂的源程序都是由简...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Common_Lisp" target="_blank">Common Lisp</a>程序由一组表达式构成。在&quot;<a href="http://tonybai.com/2011/08/30/c-programers-tame-common-lisp-series-introduction/" target="_blank">入门</a>&quot;一文中我提到过：Common Lisp使用<a href="http://en.wikipedia.org/wiki/S-Expression" target="_blank">S-expressions</a>作为表达式(Expressions)的基本语法格式。S-expressions由原子(atoms)和S-expressions列表组成，或者说原子和列表(List)是组成S-expression的基本元素。复杂的源程序都是由简单的表达式组成的，我们在学习编写实用的Common Lisp程序之前，首先要清楚简单表达式的结构和求值方法。</p>
<p>每个Lisp表达式都可以提交给Common Lisp解释器进行求值，并得到一个求值结果。这里我们从简单的原子说起。</p>
<p>一、原子<br />
	原子虽然十分简单，但它也是一种表达式。对于原子而言，其求值结果就是其自身的值。下面我们来看看一些常见的原子以及其求值结果：</p>
<p>(1) 数字(Number)<br />
	数字是一种原子，其求值结果即为其自身数值。</p>
<p>* 整型数字<br />
	[1]&gt; 13<br />
	13<br />
	[2]&gt; -4<br />
	-4<br />
	[3]&gt; 0<br />
	0<br />
	[4]&gt; #xa&nbsp;&nbsp;&nbsp; ;; 16进制数<br />
	10<br />
	[5]&gt; #o11&nbsp;&nbsp; ;; 8进制数<br />
	9<br />
	[6]&gt; #b011&nbsp; ;; 二进制数<br />
	3<br />
	[7]&gt; #24r1n ;; 24进制数<br />
	47</p>
<p>最后的#24r1n是一种通用N进制数表示形式，N取值范围为2到36，其表示形式为#Nr&#8230;。</p>
<p>* 浮点数<br />
	[1]&gt; 3.1415<br />
	13<br />
	[2]&gt; 365e0<br />
	365.0<br />
	[3]&gt; 365e-3<br />
	0.365<br />
	[4]&gt; 365f-3<br />
	0.365<br />
	[5]&gt; 365d-3<br />
	0.365d0<br />
	[6]&gt; 0.365e20<br />
	3.65E19</p>
<p>标志f表示单精度浮点数，标志d表示双精度浮点数，标志e表示默认采用单精度，与f相同。</p>
<p>* 分数<br />
	[1]&gt; 5/6<br />
	5/6</p>
<p>* 复数<br />
	[1]&gt; #C(1.2 3)<br />
	#C(1.2 3)</p>
<p>C(1.2 3)对应的复数为1.2+3i。</p>
<p>(2) 字符(character)<br />
	单独的字符也是原子，其求值结果也是其自身值。</p>
<p>下面是一些可见字符：<br />
	[1]&gt; #\a<br />
	#\a<br />
	[2]&gt; #\A<br />
	#\A<br />
	[3]&gt; #\%<br />
	#\%<br />
	[4]&gt; #\&#038;<br />
	#\&#038;</p>
<p>一些常见的控制字符的形式如下：<br />
	[1]&gt; #\newline<br />
	#\Newline<br />
	[2]&gt; #\tab<br />
	#\Tab<br />
	[3]&gt; #\backspace<br />
	#\Backspace<br />
	[4]&gt; #\space<br />
	#\Space<br />
	[5]&gt; #\escape<br />
	#\Escape</p>
<p>(3) 字符串(string)<br />
	与<a href="http://tonybai.com/tag/C/" target="_blank">C语言</a>中的字符串不同，Common lisp中字符串结尾并不包含&#039;&#039;。但字符串也是原子，其求值结果依旧是其本身。</p>
<p>[1]&gt; &quot;Hello, Common Lisp!&quot;<br />
	&quot;Hello, Common Lisp!&quot;</p>
<p>我们可以字符转义将一些特殊字符放入字符串，比如我们可以在字符串中包含双引号：<br />
	[2]&gt; (format t &quot;~A ~%&quot; &quot;He said \&quot;I am going to see Harry Potter!\&quot; and then he left.&quot;)<br />
	He said &quot;I am going to see Harry Potter!&quot; and then he left.<br />
	NIL</p>
<p>不过我们无法通过转义方法将tab字符、回车符、换行符放入字符串，只能通过键盘手工输入：<br />
	[3]&gt; &quot;Look, here are&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tabs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; and some</p>
<p>returns!</p>
<p>Understand?&quot;<br />
	&quot;Look, here are&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tabs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; and some</p>
<p>returns!</p>
<p>Understand?&quot;</p>
<p>(4) 布尔类型(bool)<br />
	布尔值也是原子，但只有两个可选值：t和nil。t代表true，nil代表false，由于比较简单，这里就不细说了。</p>
<p>(5) 符号(symbols)<br />
	与<a href="http://tonybai.com/tag/C" target="_blank">C语言</a>不同的是，Common Lisp中有一种语法元素称为&quot;符号&quot;。符号有些类似于C语言中的标识符，用来表示Lisp程序中使用的名字，诸如函数和变量的名字，像format，hello-foo, *counter*等。Lisp符号的包容性更强，像+，-等在C中为操作符关键字的字符都可以作为符号。</p>
<p>(a b c)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; 一个包含三个符号的list<br />
	(a 2 &quot;bar&quot;)&nbsp;&nbsp; ; 这个list包含一个符号，一个数字和一个字符串<br />
	(+ (* 2 3) 4) ; 这个list包含一个符号，一个列表以及一个数字</p>
<p>符号的求值比较特殊，如果该符号没有绑定到任何值，解释器会提示错误。如果绑定了值，则显示绑定的值：<br />
	[1]&gt; a</p>
<p>*** &#8211; EVAL: variable A has no value<br />
	The following restarts are available:<br />
	USE-VALUE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :R1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; You may input a value to be used instead of A.<br />
	STORE-VALUE&nbsp;&nbsp;&nbsp; :R2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; You may input a new value for A.<br />
	ABORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :R3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Abort main loop<br />
	Break 1 [2]&gt; :r3<br />
	[3]&gt; (setf a 5)<br />
	5<br />
	[4]&gt; a<br />
	5</p>
<p>二、列表<br />
	在实用程序中，我们很少将原子单独作为表达式，我们更多使用的是List，即列表。之前说过Common Lisp的核心就是List，此List不同于我们以往数据结构中学习的那个List，在Common Lisp中，List是程序和数据的载体，别忘了Lisp是&quot;LISt Processing&quot;的缩写，直译过来Lisp就是List处理语言，这也凸显了List在Lisp语言中的核心地位。</p>
<p>绝大多数情况下，Lisp程序字面上就是一组列表集合。掌握对List进行求值的方法就显得尤为重要了。</p>
<p>我们先从一个简单到不能再简单的List入手：<br />
	[1]&gt; (+ 1 2)<br />
	3</p>
<p>这个List由三个原子组成，一个符号以及两个数字。Common Lisp解释器会首先检查第一个元素是否是一个符号并且是否是一个绑定了有效函数的符号。如果不符合条件则报错。如果第一个元素是符号且绑定合法函数，如+，那么解释器会将后续的元素作为该函数的参数，并自左向右对参数逐个进行求值。</p>
<p>[1]&gt; (length &quot;hello lisp&quot;)<br />
	10<br />
	[2]&gt; (&quot;foo&quot; 1 2)</p>
<p>*** &#8211; EVAL: &quot;foo&quot; is not a function name; try using a symbol instead<br />
	The following restarts are available:<br />
	USE-VALUE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :R1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; You may input a value to be used instead.<br />
	ABORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :R2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Abort main loop</p>
<p>在(+ 1 2)这个例子中，+是一个符合条件的符号，解释器接下来对1和2这两个原子进行求值，前面提到整数是原子，其求值结果即为自身值，所以解释器将1和2传给+，得到最终结果3。</p>
<p>Common Lisp的解释器对参数的求值是自左向右递归进行的，下面是一个稍复杂的表达式，其详细的求值过程如下：</p>
<p>(+ (- 7 (/ 4 2)) (* 3 4))<br />
	-&gt; (+ (- 7 2) (* 3 4))<br />
	-&gt; (+ 5 (* 3 4))<br />
	-&gt; (+ 5 12)<br />
	-&gt; 17</p>
<p>解释器从左向右依次对参数进行求值，解释器遇到函数+的第一个参数(- 7 (/ 4 2))，这显然是也是一个减法表达式，解释器递归地对该表达式进行求值；(- 7 (/ 4 2))表达式的第一个参数7为原子类型，其求值结果为自身值7；第二个参数又是一个除法表达式，解释器再一次进行递归求值，进入(/ 4 2)，这是个简单表达式，其求值结果为2；求值程序回到表达式(- 7 2)，得到求值结果5，至此最外层表达式的第一个参数求值完毕，结果为5；解释器继续对最外层表达式的第二个参数(* 3 4)进行求值，这是个乘法表达式，对该表达式求值结果为12，这样我们的顶层表达式就变成了(+ 5 12)，则最终求值结果就为17。这个过程有点类似于树遍历算法中的深度优先遍历算法。</p>
<p>无论是多么复杂的表达式，Common Lisp解释器的求值方法都是如此的。当然解释器不一定会将函数的所有参数都进行求值，比如：(if t 5 (+ 6 7)这个表达式在if条件为t时，只会求值5这个参数，(+ 6 7)这个表达式不会被求值。</p>
<p style='text-align:left'>&copy; 2011, <a href='https://tonybai.com'>bigwhite</a>. 版权所有. </p>
]]></content:encoded>
			<wfw:commentRss>https://tonybai.com/2011/09/02/c-programers-tame-common-lisp-series-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C程序员驯服Common Lisp &#8211; 入门</title>
		<link>https://tonybai.com/2011/08/30/c-programers-tame-common-lisp-series-introduction/</link>
		<comments>https://tonybai.com/2011/08/30/c-programers-tame-common-lisp-series-introduction/#comments</comments>
		<pubDate>Tue, 30 Aug 2011 06:33:00 +0000</pubDate>
		<dc:creator>bigwhite</dc:creator>
				<category><![CDATA[技术志]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Blogger]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Clisp]]></category>
		<category><![CDATA[Common-Lisp]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Programmer]]></category>
		<category><![CDATA[入门]]></category>
		<category><![CDATA[函数]]></category>
		<category><![CDATA[博客]]></category>
		<category><![CDATA[程序员]]></category>

		<guid isPermaLink="false">http://tonybai.com/2011/08/30/c%e7%a8%8b%e5%ba%8f%e5%91%98%e9%a9%af%e6%9c%8dcommon-lisp-%e5%85%a5%e9%97%a8/</guid>
		<description><![CDATA[<p>毫无疑问，Common Lisp是一门庞大且复杂的语言，学习曲线并不平坦。对于一个从未接触过函数式语言、交互式语言以及动态类型语言的C程序员来说，学习Common Lisp显然是一个很大的挑战。<br /><br />也许有人会问："C语言已经无所不能了，为何还要学习Common Lisp？"在这里我不想...</p>]]></description>
			<content:encoded><![CDATA[<p>毫无疑问，<a href="http://tonybai.com/2011/06/21/hello-common-lisp/" target="_blank">Common Lisp</a>是一门庞大且复杂的语言，学习曲线并不平坦。对于一个从未接触过<a href="http://en.wikipedia.org/wiki/Functional_programming" target="_blank">函数式语言</a>、交互式语言以及动态类型语言的C程序员来说，学习Common Lisp显然是一个很大的挑战。</p>
<p>也许有人会问：&quot;<a href="http://tonybai.com/tag/C/" target="_blank">C语言</a>已经无所不能了，为何还要学习Common Lisp？&quot;在这里我不想说太多冠冕堂皇的话，至少对我而言，理由有三：<br />
	一是好奇，在C语言的世界里待得久了，总想探出头来吸几口新鲜空气，这次我选择了Common Lisp；<br />
	二是为了变成一名更好的程序员。为何学习Common Lisp就能成为一名更好的程序员呢？这不是我的观点，而是诸多牛人或大师们（包括<a href="http://paulgraham.com/" target="_blank">Paul Graham</a>、<a href="http://norvig.com" target="_blank">Peter Norvig</a>以及另外一个Peter：Peter Seibel等）的观点。不过不管你们信不信，反正我是信了。这个观点的关键思想就是一门语言可以影响一个程序员的思维方式。我相信Common Lisp可以给我带来一种不同于以往的新的编程思维方式，这样至少比只有一种思维方式要好，不是吗；<br />
	最后，Lisp是一门可编程的编程语言，可以很容易扩展自身并且创造一门新的语言。我无法不动心于如此一门强大的语言。</p>
<p>学习总是需要一些付出的。Jolt大奖得主《<a href="http://www.gigamonkeys.com/book" target="_blank">Practical Common Lisp</a>》的作者Peter Seibel花了一年的时间放下一切潜心学习Common Lisp并终有所成。我们还有工作，有生活压力，无法像Seibel那样潇洒，但我们依旧可以去学习Common Lisp，循序渐进地学，一步一步来&quot;驯服&quot;Common Lisp这个&quot;猛兽&quot;。&quot;猛兽&quot;被驯服后，才能为你所用，发挥出异常的威力，不是吗？我们需要的仅是恒心和足够的耐心罢了。</p>
<p>&quot;驯服&quot;意味着&quot;学会&quot;，何为学会一门语言？只是知晓语法，看懂代码还远远不够，那些仅仅叫知道或了解或&quot;纸上谈兵&quot;，还谈不上真正地&quot;学会&quot;。古人云：&quot;学以致用&quot;，只有在实际中可以灵活自如的使用了，才叫真正的&quot;学会&quot;了。</p>
<p>现在只是开始！这里我会按照C程序员学习C语言的逻辑展开，为了更加贴近C程序员的思维模式，我选择了这种相对平滑的学习方式。也许最初的几篇会让你觉得Common Lisp很像一门<a href="http://en.wikipedia.org/wiki/Imperative_language" target="_blank">命令式语言</a>^_^！</p>
<p>言归正传！学习一门编程语言之前，最好先弄清楚该语言在当前众多语言中的位置，了解一下它的前世今生，这有助于你对这门语言的认知。不过关于Common Lisp的详细历史这里就不赘述了，在进行下面内容之前，请先阅读一下<a href="http://zh.wikipedia.org/wiki/Common_Lisp" target="_blank">维基百科</a>，或是读读几本经典Common Lisp书籍（如《<a href="http://book.douban.com/subject/1456906/" target="_blank">ANSI Common Lisp</a>》、《<a href="http://www.douban.com/subject/1432683/" target="_blank">On Lisp</a>》以及《Practical Common Lisp》等）中对Common Lisp历史的介绍。</p>
<p>Common Lisp是<a href="http://en.wikipedia.org/wiki/Lisp_(programming_language)" target="_blank">Lisp</a>语言大家族中的一分子，和<a href="http://en.wikipedia.org/wiki/Scheme_(programming_language)" target="_blank">Scheme</a>等一样，它也是一门Lisp方言(Dialect)。与C语言相比，Lisp更加古老，是史上第二古老的编程语言，仅次于<a href="http://en.wikipedia.org/wiki/Fortran" target="_blank">Fortran</a>。但Common Lisp比C年轻，它是在上世纪80年代诞生的。与C语言普遍采用的&quot;编辑-&gt;编译-&gt;调试/执行&quot;的工作方式不同，Common Lisp更多采用的是类似于Python、Ruby那样的交互式的解释器工作模式。你在Common Lisp交互环境中就可以完成上述C语言的所有步骤。这种方式目前看来更易于语言的学习（虽然C语言目前也有解释器的实现，如<a href="http://en.wikipedia.org/wiki/Ch_(computer_programming)" target="_blank">Ch</a>，但C程序员似乎更喜欢传统方式）。</p>
<p>目前市面上Common Lisp的实现有很多种，既有商业收费的，也有开源免费的。商业软件这里就不提了，常用的免费开源的主流Common Lisp解释器包括<a href="http://www.clisp.org" target="_blank">CLISP</a>、<a href="http://www.sbcl.org" target="_blank">SBCL</a>(Steel Bank Common Lisp)和<a href="http://clozure.com" target="_blank">Clozure CL</a>。我个人更喜欢使用CLISP，所以后续有关解释器方面的内容更多以CLISP为主。</p>
<p>CLISP支持诸多平台，你可以很容易得到安装包并顺利的完成安装，关于这方面内容这里就不赘述了。打开一个终端（Windows下打开一个命令行窗口)，敲入&quot;clisp&quot;，回车，你就进入到CLISP提供的Common Lisp顶层环境(Top-Level)当中了(若要进入SBCL，敲入sbcl；若要进入Clozure CL，敲入ccl，以上的前提是这些包的可执行程序路径已经加入到你的PATH环境变量中了)，就像这样：</p>
<p>$ clisp<br />
	&#8230; &#8230;<br />
	Welcome to GNU CLISP 2.44.1 (2008-02-23) &lt;<a href="http://clisp.cons.org/">http://clisp.cons.org/</a>&gt;</p>
<p>Copyright (c) Bruno Haible, Michael Stoll 1992, 1993<br />
	Copyright (c) Bruno Haible, Marcus Daniels 1994-1997<br />
	Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998<br />
	Copyright (c) Bruno Haible, Sam Steingold 1999-2000<br />
	Copyright (c) Sam Steingold, Bruno Haible 2001-2008</p>
<p>Type :h and hit Enter for context help.</p>
<p>[1]&gt; _</p>
<p>对于所谓的&quot;顶层环境&quot;，熟悉Python和Ruby等解释型语言的朋友并不陌生。它就是一个已经加载了标准Common Lisp包的REPL环境。其中REPL是Read-Eval-Print-Loop的缩写。说白了，这就是一个Common Lisp代码的执行环境，你在里面可以输入Common Lisp代码，这些代码可以被直接执行，执行结果也会立刻展现在你的眼前，或如果遇到错误/异常时，你还可以在里面直接进行代码调试。当然了&quot;顶层&quot;还有一个范围(Scope)的概念在里面，用于区分不同变量和函数的作用域。</p>
<p>我们在CLISP中输入一些字符串、字符以及数字以及简单表达式：</p>
<p>[1]&gt; &quot;hello lisp&quot;<br />
	&quot;hello lisp&quot;<br />
	[2]&gt; #\c<br />
	#\c<br />
	[3]&gt; 1<br />
	1<br />
	[4]&gt; (+ 1 2)<br />
	3</p>
<p>CLISP对于我们的输入给予了回应：对于字符串、字符(注意Common Lisp的字符表示法很特别，以#\作为前缀，#\c即C语言中的&#039;c&#039;)以及数字，CLISP进行了回显（实际上是对输入求值后的结果），对于&quot;(+ 1 2)&quot;这个计算1和2之和的表达式，CLISP给出了求值后的结果。</p>
<p>我们继续输入一个a：</p>
<p>[5]&gt; a</p>
<p>*** &#8211; EVAL: variable A has no value<br />
	The following restarts are available:<br />
	USE-VALUE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :R1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; You may input a value to be used instead of A.<br />
	STORE-VALUE&nbsp;&nbsp;&nbsp; :R2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; You may input a new value for A.<br />
	ABORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :R3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Abort main loop<br />
	Break 1 [6]&gt;</p>
<p>与前面不同的是，这次CLISP给出了错误提示，求值器（evaluator)无法找到a绑定的值，CLISP进入异常处理模式，或称作调试模式。CLISP给出了三种选择：我们选择输入:R3，可以回到top-level主循环；选择输入:R2，则可以为a赋值。</p>
<p>Break 1 [6]&gt; :R2<br />
	New A: 5<br />
	5<br />
	[7]&gt; a<br />
	5</p>
<p>SBCL和Clozure CL与CLISP类似，都会有类似的调试模式，退出调试模式的方法参见各自的提示说明即可。</p>
<p>如果要退出CLISP解释器，我们可以输入&quot;(quit)&quot;，注意quit两边的括号也是命令的一部分；在SBCL中，我们可以输入(SB-EXT:QUIT)退出；Clozure CL的退出方法与CLISP相同。</p>
<p>Common Lisp源代码是由一组<a href="http://en.wikipedia.org/wiki/S-Expression" target="_blank">S-expressions</a>(symbolic expression)构成的。什么是S-expression呢？这个在Common Lisp书籍中很难找到答案，因为S-expression是一种组织数据的结构，并不是Lisp独有的，只是Lisp恰好也采用了这种结构来组织存储Lisp的代码和数据罢了。在维基百科上，S-expression有一个递归的定义：&quot;S-expression要么是一个被成为原子(atoms)的单一的数据对象(data object)，要么是一个S-expressions列表(list)。数字、数组、字符串以及符号都是原子&quot;，比如：</p>
<p>[1]&gt; 13<br />
	13<br />
	[2]&gt; #(1 2 3)<br />
	#(1 2 3)<br />
	[3]&gt; &quot;hello&quot;<br />
	&quot;hello&quot;<br />
	[4]&gt; #&#039;length<br />
	#</p>
<p>数字&#039;13&#039;、数组&#039;#(1 2 3)&#039;、字符串&quot;hello&quot;以及符号&#039;length&#039;都是原子。</p>
<p>Lisp将代码和数据都存储于S-expressions当中，这是Lisp与其他主流语言的最大区别之一。我们在编写Common Lisp源码时，需要遵循正确的S-expression格式。前面说过Common Lisp解释器就是一个READ-EVAL-PRINT-LOOP环境，这个环境主要由一个Reader和一个Evaluator构成。Reader负责读取源文件中的文本或者我们在提示符后面输入的文本，检查文本格式是否符合S-Expression要求，直到所有文本都符合格式要求，这样解释器就得到了正确的S-expression：</p>
<p>[1]&gt; (+ 1 2))<br />
	3<br />
	[2]&gt;<br />
	*** &#8211; READ from &#8230; &gt;: an object<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cannot start with #\)</p>
<p>通过上面例子可以看出，Reader识别出了不符合S-expression格式的源码文本。</p>
<p>Reader将文本转换为S-expressions后，Evaluator就开始对S-expression进行校验，校验其是否符合Lisp Code的规范形式(Lisp Form)。</p>
<p>下面的例子说明了Evaluator的作用：</p>
<p>[1]&gt; (foo 1 2)<br />
	*** &#8211; EVAL: undefined function FOO</p>
<p>毫无疑问，(foo 1 2)是一个有效的S-expression，其通过Reader这关是没有问题的。但是当Evaluator对S-expression&quot;(foo 1 2)&quot;进行验证求值时，却发现无法找到函数foo的定义，这行源码不合法。</p>
<p>简单总结Reader和Evaluator的工作流程就是：&quot;源码文本&quot;通过Reader转换为有效的&quot;S-expressions&quot;，后者则由Evaluator转换成有效&quot;Lisp Form&quot;并求值得出结果。</p>
<p><a href="http://tonybai.com/2011/08/05/some-experience-of-common-lisp-beginner/" target="_blank">Common Lisp初学者</a>常常被那满眼的括号所吓住，不过事实上括号并没有那么&quot;可怕&quot;。括号其实主要是给Common Lisp解释器(Reader和Evaluator)用的，而不是给程序员看的。现今的代码编辑器都很智能，基本上可以消除括号在编程过程中给你带来的影响（要说一点影响没有也不太可能）。</p>
<p>Common Lisp支持多种注释形式。在C语言中我们用&#039;//&#039;进行单行注释(<a href="http://en.wikipedia.org/wiki/C99" target="_blank">C99标准</a>引入)，而Common Lisp的单行注释符号为&#039;;&#039;。C语言采用&#039;/*&#8230;*/&#039;进行多行注释，Common Lisp使用的是&#039;#|&#8230;|#&#039;。Common Lisp还提供了一种大多语言都不具备的注释方式，那就是将注释直接写到紧邻函数定义的参数列表后面的位置上，这样通过Common Lisp提供的工具，我们可以轻松地提取出该函数的注释，并生成代码文档，比如：</p>
<p>[1]&gt; (defun foo (x) &quot;test comments&quot; (+ x 1))<br />
	FOO<br />
	[2]&gt; (documentation #&#039;foo t)<br />
	&quot;test comments&quot;</p>
<p>由于Common Lisp括号众多，一个风格良好的Lisp程序需要通过良好风格的代码缩进来保证，这方面我推荐AI领域大师Peter Norvig若干年前编写的一篇有关优秀Lisp编程风格的文章《<a href="http://norvig.com/luv-slides.ps" target="_blank">Tutorial on Good Lisp Programming Style</a>》。</p>
<p>很多C程序员可能还是习惯于将代码写到文件中。Common Lisp解释器提供了将你的源文件加载到顶层环境并直接使用其中的定义的方法：<br />
	;; foo.lisp<br />
	(defun foo (x) &quot;test foo&quot;<br />
	&nbsp;&nbsp; (+ x 1))</p>
<p>[1]&gt; (load &quot;foo.lisp&quot;)<br />
	;; Loading file foo.lisp &#8230;<br />
	;; Loaded file foo.lisp<br />
	T<br />
	[2]&gt; (foo 5)<br />
	6</p>
<p>利用load函数我们可将你的源文件加载到顶层环境中，并在顶层环境里使用该源文件中定义的函数。</p>
<p>编程语言初学者总喜欢在终端控制台上看到自己编写的程序的输出结果，那样会产生一种奇妙的成就感，程序员们多陶醉于其中。C程序员最常用的就是printf函数了，Common Lisp中也有与printf等价的函数，它就是format。这里不是专门讲解format函数的，下面仅仅列举一些常见的例子，这些例子应该可以满足你在学习语言初期的需求了：</p>
<p>* 输出整型数<br />
	(format t &quot;~d&quot; 1000000) ==&gt; 1000000<br />
	(format t &quot;~x&quot; 1000000) ==&gt; f4240<br />
	(format t &quot;~o&quot; 1000000) ==&gt; 3641100<br />
	(format t &quot;~b&quot; 1000000) ==&gt; 11110100001001000000</p>
<p>上面依次是按十进制、16进制、八进制和二进制输出。</p>
<p>* 输出浮点数<br />
	(format t &quot;~f&quot; 3.1415) ==&gt; 3.1415</p>
<p>* 输出字符串<br />
	(format t &quot;~a&quot; &quot;hello lisp&quot;) ==&gt; hello lisp</p>
<p>* 输出字符<br />
	(format t &quot;~c&quot; #\c) ==&gt; c</p>
<p>* 输出换行符<br />
	以下借用《ANSI Common Lisp》书中的一个例子：<br />
	(format nil &quot;Dear ~a, ~% Our records indicate&#8230;&quot; &quot;Mr. Malatesta&quot;)<br />
	==&gt; &quot;Dear Mr. Malatesta,<br />
	&nbsp;Our records indicate&#8230;&quot;</p>
<p>format函数的第一个参数表示是否输出到&quot;标准输出(*STANDARD-OUTPUT*&quot;，如果传入t，则表示输出到标准输出设备上。第二个参数与C中的printf函数的第一个参数类似，是一个格式串，不同的是格式串中的指示符(directive)由printf中的&#039;%&#039;变成了&#039;~&#039;。</p>
<p>为了让大家更加直观地了解Common Lisp源代码到底是什么样子的，下面将给出一个Common Lisp的例子程序，这个程序用来计算参数字符串中大写字母的总个数：</p>
<p>我们先给出一个命令式风格的实现版本：<br />
	;; upper-char-counter.lisp<br />
	(defun upper-char-counter (str)<br />
	&nbsp; (let ((len (length str)) (result 0))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (do ((i 0 (+ i 1)))<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((&gt;= i len) result)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (upper-case-p (char str i)) (setf result (1+ result))))))</p>
<p>即使你不懂Common Lisp语法，你也能大致猜测处理这段代码的逻辑，基本上与下面C代码是等价的：<br />
	int upper_char_counter(const char *str) {<br />
	&nbsp;&nbsp;&nbsp; int result = 0;<br />
	&nbsp;&nbsp;&nbsp; int len = strlen(str);</p>
<p>&nbsp;&nbsp;&nbsp; int i = 0;<br />
	&nbsp;&nbsp;&nbsp; while (i &lt; len) {<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (str[i] &gt;= &#8216;A&#8217; &#038;&#038; str[i] &lt;= &#039;Z&#039;) {<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result++;<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i++;<br />
	&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp; return result;<br />
	}<br />
	&nbsp;<br />
	下面是一个函数式风格的实现版本：</p>
<p>;; upper-char-counter.lisp<br />
	(defun upper-char-counter (str)<br />
	&nbsp;&nbsp; (count-if #&#039;upper-case-p str))</p>
<p>[1]&gt; (load &quot;upper-char-counter.lisp&quot;)<br />
	;; Loading file upper-char-counter.lisp &#8230;<br />
	;; Loaded file upper-char-counter.lisp<br />
	T<br />
	[2]&gt; (upper-char-counter &quot;a5B6CD!&quot;)<br />
	3</p>
<p>这个版本的代码显然更加简洁，但理解起来有些难度。函数count-if接受一个函数和一个字符串作为参数，count-if将函数upper-case-p应用于str中的各个字符上，并将返回true(t)的结果个数累加得到最终返回值。</p>
<p>走到这里，我想大家应该对Common Lisp有了一个感性的认识了，至少可以编写一些命令式风格的简单代码或复制一些现存的代码放到顶层环境中执行了。如果真的是这样，那我的目的就达到了^_^。</p>
<p style='text-align:left'>&copy; 2011, <a href='https://tonybai.com'>bigwhite</a>. 版权所有. </p>
]]></content:encoded>
			<wfw:commentRss>https://tonybai.com/2011/08/30/c-programers-tame-common-lisp-series-introduction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Common Lisp初学点滴</title>
		<link>https://tonybai.com/2011/08/05/some-experience-of-common-lisp-beginner/</link>
		<comments>https://tonybai.com/2011/08/05/some-experience-of-common-lisp-beginner/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 15:05:00 +0000</pubDate>
		<dc:creator>bigwhite</dc:creator>
				<category><![CDATA[技术志]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Blogger]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Clisp]]></category>
		<category><![CDATA[Common-Lisp]]></category>
		<category><![CDATA[Emacs]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Programmer]]></category>
		<category><![CDATA[Slime]]></category>
		<category><![CDATA[Slimv]]></category>
		<category><![CDATA[Vim]]></category>
		<category><![CDATA[函数]]></category>
		<category><![CDATA[博客]]></category>
		<category><![CDATA[程序员]]></category>

		<guid isPermaLink="false">http://tonybai.com/2011/08/05/common-lisp%e5%88%9d%e5%ad%a6%e7%82%b9%e6%bb%b4/</guid>
		<description><![CDATA[<p>Common Lisp是一门Interactive语言，比较容易上手。无论你是用CLISP，SBCL还是Clozure CL，你都可以很快地写出一个"Hello, World"程序出来。不过千万不要因此低估了Common Lisp，前人的经验表明：Common Lisp是门庞大且复杂的语言，其学习曲线可并不低。要...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://tonybai.com/2011/06/21/hello-common-lisp/" target="_blank">Common Lisp</a>是一门Interactive语言，比较容易上手。无论你是用<a href="http://www.clisp.org" target="_blank">CLISP</a>，<a href="http://www.sbcl.org" target="_blank">SBCL</a>还是<a href="http://ccl.clozure.com" target="_blank">Clozure CL</a>，你都可以很快地写出一个&quot;<a href="http://bigwhite.blogbus.com/logs/138644306.html" target="_blank">Hello, World</a>&quot;程序出来。不过千万不要因此低估了Common Lisp，前人的经验表明：Common Lisp是门庞大且复杂的语言，其学习曲线可并不低。要想真正掌握它，需要你有持续的热情、足够的耐心和不断的练习。我接触Common Lisp时间也不长，是个地地道道的初级选手。这段时间看了些书，做了一些练习，这里把我初学Common Lisp过程中的点点滴滴记录下来，以备忘。</p>
<p>俗话说：工欲善其事，必先利其器。Common Lisp开发者们也有着自己一套高效的开发工具。目前无论是在Windows还是在Linux或是其他平台上，最受Lisper们推崇的工具组合是<a href="http://www.gnu.org/software/emacs" target="_blank">Emacs</a>+ <a href="http://common-lisp.net/project/slime" target="_blank">Slime</a>(The Superior Lisp Interaction Mode for Emacs)。鼎鼎大名的Emacs这里就不说了，Slime对于很多非Lisp开发者来说是一个陌生的名字，我们可以把它看成是一种专门为Lisper们提供的一个嵌入到Emacs中的IDE，通过它我们可以在Emacs编辑器中直接进行Lisp代码的求值，编译，宏扩展，符号定义的查找，名字的自动补全以及在线文档查询等操作。我平时开发更多使用的是另外一种编辑神器-<a href="http://http://tonybai.com/2008/12/30/in-depth-study-vim/" target="_blank">VIM</a>，幸运的是已经有人将Slime移植到了Vim下，Slime摇身一变，变成了<a href="http://www.vim.org/scripts/script.php?script_id=2531" target="_blank">Slimv</a>（The Superior Lisp Interaction Mode for Vim）。由于接触时间较短，我目前尚不确定在功能上Slimv是否完全等同于Slime。不过就目前来看，Slimv的确让Vim下Common Lisp代码的编写变的高效了许多。</p>
<p>Slimv的安装极其简单：将Slimv包下载到你的$HOME/.vim下（这里以Linux下的安装为例），直接解压即可。Slimv首先为Vim提供了一种名为Paredit Mode(.vim/doc/paredit.txt )的编辑模式，这种模式专门针对Lisp代码源文件，诸如以.lisp为后缀名的文件。该编辑模式保证内容中所有括号、方括号以及双引号均平衡出现，即成对匹配。当你敲入&quot;(&quot;，该模式会自动补充对应的&quot;)&quot;；删除半个括号时，另半个括号也被自动删除。初次使用Paredit mode很不习惯，特别是不知如何在括号的外层再包裹一层括号，也就是将(list 1 2)变为((list 1 2))。每次在(list 1 2)开始处输入&quot;(&quot;，都会得到&quot;()(list 1 2)&quot;。后来才在Stackoverflow上觅到答案：原来先输入&quot;\&quot;再输入&quot;(&quot;时，Slimv不会自动补充&quot;)&quot;，通过这种方式可以在括号的外围再加上一层括号了，在Lisp实际编程过程当中，嵌套括号的情况还是很多的。</p>
<p>打开一个名为xx.lisp的源文件，Slimv就会自动发挥作用。在Vim的命令模式下，敲入&quot;,c&quot;，Slimv会自动启动Swank Server，这个Server运行着一个Common Lisp的REPL，接收并处理嵌入在Vim中的Slimv client端发出的求值、编译、调试等请求，保存你在Vim中与REPL的session内容。Slimv同时会在Vim里创建一个REPL窗口，不过这仅是用来等待你的输入，真正的求值等操作是在Swank Server完成的。</p>
<p>Slimv会自动Detect你已安装的Common Lisp实现，在我的已经安装过Clisp和SBCL的系统中，Slimv优先选择了SBCL。 关于Slimv，这里不再多说什么了，因为其作者已经编写了一份很详尽的<a href="http://kovisoft.bitbucket.org/tutorial.html" target="_blank">Tutorial</a>在这里，有兴趣的朋友可以参考之。</p>
<p>我在读的Common Lisp书籍主要有两本：一本是&quot;<a href="http://book.douban.com/subject/6021440/" target="_blank">黑客与画家</a>&quot;的作者Paul Graham编写的&quot;<a href="http://book.douban.com/subject/1456906/" target="_blank">ANSI Common Lisp</a>&quot;，另外一本则是Peter Seibel的&quot;<a href="http://www.gigamonkeys.com/book/" target="_blank">Practical Common Lisp</a>&quot;(据说该书的中文译本已由<a href="http://tianchunbinghe.blog.163.com/blog/static/7001201102794634421/" target="_blank">binghe</a>完成)。这一周多来，我快速地浏览了Peter Seibel的&quot;Practical Common Lisp&quot;，除了惊奇于一些之前未曾接触过的特殊语法结构（如Closure）之外，也感叹于Common Lisp的复杂，数不尽的function, macro和special operator让我有些迷失和混淆。另外Peter Seibel自称书中有关macro的例子都很初级，但就是这样初级的macro也是甚难以理解的。关于macro的深入领会，我看只能指望Paul Graham的大作：&quot;ANSI Common Lisp&quot;和&quot;on lisp&quot;了。</p>
<p>另外一本名为&quot;<a href="http://clqr.berlios.de/index.php" target="_blank">Common Lisp Quick Reference</a>&quot;的小书也值得一看，不过更适合Common Lisp老手查阅手册时使用。</p>
<p>浏览完&quot;Practical Common Lisp&ldquo;后，继续精读&quot;ANSI Common Lisp&quot;，并且对其中的习题也不放过。这些练习估计很初级，不过对于我这个初级选手来说正合适。刚刚看完第二章(Welcome to Lisp)，这里将我的习题答案放到这里，供大家批评指正：</p>
<p>练习1.<br />
	(a) 14<br />
	(b) (1 5)<br />
	(c) 7<br />
	(d) (NIL 3)</p>
<p>练习2.<br />
	[1]&gt; (cons &#039;a &#039;(b c))<br />
	(A B C)<br />
	[2]&gt; (cons &#039;a (cons &#039;b (cons &#039;c nil)))<br />
	(A B C)<br />
	[3]&gt; (cons &#039;a (list &#039;b &#039;c))<br />
	(A B C)</p>
<p>练习3.<br />
	[1]&gt; (defun my-fourth (x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (car (cdr (cdr (cdr x)))))<br />
	MY-FOURTH<br />
	[2]&gt; (my-fourth &#039;(1 2 3 4 5))<br />
	4</p>
<p>练习4.&nbsp;&nbsp;&nbsp;<br />
	[1]&gt; (defun my-max (x y)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (&gt; x y) x y))<br />
	MY-MAX<br />
	[2]&gt; (my-max 5 6)<br />
	6<br />
	[3]&gt; (my-max 7 6)<br />
	7</p>
<p>以上方案只适用于整数等适用&gt;进行比较的类型，下面是一个更加通用的版本：</p>
<p>[1]&gt; (defun my-max1 (x y comp_func)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (funcall comp_func x y) x y))<br />
	MY-MAX1<br />
	[2]&gt; (defparameter *cf* (lambda (x y) (if (&gt; x y) t nil)))<br />
	*CF*<br />
	[3]&gt; (my-max1 5 6 *cf*)<br />
	6<br />
	[4]&gt; (my-max1 7 6 *cf*)<br />
	7<br />
	[5]&gt; (defparameter *ccf* (lambda (x y) (if (char&gt; x y) t nil)))<br />
	*CCF*<br />
	[6]&gt;&nbsp; (my-max1 #\c #\b *ccf*)<br />
	#\c<br />
	[7]&gt; (my-max1 #\c #\d *ccf*)<br />
	#\d</p>
<p>练习5.<br />
	(a) enigma函数的功能是找出list中是否有值为nil的元素，如果有，返回T；否则返回nil<br />
	(b) mystery函数的功能是返回x在y列表中的位置(下标)</p>
<p>练习6.<br />
	(a) x = car<br />
	(car (car (cdr &#039;( a (b c) d ) ) ) )</p>
<p>(b) x = or<br />
	(or 13 (/ 1 0))<br />
	注：短路求值，后一项在13为t的情况下不被求值，避免了divide by 0错误</p>
<p>(c) x = apply</p>
<p>注意funcall与apply的区别<br />
	(funcall function arg1 arg2 &#8230;)<br />
	==&nbsp; (apply function arg1 arg2 &#8230; nil)<br />
	==&nbsp; (apply function (list arg1 arg2 &#8230;))</p>
<p>练习7.<br />
	(defun have-list-param-p (x)<br />
	&nbsp; (let ((result nil))<br />
	&nbsp;&nbsp;&nbsp; (dolist (obj x)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (listp obj)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setf result t)))<br />
	&nbsp;&nbsp;&nbsp; result))</p>
<p>[1]&gt; (load &quot;list_param.lisp&quot;)<br />
	;; Loading file list_param.lisp &#8230;<br />
	;; Loaded file list_param.lisp<br />
	T<br />
	[38]&gt; (have-list-param-p &#039;(1 2 3))<br />
	NIL<br />
	[39]&gt; (have-list-param-p &#039;(1 (2 3) 4))<br />
	T</p>
<p>练习8.<br />
	(a)<br />
	iterative solution:<br />
	(defun print_dots (number-of-dots)<br />
	&nbsp; (do ((i 1 (+ i 1)))<br />
	&nbsp;&nbsp;&nbsp; ((&gt; i number-of-dots))<br />
	&nbsp;&nbsp;&nbsp; (format t &quot;.&quot;)))</p>
<p>recursive solution:<br />
	(defun print_dots (number-of-dots)<br />
	&nbsp; (let&nbsp; ((i number-of-dots))<br />
	&nbsp;&nbsp;&nbsp;&nbsp; (if (&gt; i 1)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (print_dots (- number-of-dots 1)))<br />
	&nbsp;&nbsp;&nbsp;&nbsp; (format t &quot;.&quot;)))</p>
<p>练习9.<br />
	(a) 问题所在：remove返回一个新的lst，原来的lst如果包含nil，则+会提示nil is not a number<br />
	修改后：<br />
	(defun summit (lst)<br />
	&nbsp; (setf lst (remove nil lst))&nbsp;<br />
	&nbsp; (apply #&#039;+ lst))</p>
<p>(b) 问题所在：导致无穷递归，提示Program stack overflow. RESET<br />
	修改后：<br />
	(defun summit (lst)<br />
	&nbsp; (if lst (+ (or (car lst) 0) (summit (cdr lst))) 0))<br />
	&nbsp; &nbsp; &nbsp;<br />
	Common Lisp与<a href="http://tonybai.com/2010/11/14/the-chinese-translation-project-for-programming-in-haskell/" target="_blank">Haskell</a>不同，Common Lisp并非纯函数式编程语言，其中包含了诸多命令式(imperative)的元素，这样对于习惯了命令式编程的初学者来说，在学习过程中就不会感觉到过于剧烈的思维跳跃了。</p>
<p style='text-align:left'>&copy; 2011, <a href='https://tonybai.com'>bigwhite</a>. 版权所有. </p>
]]></content:encoded>
			<wfw:commentRss>https://tonybai.com/2011/08/05/some-experience-of-common-lisp-beginner/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Hello，Common Lisp</title>
		<link>https://tonybai.com/2011/06/21/hello-common-lisp/</link>
		<comments>https://tonybai.com/2011/06/21/hello-common-lisp/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 05:43:00 +0000</pubDate>
		<dc:creator>bigwhite</dc:creator>
				<category><![CDATA[技术志]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Blogger]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Clisp]]></category>
		<category><![CDATA[Common-Lisp]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Programmer]]></category>
		<category><![CDATA[SBCL]]></category>
		<category><![CDATA[函数]]></category>
		<category><![CDATA[博客]]></category>
		<category><![CDATA[强类型]]></category>
		<category><![CDATA[程序员]]></category>
		<category><![CDATA[静态类型]]></category>

		<guid isPermaLink="false">http://tonybai.com/2011/06/21/hello%ef%bc%8ccommon-lisp/</guid>
		<description><![CDATA[<p>Paul Graham不愧被誉为Lisp的超级推手，他的煽动力真的是很强悍。这不才刚刚看完一遍他编写的《黑客与画家》后，我就决定将Common Lisp作为今年计划学习的那门新语言，而且从现在就开始。<br /><br />去年曾囫囵吞枣般的学习过Haskell，一门通用且庞大的纯函数式编程语言。在惊...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.paulgraham.com" target="_blank">Paul Graham</a>不愧被誉为<a href="http://en.wikipedia.org/wiki/Lisp_(programming_language)" target="_blank">Lisp</a>的超级推手，他的煽动力真的是很强悍。这不才刚刚看完一遍他编写的《<a href="http://book.douban.com/subject/6021440" target="_blank">黑客与画家</a>》后，我就决定将<a href="http://en.wikipedia.org/wiki/Common_Lisp" target="_blank">Common Lisp</a>作为今年计划学习的那门新语言，而且从现在就开始。</p>
<p>去年曾囫囵吞枣般的学习过<a href="http://tonybai.com/2010/11/14/the-chinese-translation-project-for-programming-in-haskell/" target="_blank">Haskell</a>，一门通用且庞大的纯函数式编程语言。在惊叹于Haskell如此与众不同且功能强大的同时，也为Haskell Monad那魔鬼般的蹩脚语法所苦恼，而Monad的引入就是为了隔离副作用，并让你可以利用些过程式命令式语言解决问题的范式。</p>
<p>原以为Common Lisp也是一门函数式编程语言，应该与Haskell很像，但在看了《<a href="http://book.douban.com/subject/1456906" target="_blank">ANSI Common Lisp</a>》一书后的前几章后我才发现其实不是那么回事儿。Common Lisp设计初衷其实是一门支持多范式的通用语言。除了语法上更接近于函数式编程范式外，你完全可以用Common Lisp写出具有过程式特点的代码（而且看起来也很容易）。另外Common Lisp Object System(CLOS)还给你提供了OO范式的选择。与Haskell相比，对于我这个C程序员来讲，Common Lisp带来思维跳跃似乎更小些，更有利于后续的学习和使用。</p>
<p>与Haskell的强类型和静态类型（即使你不显式指出类型，Haskell也会根据一些上下文的clue推导出类型，如果它发现类有不匹配，那么编译期间就会报错）不同，Common Lisp是动态类型和弱类型的，当然你也可以显式声明类型，但这么做也仅有利于编译器对代码的速度优化，并不能阻止什么。如：<br />
	&gt; (declaim (type fixnum count))<br />
	&gt; NIL<br />
	&gt; (format t count)<br />
	*** &#8211; EVAL: variable COUNT has no value<br />
	&gt; (setf count &quot;hello lisp&quot;)<br />
	&gt; &quot;hello lisp&quot;<br />
	&gt; (format t count)<br />
	&gt; hello lisp<br />
	&gt; NIL</p>
<p>看到了吧，即使我们显式声明了count为fixnum类型，我们依然可以用字符串为其赋值。</p>
<p>Lisp语法十分简单：万物皆在括号内，无论代码还是数据。Lisp一直因括号泛滥而被诟病，不过对于我来说还好，也许是因为在C语言中也没少使用括号的缘故吧。另外现在的编辑器都支持高亮括号匹配，这样只要你细心些，写代码时基本不会出现因括号不匹配导致的一些问题。</p>
<p>编程语言影响思维习惯，但思维的转变不是一蹴而就的，也就是说用惯了C、Python等语言后，再去学习类似Common Lisp这类语言的确有些困难。遇到某问题时，或多或少还会首先以过程式的思维去考虑。另外Common Lisp也确实不是一门&ldquo;小语言&rdquo;，和Haskell一样，Common Lisp也很庞大，这也使得这些语言的学习曲线陡增。<br />
	&nbsp;&nbsp;<br />
	之前一直认为Lisp也是一门解释性的语言，类似Python采用解析器的方式执行，性能不会很快。后来经了解后才得知诸多编译器（如<a href="http://clisp.org/" target="_blank">CLisp</a>）都是将源代码编译为某种格式的字节中间码，这样不仅性能得到了提升，可移植性还得到了兼顾。当然Lisp性能与C比起来还是要差出至少一个数量级的。Common Lisp的编译器我装了两个：CLisp和<a href="http://www.sbcl.org" target="_blank">SBCL</a>，目前来看似乎后者的开发更活跃一些。我个人则更多地使用CLisp。</p>
<p>这里必须得承认的是Lisp语言的应用还是比较小众的，甚至很多程序员都没有听说过有Lisp这门语言的存在。在实际商业开发中更是很少见到用Lisp实现的系统，这方面Haskell也有着同样的&rdquo;感受&ldquo;。不过近几年Lisp各种方言大有回升之势，Lisper们需要是耐心和时间。如果要想了解如何利用Common Lisp进行一些实际系统的开发，那么你就不能放过Peter Seibel于2005年编写的《<a href="http://book.douban.com/subject/1456903" target="_blank">Practical Common Lisp</a>》一书。后来出版的《<a href="http://book.douban.com/subject/3134515" target="_blank">Real World Haskell</a>》想必也是学习Peter Seibel试图为Haskell开发者们找到一条实际应用之道吧。</p>
<p>但有关Common Lisp的入门书，我还是觉得Paul Graham的《Ansi Common Lisp》更适合，另外在这之前可以先拜读一下Paul Graham的文章&quot;<a href="http://www.paulgraham.com/rootsoflisp.html" target="_blank">The Roots of Lisp</a>&quot;，这将对Lisp的学习大有裨益。</p>
<p>既然本文的主题为Hello，Common Lisp，那最后还是按学习新语言的惯例，在这里向大家展示一下用Common Lisp是如何编写<a href="http://en.wikipedia.org/wiki/Hello_world_program" target="_blank">Hello World</a>的吧：</p>
<p>;; HelloWorld.lisp</p>
<p>(defun hello-world ()<br />
	&nbsp; (format t &quot;hello, world!&quot;))</p>
<p>(hello-world)</p>
<p style='text-align:left'>&copy; 2011, <a href='https://tonybai.com'>bigwhite</a>. 版权所有. </p>
]]></content:encoded>
			<wfw:commentRss>https://tonybai.com/2011/06/21/hello-common-lisp/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
