The Racket Guide阅读笔记,chapter 1,chapter2,chapter24.
1 Welcome to Racket
Racket是一门编程语言,Lisp的一种方言,以及Scheme的一个后记。 同时Racket也是一整个编程语言家族以及相关的工具合集。
Racket的主要工具
- racket,编译,解释,运行时体系
- DrRacket, 编程环境
- raco,命令行包管理器
1.1 Interacting with Racket
略
1.2 Definitions and Interactions
略
1.3 Creating Executables
除了使用DrRacket创建可执行文件以外。还可以使用raco exe <src-filename>
来创建可执行文件。
1.4 A Note to Readers with Lisp/Scheme Experience
可以使用Scheme或者Lisp中的load
来加载代码:
> (load "extract.rktl")
> (extract "the dog out")
但是最好使用racket的模块机制。
2 Racket Essentials
2.1 Simple Values
略
2.2 Simple Definitions and Expressions
racket的程序模块形式如:
#lang ‹langname› ‹topform›*
2.2.1 Definitions
一个函数可以有多行表达式,最后一行表达式的值会被作为这个函数的值。其他行只是为了产生副作用而存在:
(define (bake flavor)
(printf "preheating oven...\n")
(string-append flavor " pie"))
2.2.2 An Aside on Indenting Code
略
2.2.3 Identifiers
除了( ) [ ] { } " , ' `` ; # | \
这些字符,以及数字常量,其他的连续非空白字串都可以作为标识符使用。
2.2.4 Function Calls (Procedure Applications)
略
2.2.5 Conditionals with if, and, or, and cond
略
2.2.6 Function Calls, Again
略
2.2.7 Anonymous Functions with lambda
略
2.2.8 Local Binding with define, let, and let*
略
2.3 Lists, Iteration, and Recursion
2.3.1
Predefined List Loops
andmap和ormap把map的返回的连对分别进行and操作和or操作。
scheme中的fold-left在racket中叫做foldl。
racket还提供for/list。
2.3.2
List Iteration from Scratch
first表示car。rest表示cdr。empty产生一个空连,(empty? empty)
返回#t。
2.3.3
Tail Recursion
(define (my-map f lst)
(for/list ([i lst])
(f i)))
会展开为
(define (my-map f lst)
(define (iter lst backward-result)
(cond
[(empty? lst) (reverse backward-result)]
[else (iter (rest lst)
(cons (f (first lst))
backward-result))]))
(iter lst empty))
2.3.4 Recursion versus Iteration
略
2.4 Pairs, Lists, and Racket Syntax
cons产生的是immutable数据。
cons?竟然和pair?等价。
2.4.1 Quoting Pairs and Symbols with quote
A value that prints like a quoted identifier is a symbol.
2.4.2 Abbreviating quote with '
(quote (quote road)
的输出是''road
。
2.4.3 Lists and Racket Syntax
Racket的语法解析分两层:
- 读取层(reader layer),将字符序列转为连对,名号以及其他常量
- 扩展层(expander layer),解析连对,名号以及其他常量,将它们转为表达式。
关于点号,下面的用法是允许的:
(+ 1 . (2))
(1 . < . 2)
(1 . < . 2)
这种不属于传统的用法。
24 Command-Line Tools and Your Editor of Choice
24.1 Command-Line Tools
raco make
将Racket源码编译成字节码。
raco setup
用来管理racket的安装。
raco pkg
用来管理racket的软件包。
REPL下
- ,enter用来在指定模块下跑REPL
- ,edit根据环境变量EDITOR调用编辑其
- ,drracket调用编辑器
Shell补全脚本子在"share/pkgs/shell-completion/racket-completion.bash"以及"share/pkgs/shell-completion/racket-completion.zsh"
24.2 Emacs
介绍一些可用于Racket的Emacs的mode。
24.3 Vim
有几个插件可用,比如vim-racket。
(本篇完)