UP | HOME

符号

Table of Contents

在本章讲解在 Lisp/Scheme 程序设计语言中极具特色的数据类型 : 符号 。符号是一种 通过地址管理字符串 的数据:

  由于符号可以被快速比较,它们被用于做 关联表 和 哈希表 的键

符号的基本函数

下列都是有关符号的基本函数:

  • (symbol? x) : 如果 x 是一个 符号 则返回 #t
  • (string->symbol str) : 将 str 转换为 符号
(eq? (string->symbol "Hello") 'Hello) ; #f 

(eq? (string->symbol "Hello") (string->symbol "Hello")) ; #t 
在MIT-Scheme中,(string->symbol "Hello") 和 'Hello 是不同的

str 应该都是 小写 的,否则地址系统可能无法正常工作
  • (symbol->string sym) : 将 符号 转换为 字符串

实例:统计文本中的单词

下面的代码是一段 统计文本中单词个数 的程序,这也是被经常用作演示如何使用 符号 的例子

    该程序使用了 哈希表 和 关联表

(list->symbo ls0) : 将一个由 字符 构成的列表 ls0 转换为一个 符号

(define (list->symbol ls0)
  (string->symbol
   (list->string (reverse! ls0))))

(char-in c . ls) : 检查字符 c 是否存在表 ls

  • 如果 存在 返回 #t
  • 不存在 返回 #f
(define (char-in c . ls)
  (let loop((ls0 ls))
    (if (null? ls0)
        #f
        (or (char=? c (car ls0))
            (loop (cdr ls0)))))) 

(read-words fname) : 读取一个名为 fname 的文件,并返回一个 符号列表

  • 将 大写转换为小写
  • 将字符表 w 转换为一个字符
  • 符号 添加到符号表 wls
(define (read-words fname)
  (with-input-from-file fname
    (lambda ()
      (let loop((w '()) (wls '()))
        (let ((c (read-char)))
          (cond
           ((eof-object? c) (reverse! (if (pair? w)
                                          (cons (list->symbol w) wls)
                                          wls))) ; 输出符号表
           ((char-in c #\Space #\Linefeed #\Tab #\, #\.  #\ #\( #\) #\= #\? #\! #\; #\:) (loop '() (if (pair? w)
                                                                                                       (cons (list->symbol w) wls)
                                                                                                       wls))) ; 根据特殊符号断字
           (else (loop (cons (char-downcase c) w) wls)))))))) ; 转换为小写

(sort-by-frequency al) : 以 出现频率 降序排序 关联表 al

(define (sort-by-frequency al)
  (sort al (lambda (x y) (> (cdr x) (cdr y)))))

(wc fname) : 读取名为 fname 的文件,并返回一个以 出现频率 降序排序 关联表

  • 因为函数使用了 符号eq-hash-table 是适用的,它使用执行速度很快地eq?比较键
  • 函数统计由 read-words 创建的单词表里各单词的数量,并将其存储在一个哈希表
  • 在统计完成时,将哈希表转换为关联表
(define (wc fname)
  (let ((wh (make-eq-hash-table))) ; 创建哈希表
    (let loop((ls (read-words fname))) ; 创建单词表
      (if (null? ls)
          (sort-by-frequency (hash-table->alist wh)) ; 统计完成时,将哈希表转换为关联表,然后排序 
          (begin
            ;; 单词作为 key, 出现的次数作为值
            (hash-table/put! wh (car ls) (1+ (hash-table/get wh (car ls) 0)))
            (loop (cdr ls)))))))

测试代码:

