Marvin's Blog【程式人生】

Ability will never catch up with the demand for it

27 Feb 2021

The Scheme Programming Language读书笔记【九】

The Scheme Programming Language 读书笔记,chapter 10.

Chapter 10. Libraries and Top-Level Programs

R6RS定义了程序库以及顶级程序。库采用的命名方式有点特别,形如:(a), (a b), (a b ()), (a b (1 2 3))

An implementation of the standard library mechanism is available with the portable implementation of syntax-case at http://www.cs.indiana.edu/syntax-case/.

Section 10.1. Standard Libraries

基础库(rnrs base (6))

下面是单独列出的库:

  • (rnrs arithmetic bitwise (6))
  • (rnrs arithmetic fixnums (6))
  • (rnrs arithmetic flonums (6))
  • (rnrs bytevectors (6))
  • (rnrs conditions (6))
  • (rnrs control (6))
  • (rnrs enums (6))
  • (rnrs eval (6))
  • (rnrs exceptions (6))
  • (rnrs files (6))
  • (rnrs hashtables (6))
  • (rnrs io ports (6))
  • (rnrs io simple (6))
  • (rnrs lists (6))
  • (rnrs mutable-pairs (6))
  • (rnrs mutable-strings (6))
  • (rnrs programs (6))
  • (rnrs r5rs (6))
  • (rnrs records procedural (6))
  • (rnrs records syntactic (6))
  • (rnrs records inspection (6))
  • (rnrs sorting (6))
  • (rnrs syntax-case (6))
  • (rnrs unicode (6))

除了基础库,还有(rnrs (6)),这包括base,以及上面列举的库,但是不包括(rnrs eval (6)), (rnrs mutable-pairs (6)), (rnrs mutable-strings (6)), 以及(rnrs r5rs (6))

Section 10.2. Defining New Libraries

可以用library语法格式来定义新的库:

(library library-name
    (export export-spec ...)
    (import import-spec ...)
    library-body)

Section 10.3. Top-Level Programs

顶层程序不是语法形式,而是存在不同文件中的源代码。顶层程序可以看作是没有library形式的library。并且顶层程序中定义和表达式可以互相混合。所以顶层程序实际上是下列import形式:

(import import-spec ...)
definition-or-expression
...

顶层程序的表示式,如果在定义之前出现,可以看成是某种变量对应的定义,只不过这个变量不是真的存在。

执行诀:

  • (command-line)
  • (exit)

Section 10.4. Examples

Chapter 11. Exceptions and Conditions

异常(Exception)和状况(condition)为系统以及用户提供了发射,检测以及修正错误的能力。

语法形式和执行诀都可能产生异常。异常也可以通过raiseraise-continuable来直接产生。

程序执行的任意时间点,都存在单个异常处理器,也就是current exception handler,用来处理抛出的异常。默认的异常处理器通常打印异常相关的某些信息,比如异常发生的状况;并且在严重的情况下,终止程序的运行。如果是交互的条件下,终止运行意味着重置REPL。

通过guard语法,或者通过with-exception-handler执行诀来自定义异常处理。自定义的异常处理可以只针对部分状况,并且可以嵌套。

Section 11.1. Raising and Handling Exceptions

执行诀

  • (raise obj)
  • (raise-continuable obj)
  • (error who msg irritant ...)
  • (assertion-violation who msg irritant ...)
  • 语法形式(assert expression)
  • (syntax-violation who msg form)
  • (syntax-violation who msg form subform)
  • (with-exception-handler procedure thunk)
  • 语法形式(guard (var clause1 clause2 ...) b1 b2 ...)

Section 11.2. Defining Condition Types

可以在raise或者raise-continuable的时候指定condition对象用来描述异常。

状况类型有点类似记录类型,不过更灵活,一个实际状况可以是两个或者多个不相干的状况类型,也就是复合状况,对应的就是简单状况。

  • 语法&condition
  • (condition? obj)
  • (condition condition ...)
  • (simple-conditions condition)
  • 语法(define-condition-type name parent constructor pred field ...)
  • (condition-predicate rtd)
  • (condition-accessor rtd procedure)

Section 11.3. Standard Condition Types

  • 语法&serious
  • (make-serious-condition)
  • (serious-condition? obj)
(define-condition-type &serious &condition
make-serious-condition serious-condition?)
  • 语法&violation
  • (make-violation)
  • (violation? obj)
(define-condition-type &serious &condition
make-serious-condition serious-condition?)
  • 语法&assertion
  • (make-assertion-violation)
  • (assertion-violation? obj)
  • 语法&error
  • (make-error)
  • (error? obj)
  • 语法&warning
  • (make-warning)
  • (warning? obj)
  • 语法&message
  • (make-message-condition message)
  • (message-condition? obj)
  • (condition-message condition)
  • 语法&irritants
  • (make-irritants-condition irritants)
  • (irritants-condition? obj)
  • (condition-irritants condition)
  • 语法&who
  • (make-who-condition who)
  • (who-condition? obj)
  • (condition-who condition)
  • 语法&non-continuable
  • (make-non-continuable-violation)
  • (non-continuable-violation? obj)
  • 语法&implementation-restriction
  • (make-implementation-restriction-violation)
  • (implementation-restriction-violation? obj)
  • 语法&lexical
  • (make-lexical-violation)
  • (lexical-violation? obj)
  • 语法&syntax
  • (make-syntax-violation form subform)
  • (syntax-violation? obj)
  • (syntax-violation-form condition)
  • (syntax-violation-subform condition)
  • 语法&undefined
  • (make-undefined-violation)
  • (undefined-violation? obj)
  • 语法&i/o
  • (make-i/o-error)
  • (i/o-error? obj)
  • 语法&i/o-read
  • (make-i/o-read-error)
  • (i/o-read-error? obj)
  • 语法&i/o-write
  • (make-i/o-write-error)
  • (i/o-write-error? obj)
  • 语法&i/o-invalid-position
  • (make-i/o-invalid-position-error position)
  • (i/o-invalid-position-error? obj)
  • (i/o-error-position condition)
  • 语法&i/o-filename
  • (make-i/o-filename-error filename)
  • (i/o-filename-error? obj)
  • (i/o-error-filename condition)
  • 语法&i/o-file-protection
  • (make-i/o-file-protection-error filename)
  • (i/o-file-protection-error? obj)
  • 语法&i/o-file-is-read-only
  • (make-i/o-file-is-read-only-error filename)
  • (i/o-file-is-read-only-error? obj)
  • 语法&i/o-file-already-exists
  • (make-i/o-file-already-exists-error filename)
  • (i/o-file-already-exists-error? obj)
  • 语法&i/o-file-does-not-exist
  • (make-i/o-file-does-not-exist-error filename)
  • (i/o-file-does-not-exist-error? obj)
  • 语法&i/o-port
  • (make-i/o-port-error pobj)
  • (i/o-port-error? obj)
  • (i/o-error-port condition)
  • 语法&i/o-decoding
  • (make-i/o-decoding-error pobj)
  • (i/o-decoding-error? obj)
  • 语法&i/o-encoding
  • (make-i/o-encoding-error pobj cobj)
  • (i/o-encoding-error? obj)
  • (i/o-encoding-error-char condition)
  • 语法&no-infinities
  • (make-no-infinities-violation)
  • (no-infinities-violation? obj)
  • 语法&no-nans
  • (make-no-nans-violation)
  • (no-nans-violation? obj)

Chapter 12. Extended Examples

(本篇完)

Categories

Tags