第十三章 用 list 构造表

最后更新于:2022-04-02 04:14:28

[TOC] ## 概述 使用cons构造一个包含多个元素的表十分麻烦,因此 Scheme提供了list操作,这是扩展的 Scheme 语法 ` =list` 和扩展的 Scheme值的集合 `=(1ist..,` 理解list表达式的一种简单方法是将它当作若干cons的简写,具体来说,就是将每个形如 `(list exp-1 .. exp-n)` 的表达式看成是如下表达式的简写 `(cons exp-1 (cons ..(cons exp-n empty))` ## 13.1.1 1. (list 0 1 2 3 4 5 ) ``` (cons 0 (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 empty)))))) ``` 2. (list (list 'adam 0) (list 'eve 1) (list 'louisXIV 2)) ``` ;;(cons x ;; (cons y ;; (cons z empty))) (cons (cons 'adam (cons 0 empty)) (cons (cons 'eve (cons 1 empty)) (cons (cons 'louisXIV (cons 2 empty)) empty))) ``` 3. ``` ;;(cons 1 ;; (cons x ;; (cons y empty))) (cons 1 (cons (cons 1 (cons 2 empty)) (cons (cons 1 (cons 2 (cons 3 empty))) empty))) ``` ## 13.1.2 const 转 list 1. (cons 'a (cons 'b (cons 'c (cons 'd (cons 'e empty))))) ``` (list 'a 'b 'c 'd 'e) ``` 2. (cons (cons 1 (cons 2 empty)) empty) ``` (list (list 1 2)) ``` 3. (cons 'a (cons (cons 1 empty) (cons false empty))) ``` (list 'a (list 1) false) ``` 4. ( cons (cons 1 (cons 2 empty)) (cons (cons 2 (cons 3 empty)) empty)) ``` (list (list 1 2 )(list 2 3)) ``` ## 13.1.3 cons与 list 混合 转 list 1. (cons 'a (list 0 false)) ``` (list 'a 0 false) ``` 2. (list (cons 1 (cons 13 empty))) ``` (list (list 1 13)) ``` 3. (list empty empty (cons 1 empty)) ``` (list empty empty (list 1 )) ;(list '() '() (list 1)) ``` 4. (cons 'a (cons (list 1) (list false empty))) ``` (list 'a (list 1) false empty) ; (list 'a (list 1) #false '()) ``` ## 13.1.4 list 转 list 1. (list (symbol=? 'a 'b) (symbol=? 'c 'c) false) ``` (list false true false) ``` ## 13.1.5 list 使用 first 与 rest 1. (first (list 1 2 3)) ``` ;1 ``` 2. (rest (list 1 2 3)) ``` (list 2 3) ``` ## 恢复 list 及 引号 1. '(1 a 2 b 3 c) ``` (list 1 'a 2 'b 3 'c) ``` 2. '((alan 1000) (barb 200)(carl 1500) (dawn 2300)) ``` (list (list 'alan 1000) (list 'barb 200) (list 'carl 1500) (list 'dawn 2300)) ``` 3. _ ``` '((My Fisrt Paper) (Sean Fisler) (Section 1 (Subsection 1 Life is difficult) (Subsection 2 But learning things makes it interesting)) (Section 2 (Conclustion? What conclustion?))) //解答 (list (list 'My 'Fisrt 'Paper) (list 'Sean 'Fisler) (list 'Section 1 (list 'Subsection 1 'Life 'is 'difficult) (list 'Subsection 2 'But 'learning 'things 'makes 'it 'interesting)) (list 'Section 2 (list 'Conclustion? 'What 'conclustion?))) ```
';