(wc "siod_preword.txt")
((the . 82) (of . 72) (and . 46) (to . 40) (in . 34) (a . 32) (is . 31) (programs . 29) (lisp . 21) (as . 18) (are . 17) (be . 16) (that . 16) (we . 16) (for . 16) (programming . 15) (computer . 13) (this . 12) (an . 11) (with . 11) (it . 11) (by . 10) (they . 10) (functions . 10) (must . 10) (large . 10) (at . 9) (our . 9) (program . 9) (on . 9) (what . 8) (-- . 8) (or . 8) (these . 8) (within . 7) (languages . 7) (them . 7) (such . 6) (than . 6) (into . 6) (can . 6) (only . 6) (structures . 6) (two . 6) (used . 5) (has . 5) (artificial . 5) (intelligence . 5) (any . 5) (more . 5) (its . 5) (programmer . 5) (matter . 5) (pascal . 5) (which . 5) (book . 5) (become . 5) (important . 5) (have . 5) (from . 5) (data . 5) (model . 5) (all . 4) (new . 4) (organizational . 4) (not . 4) (function . 4) (text . 4) (correctness . 4) (whose . 4) (every . 4) (structure . 4) (most . 4) (foci . 4) (so . 4) (one . 4) (their . 4) (other . 3) (though . 3) (engineering . 3) (human . 3) (never . 3) (change . 3) (creation . 3) (execution . 3) (systems . 3) (detail . 3) (between . 3) (traffic . 3) (say . 3) (itself . 3) (language . 3) (fortran . 3) (no . 3) (active . 3) (scheme . 3) (computers . 3) (task . 3) (but . 3) (use . 3) (semantics . 3) (techniques . 3) (about . 3) (very . 3) (models . 3) (always . 3) (using . 3) (even . 3) (behavior . 3) (both . 3) (place . 3) (do . 3) (perform . 3) (if . 3) (modeled . 3) (another . 3) (some . 3) (problems . 3) (many . 3) (mind . 3) (you . 3) (enough . 3) (three . 2) (enterprises . 2) (primitive . 2) (organisms . 2) (associated . 2) (activity . 2) (organizing . 2) (since . 2) (how . 2) (devices . 2) (time . 2) (there . 2) (greater . 2) (invent . 2) (responsible . 2) (programmers . 2) (ask . 2) (|10| . 2) (art . 2) (deal . 2) (induces . 2) (who . 2) (breathtaking . 2) (should . 2) (few . 2) (adequacy . 2) (end . 2) (symbolic . 2) (idioms . 2) (operate . 2) (rarely . 2) (areas . 2) (application . 2) (metastable . 2) (abstract . 2) (precise . 2) (building . 2) (significant . 2) (algol . 2) (argument . 2) (static . 2) (algorithms . 2) (specifications . 2) (growth . 2) (imposing . 2) (discretionary . 2) (great . 2) (result . 2) (write . 2) (hardware . 2) (larger . 2) (built . 2) (huge . 2) (among . 2) (syntax . 2) (life . 2) (|60| . 2) (may . 2) (where . 2) (study . 2) (number . 2) (armies . 2) (tend . 2) (utility . 2) (experience . 2) (under . 2) (technology . 2) (been . 2) (parsing . 2) (perception . 2) (dialect . 2) (evolve . 2) (often . 2) (themselves . 2) (finding . 2) (small . 2) (difference . 2) (fit . 2) (continue . 2) (almost . 2) (treated . 2) (methods . 2) (well . 2) (processes . 2) (written . 2) (cultures . 2) (simple . 2) (call . 2) (thus . 2) (principles . 2) (would . 2) (found . 2) (still . 2) (collections . 2) (wish . 2) (unlike . 2) (finally . 1) (open . 1) (dreams . 1) (handcrafted . 1) (numbers . 1) (real . 1) (penalizes . 1) (understanding . 1) (writing . 1) (obey . 1) (separation . 1) (dynamic . 1) (except . 1) (square . 1) (fast . 1) (breakthrough . 1) (j . 1) (employs . 1) (learn . 1) (executing . 1) (|``program''| . 1) (struggle . 1) (programmed . 1) (until . 1) (course . 1) (care . 1) (system . 1) (permitting . 1) (determining . 1) (understood . 1) (reinvent . 1) (standard . 1) (peculiar . 1) (contain . 1) (precisely . 1) (balancing . 1) (enterprise . 1) (feet . 1) (boundaries . 1) (said . 1) (assigned . 1) (continual . 1) (complex . 1) (saturated . 1) (blocks . 1) (generate . 1) (less . 1) (|doesn't| . 1) (acquire . 1) (individual . 1) (logic . 1) (else . 1) (construction . 1) (uncovering . 1) (process . 1) (generated . 1) (fluctuating . 1) (proof . 1) (order . 1) (convenience . 1) (scientific . 1) (elementary . 1) (labor . 1) (note . 1) (transcends . 1) (head . 1) (expect . 1) (fits . 1) (impediment . 1) (native . 1) (control . 1) (unchanged . 1) (differential . 1) (longer . 1) (research . 1) (reflected . 1) (beyond . 1) (intellectual . 1) (mechanics . 1) (produced . 1) (ultimately . 1) (root . 1) (pyramids . 1) (lisps . 1) (accompany . 1) (specified . 1) (pushing . 1) (attempt . 1) (route . 1) (element . 1) (perlis . 1) (space . 1) (lives . 1) (gathered . 1) (proven . 1) (plethora . 1) (rate . 1) (develop . 1) (transmit . 1) (too . 1) (coin . 1) (truth . 1) (perfection . 1) (subject . 1) (role . 1) (find . 1) (make . 1) (assault . 1) (performance . 1) (convinced . 1) (parentheses . 1) (notation . 1) (physics . 1) (way . 1) (much . 1) (part . 1) (represented . 1) (mass . 1) (foreword . 1) (same . 1) (useful . 1) (myriads . 1) (multiplicity . 1) (limit . 1) (survivor . 1) (least . 1) (leads . 1) (|program's| . 1) (digestible . 1) (stand . 1) (philosophy . 1) (isolate . 1) (better . 1) (state . 1) (mastery . 1) (toward . 1) (those . 1) (students . 1) (|``toward| . 1) (mathematical . 1) (genes . 1) (consistency . 1) (borne . 1) (parents . 1) (|1| . 1) (times . 1) (constipation . 1) (|1/2| . 1) (inhibits . 1) (correct . 1) (attains . 1) (exceeded . 1) (rife . 1) (relationships . 1) (own . 1) (had . 1) (student . 1) (en . 1) (taskmaster . 1) (psychologists . 1) (give . 1) (through . 1) (value . 1) (digital . 1) (himself . 1) (detailed . 1) (serve . 1) (|100| . 1) (entrusted . 1) (|''| . 1) (guise . 1) (changes . 1) (symbols . 1) (differs . 1) (existence . 1) (pass . 1) (concentrated . 1) (casual . 1) (involves . 1) (periodically . 1) (numerical . 1) (libraries . 1) (developed . 1) (sorting . 1) (appreciate . 1) (continually . 1) (grow . 1) (list . 1) (having . 1) (being . 1) (simplicity . 1) (original . 1) (communicating . 1) (mit . 1) (spate . 1) (lists . 1) (reducing . 1) (nanoseconds . 1) (combine . 1) (per . 1) (arguments . 1) (called . 1) (source . 1) (captured . 1) (powerful . 1) (generals . 1) (issues . 1) (enlarges . 1) (focused . 1) (principle . 1) (evolution . 1) (acceleration . 1) (ways . 1) (rapidly . 1) (scoping . 1) (descriptions . 1) (massive . 1) (akin . 1) (specialization . 1) (quantum . 1) (does . 1) (extraordinarily . 1) (think . 1) (seldom . 1) (particular . 1) (conversely . 1) (turn . 1) (serious . 1) (generalizes . 1) (heat . 1) (stimulated . 1) (enrichment . 1) (perish . 1) (switching . 1) (devoted . 1) (complete . 1) (compare . 1) (insight . 1) (around . 1) (mosaics . 1) (lest . 1) (critical . 1) (books . 1) (logical . 1) (accurately . 1) (intricate . 1) (invention . 1) (binding . 1) (respect . 1) (burden . 1) (exquisite . 1) (latter . 1) (improve . 1) (ones . 1) (interest . 1) (appear . 1) (approaches . 1) (investigation . 1) (arsenal . 1) (restrict . 1) (century . 1) (estimate . 1) (societies . 1) (work . 1) (vitality . 1) (partially . 1) (evolved . 1) (exhilaration . 1) (alan . 1) (via . 1) (yield . 1) (applicability . 1) (density . 1) (promethean . 1) (up . 1) (particularly . 1) (read . 1) (mental . 1) (hatched . 1) (complicated . 1) (explosion . 1) (interprets . 1) (cooperation . 1) (illusion . 1) (simpler . 1) (responsibility . 1) (known . 1) (potential . 1) (fun . 1) (functionality . 1) (amazingly . 1) (growing . 1) (expressed . 1) (|it's| . 1) (arbitrary . 1) (first-course . 1) (parameters . 1) (level . 1) (executes . 1) (closely . 1) (toast . 1) (needs . 1) (inflate . 1) (best . 1) (language-processing . 1) (fitted . 1) (applications . 1) (operates . 1) (organism . 1) (dieticians . 1) (exercises . 1) (variable . 1) (sure . 1) (nests . 1) (calculus . 1) (freedom . 1) (squads . 1) (phenomena . 1) (get . 1) (educators . 1) (quarter . 1) (deepens . 1) (transform . 1) (coalesce . 1) (unfortunately . 1) (sequence . 1) (event . 1) (concerns . 1) (heavy . 1) (identify . 1) (length . 1) (carefully . 1) (primality . 1) (thoughts . 1) (outside . 1) (preparation . 1) (us . 1) (predicate . 1) (processors . 1) (unfolding . 1) (storage . 1) (|lisp's| . 1) (essential . 1) (after . 1) (lines . 1) (doubt . 1) (natural . 1) (haven . 1) (values . 1) (plays . 1) (smoothly . 1) (right . 1) (arising . 1) (supported . 1) (power . 1) (bittersweet . 1) (modules . 1) (quarter-century . 1) (requirements . 1) (physical . 1) (reader . 1) (nonidiosyncratic . 1) (thought . 1) (permanent . 1) (again . 1) (removed . 1) (illustrate . 1) (treatment . 1) (equations . 1) (exportable . 1) (accelerates . 1) (optimal . 1) (without . 1) (maximum . 1) (generates . 1) (interlocking . 1) (computation . 1) (connecticut . 1) (laws . 1) (separately . 1) (largely . 1) (create . 1) (participation . 1) (size . 1) (described . 1) (interact . 1) (pens . 1) (harsh . 1) (his . 1) (merely . 1) (spring . 1) (satisfaction . 1) (explains . 1) (fitting . 1) (|``machine''| . 1) (why . 1) (magnitude . 1) (tactical . 1) (indeed . 1) (goals . 1) (problem . 1) (formal . 1) (hand . 1) (cases . 1) (good . 1) (richness . 1) (pyramid . 1) (electrons . 1) (replicated . 1) (difficult . 1) (collection . 1) (resist . 1) (software . 1) (smallest . 1) (declarable . 1) (early . 1) (discrete . 1) (breed . 1) (acceptable . 1) (composed . 1) (including . 1) (seek . 1) (taxing . 1) (several . 1) (humans . 1) (semantic . 1) (spawns . 1) (millennium . 1) (approximations . 1) (succession . 1) (mechanisms . 1) (material . 1) (different . 1) (anything . 1) (distances . 1) (each . 1) (ability . 1) (crucial . 1))

Next:哈希表

Previous:字符串

Home:目录