Last active
November 2, 2016 10:49
-
-
Save CYanLong/e8b9dbbff250fb8c6e992ecbb7c4eb9e to your computer and use it in GitHub Desktop.
Scheme List Operation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;Scheme中关于list的操作. | |
; author: cyanlong | |
; date: 2016-11-02 | |
;一:构造list | |
;通过Scheme提供的序对操作. | |
;cons :construct | |
(cons 1 | |
(cons 2 | |
(cons 3 nil))) | |
;output: (1 . #<promise:temp2>) | |
;list 过程便捷构造 | |
(list 1 2 3 4) | |
;二:访问元素 | |
;car 选取表的第一个元素, cdr选取表中除去第一项之后剩下的所有项形成的子表 | |
(define list1 (list 1 2 3 4)) | |
(car list1) ;1 | |
(cdr list1) ;(2 3 4) | |
;2.依次遍历输出list | |
;(define (print items) | |
; (if (null?items) | |
; "." | |
; (car items))) | |
;三:常规的表操作 | |
;1.(list-ref items n) 返回这个表中的第n个项. | |
;方法1: | |
(define (my-list-ref items n) | |
(define (list-iter its count) | |
(if (>= count n) | |
(car its) | |
(list-iter (cdr its) (+ count 1)))) | |
(list-iter items 0)) | |
;方法2: | |
(define (my-list-ref2 items n) | |
(if (= n 0) | |
(car items) | |
(my-list-ref2 (cdr items) (- n 1)))) | |
;test | |
(my-list-ref (list 1 2 3 4) 3) | |
(my-list-ref2 (list 1 2 3 4) 3) | |
;2.(length items) 返回一个列表的长度 | |
;迭代. | |
(define (my-length items) | |
(define (length-iter items count) | |
(if (null? items) | |
count | |
(length-iter (cdr items) (+ count 1)))) | |
(length-iter items 0)) | |
;递归. | |
(define (my-length-recu items) | |
(if (null? items) | |
0 | |
(+ 1 (my-length-recu (cdr items))))) | |
;test | |
(my-length (list 1 2 3 4)) | |
(my-length-recu (list 1 2 3 4)) | |
;3.(append items item) 添加item(基本类型值)到items列表末尾 | |
(define (my-append items item) | |
(if (null? items) | |
(cons item (nil)) | |
(cons (car items) | |
(my-append (cdr items) item)))) | |
;test | |
(define app-list (my-append (list 1 2 3) 4)) | |
(list-ref app-list 3) | |
;(append list1 list2) 两个list连接. | |
(define (my-append list1 list2) | |
(if (null? list1) | |
list2 | |
(cons (car list1) | |
(append (cdr list1) list2)))) | |
;test | |
(define con-list (my-append (list 1 2 3) (list 4 5 6))) | |
(list-ref con-list 4) | |
;4: (last-pari items) 返回只包含给定表里最后一个元素的表. | |
(define (my-last-pari items) | |
(if (null?(cdr items)) | |
items | |
(my-last-pari (cdr items)))) | |
;test | |
(my-last-pari (list 1 2 3 4)) | |
;5.(reverse items) 返回与参数表元素相反的表. | |
;(define (my-reverse items) | |
; (if (null? items) | |
; | |
; (cons ))) | |
;四:高阶操作. | |
;如果没有map等高阶抽象,我们需要将注意力集中到对表的依次遍历上. | |
(define (scale-list items factor) | |
(if (null? items) | |
nil | |
(cons (* (car items) factor) | |
(scale-list (cdr items) factor)))) | |
(scale-list (list 1 2 3) 10) | |
;map: 它由一个过程参数和一个表参数,返回将这一过程应用于表中各个元素得到的结果形成的表. | |
(define (my-map proc items) | |
(if (null? items) | |
nil | |
(cons (proc (car items)) | |
(my-map proc (cdr items))))) | |
(define abs-list (my-map abs (list -1 2 -3))) | |
;test | |
(list-ref abs-list 0) | |
(my-list-ref abs-list 2) | |
;2.for-each | |
;特殊形式begin可以确保多语句按顺序求值. | |
;它可以将多条表达式当作一条表达式来运行. | |
(define (my-for-each proc items) | |
(if (not (null? items)) | |
(begin | |
(proc (car items)) | |
(for-each proc (cdr items))))) | |
"for-each" | |
(my-for-each (lambda (x) (newline)(display x)) (list 1 2 3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment