第十二章 符號(hào)

2018-02-24 15:45 更新

12.1?簡(jiǎn)介

我會(huì)在本章講解在Lisp/Scheme程序設(shè)計(jì)語(yǔ)言中具有字符性質(zhì)的數(shù)據(jù)類型——符號(hào)。

12.2?有關(guān)符號(hào)的基本函數(shù)

下列都是有關(guān)符號(hào)的基本函數(shù)。

(symbol? x)

如果x是一個(gè)符號(hào)則返回#t。

(string->symbol str)

str轉(zhuǎn)換為符號(hào)。str應(yīng)該都是小寫(xiě)的,否則地址系統(tǒng)可能無(wú)法正常工作。在MIT-Scheme中,(string->symbol "Hello")'Hello是不同的。 scheme (eq? (string->symbol "Hello") 'Hello) ;Value: () (eq? (string->symbol "Hello") (string->symbol "Hello")) ;Value: #t (symbol->string (string->symbol "Hello")) ;Value 15: "Hello"

(symbol->string sym)

將sym轉(zhuǎn)換為字符。

12.3?統(tǒng)計(jì)文本中的單詞

下面的代碼是一段統(tǒng)計(jì)文本中單詞個(gè)數(shù)的程序,這也是被經(jīng)常用作演示符號(hào)的例子。這個(gè)程序使用了哈希表(Hash table)關(guān)聯(lián)表(Association list),這些都將在下一章中講解。

01:     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
02:     ;;;   wc.scm
03:     ;;;   a scheme word-count program
04:     ;;;
05:     ;;;    by T.Shido
06:     ;;;    on August 19, 2005
07:     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
08:     
09:     (define (list->symbol ls0)
10:       (string->symbol (list->string (reverse! ls0))))
11:     
12:     (define (char-in c . ls)
13:       (let loop((ls0 ls))
14:         (if (null? ls0)
15:             #f
16:           (or (char=? c (car ls0))
17:               (loop (cdr ls0))))))
18:     
19:     (define (read-words fname)
20:       (with-input-from-file fname
21:         (lambda ()
22:           (let loop((w '()) (wls '()))
23:             (let ((c (read-char)))
24:           (cond
25:            ((eof-object? c)
26:                 (reverse! (if (pair? w)
27:                               (cons (list->symbol w) wls)
28:                             wls)))
29:            ((char-in c #\Space #\Linefeed #\Tab #\, #\.  #\ #\( #\) #\= #\? #\! #\; #\:)
30:                 (loop '() (if (pair? w)
31:                               (cons (list->symbol w) wls)
32:                             wls)))
33:            (else
34:             (loop (cons (char-downcase c) w) wls))))))))
35:     
36:     (define (sort-by-frequency al)
37:       (sort al (lambda (x y) (> (cdr x) (cdr y)))))
38:     
39:     (define (wc fname)
40:       (let ((wh (make-eq-hash-table)))
41:         (let loop((ls (read-words fname)))
42:           (if (null? ls)
43:               (sort-by-frequency (hash-table->alist wh))
44:             (begin
45:              (hash-table/put! wh (car ls) (1+ (hash-table/get wh (car ls) 0)))
46:              (loop (cdr ls)))))))
(wc "opensource.txt")
?
((the . 208) (to . 142) (a . 104) (of . 103) (and . 83) (that . 75) (is . 73) (in . 65) (i . 64)
(you . 55) (it . 54) (they . 48) (for . 46) (what . 38) (work . 37) (but . 35) (have . 32) (on . 32)
(people . 32) (are . 30) (be . 29) (do . 29) (from . 27) (so . 26) (like . 25) (as . 25) (by . 24)
(source . 24) (not . 23) (open . 23) (can . 23) (we . 22) (was . 22) (one . 22) (it's . 22) (an . 21)
(this . 20) (about . 20) (business . 18) (working . 18) (most . 17) (there . 17) (at . 17) (with . 16)
(don't . 16) (just . 16) (their . 16) (something . 15) (than . 15) (has . 15) (if . 15) (when . 14)
(because . 14) (more . 14) (were . 13) (office . 13) (own . 13) (or . 12) (online . 12) (now . 12)
(blogging . 12) (how . 12) (employees . 11) (them . 11) (think . 11) (time . 11) (company . 11)
(lot . 11) (want . 11) (companies . 10) (could . 10) (know . 10) (get . 10) (learn . 10) (better . 10)
(some . 10) (who . 10) (even . 9) (thing . 9) (much . 9) (no . 9) (make . 9) (up . 9) (being . 9)
(money . 9) (relationship . 9) (that's . 9) (us . 9) (anyone . 8) (average . 8) (bad . 8) (same . 8)
..........)

說(shuō)明:

行號(hào) 函數(shù) 說(shuō)明 09?(list->symbo ls0)?將一個(gè)由字符構(gòu)成的列表(ls0)轉(zhuǎn)換為一個(gè)符號(hào) 12?(char-in c . ls)?檢查

12.4?小結(jié)

符號(hào)是Lisp/Scheme中用于解析分析文段(例如詞數(shù)統(tǒng)計(jì),解析等)的一種字符式數(shù)據(jù)類型,有一些速度很快的函數(shù)適用于符號(hào)。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)