diff --git a/Goldfish.tmu b/Goldfish.tmu new file mode 100644 index 0000000..4367696 --- /dev/null +++ b/Goldfish.tmu @@ -0,0 +1,540 @@ +> + +> + +<\body> + 文档许可证暂时没有确定! + + + + + + <\scm-chunk|goldfish/liii/list.scm|false|true> + ; + + ; Copyright (C) 2024 The Goldfish Scheme Authors + + ; + + ; Licensed under the Apache License, Version 2.0 (the "License"); + + ; you may not use this file except in compliance with the License. + + ; You may obtain a copy of the License at + + ; + + ; http://www.apache.org/licenses/LICENSE-2.0 + + ; + + ; Unless required by applicable law or agreed to in writing, software + + ; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + + ; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + + ; License for the specific language governing permissions and limitations + + ; under the License. + + ; + + \; + + + + + Liii List函数库是金鱼标准库中的List函数库,其中的大部分函数来自函数库,小部分是三鲤自己设计的函数。来自SRFI 1的,我们只是在Liii List中导出相关函数名,相关实现和单元测试都在SRFI 1中维护。 + + <\scm-chunk|goldfish/liii/list.scm|true|true> + (define-library (liii list) + + (export + + \ \ ; SRFI 1: Constructors + + \ \ circular-list iota + + \ \ ; SRFI 1: Predicates + + \ \ null-list? circular-list? + + \ \ ; SRFI 1: Selectors + + \ \ first second third fourth fifth sixth seventh eighth ninth tenth + + \ \ take drop take-right drop-right + + \ \ last-pair last + + \ \ ; SRFI 1: fold, unfold & map + + \ \ count fold fold-right reduce reduce-right + + \ \ filter partition remove + + \ \ ; SRFI 1: Searching + + \ \ find any every list-index + + \ \ take-while drop-while + + \ \ ; SRFI 1: deleting + + \ \ delete + + \ \ ; Liii List extensions + + \ \ list-view flatmap + + \ \ list-null? list-not-null? not-null-list? + + \ \ length=? + + ) + + (import (srfi srfi-1)) + + (begin + + \; + + + + + <\scm-chunk|tests/goldfish/liii/list-test.scm|false|true> + (import (liii list) + + \ \ \ \ \ \ \ \ (liii check)) + + \; + + (check-set-mode! 'report-failed) + + \; + + + <\session|elvish|default> + <\output> + Elvish 0.19.2 + + + <\unfolded-io|Elvish] > + cd ~/git/liii; bin/scheme tests/goldfish/liii/list-test.scm + <|unfolded-io> + \; + + ; *** checks *** : 24 correct, 0 failed. + + tests/goldfish/liii/list-test.scm =\ #\unspecified\ + + + <\input|Elvish] > + \; + + + + + + boolean> + + <\description> + 期望的列表长度,如果长度为负数,该函数会抛出 + + 列表 + + + 快速判断一个列表的长度是否为。由于这种判断方式的复杂度是>,故而需要这种快速的判断方式。 + + <\scm-chunk|tests/goldfish/liii/list-test.scm|true|true> + (check (length=? 3 (list 1 2 3)) =\ #t) + + (check (length=? 2 (list 1 2 3)) =\ #f) + + (check (length=? 4 (list 1 2 3)) =\ #f) + + \; + + + <\scm-chunk|goldfish/liii/list.scm|true|true> + (define (length=? x scheme-list) + + \ \ (cond ((and (= x 0) (null? scheme-list)) #t) + + \ \ \ \ \ \ \ \ ((or (= x 0) (null? scheme-list)) #f) + + \ \ \ \ \ \ \ \ (else (length=? (- x 1) (cdr scheme-list))))) + + \; + + + + + 由于Scheme的List和数据的流向是相反的: + + <\scm-code> + (map (lambda (x) (* x x)) + + \ \ \ \ \ (map (lambda (x) (+ x 1)) + + \ \ \ \ \ \ \ \ \ \ (list 1 2 3))) + + + \; + + 所以我们实现了,采用和Scala的List类似的语法来处理数据: + + <\scm-chunk|tests/goldfish/liii/list-test.scm|true|true> + (check ((list-view (list 1 2 3))) =\ (list 1 2 3)) + + \; + + (check (((list-view (list 1 2 3)) + + \ \ \ \ \ \ \ \ map (lambda (x) (+ x 1)))) =\ (list 2 3 4)) + + \; + + (check (((list-view (list 1 2 3)) + + \ \ \ \ \ \ \ \ map (lambda (x) (+ x 1)) + + \ \ \ \ \ \ \ \ map (lambda (x) (* x x)))) + + \ \ \ \ \ \ \ =\ (list 4 9 16)) + + \; + + + 得到的是函数,需要在外面再加一层括号才能得到。 + + <\big-figure| + <\scm-code> + (map (lambda (x) (* x x)) + + \ \ \ \ \ (map (lambda (x) (+ x 1)) + + \ \ \ \ \ \ \ \ \ \ (list 1 2 3))) + + |<\cell> + <\scm-code> + (((list-view 1 2 3) + + \ \ \ \ \ \ \ \ map (lambda (x) (+ x 1)) + + \ \ \ \ \ \ \ \ map (lambda (x) (* x x)))) + + >>>>> + 使用list处理数据和使用list-view处理数据的对比 + + + 实现list-view时需要考虑三种情况和一种例外情况。 + + <\description> + 也就是直接在list-view得到的结果外面添加括号,此时得到的是list-view对应的list + + 这里举例说明,实际的计算过程是: + + <\enumerate> + 计算并得到结果 (list 2 3 4)> + + 将计算结果包装到 里面,这里使用了这个内置函数 + + + 其实也是树的转换: + + <\big-figure|>>>> + 原理的可视化 + + + 在上述两个递归退出条件写好的情况下,在思考这种一般的情况。 + + 需要计算,其中hf指的是high-order function,也就是高阶函数。也就是需要计算: + + <\scm> + ((((list-view 1 2 3) hf1 f1) hf2 f2) ... hfn fn) + + + \; + + + <\scm-chunk|goldfish/liii/list.scm|true|true> + (define (list-view scheme-list) + + \ \ (define (f-inner-reducer scheme-list filter filter-func rest-funcs) + + \ \ \ \ (cond ((null? rest-funcs) (list-view (filter filter-func scheme-list))) + + \ \ \ \ \ \ \ \ \ \ (else + + \ \ \ \ \ \ \ \ \ \ \ (f-inner-reducer (filter filter-func scheme-list) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (car rest-funcs) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cadr rest-funcs) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cddr rest-funcs))))) + + \ \ (define (f-inner . funcs) + + \ \ \ \ (cond ((null? funcs) scheme-list) + + \ \ \ \ \ \ \ \ \ \ ((length=? 2 funcs) + + \ \ \ \ \ \ \ \ \ \ \ (list-view ((car funcs) (cadr funcs) scheme-list))) + + \ \ \ \ \ \ \ \ \ \ ((even? (length funcs)) + + \ \ \ \ \ \ \ \ \ \ \ (f-inner-reducer scheme-list + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (car funcs) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cadr funcs) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cddr funcs))) + + \ \ \ \ \ \ \ \ \ \ (else (error 'wrong-number-of-args + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ "list-view only accepts even number of args")))) + + \ \ f-inner) + + \; + + + + + <\scm-chunk|goldfish/liii/list.scm|true|true> + (define (flatmap f seq) + + \ \ (fold-right append () (map f seq))) + + \; + + + \; + + + + 的反面,会抛出异常。 + + <\scm-chunk|goldfish/liii/list.scm|true|true> + (define (not-null-list? l) + + \ \ (cond ((pair? l) + + \ \ \ \ \ \ \ \ \ (or (null? (cdr l)) (pair? (cdr l)))) + + \ \ \ \ \ \ \ \ ((null? l) #f) + + \ \ \ \ \ \ \ \ (else + + \ \ \ \ \ \ \ \ \ (error 'type-error "type mismatch")))) + + \; + + + + + 的没有异常的版本,只要不是,都是。 + + <\scm-chunk|goldfish/liii/list.scm|true|true> + (define (list-null? l) + + \ \ (and (not (pair? l)) (null? l))) + + \; + + + + + 的没有异常的版本。 + + \; + + <\scm-chunk|goldfish/liii/list.scm|true|true> + (define (list-not-null? l) + + \ \ (and (pair? l) + + \ \ \ \ \ \ \ (or (null? (cdr l)) (pair? (cdr l))))) + + \; + + + \; + + <\scm-chunk|tests/goldfish/liii/list-test.scm|true|true> + (check (not-null-list? (list 1)) =\ #t) + + (check (list-not-null? (list 1)) =\ #t) + + (check (list-null? (list 1)) =\ #f) + + \; + + (check (not-null-list? (list 1 2 3)) =\ #t) + + (check (list-not-null? (list 1 2 3)) =\ #t) + + (check (list-null? (list 1 2 3)) =\ #f) + + \; + + (check (not-null-list? '(a)) =\ #t) + + (check (list-not-null? '(a)) =\ #t) + + (check (list-null? '(a)) =\ #f) + + \; + + (check (not-null-list? '(a b c)) =\ #t) + + (check (list-not-null? '(a b c)) =\ #t) + + (check (list-null? '(a b c)) =\ #f) + + \; + + (check (not-null-list? ()) =\ #f) + + (check (list-not-null? ()) =\ #f) + + (check (list-null? ()) =\ #t) + + \; + + ; '(a) is a pair and a list + + ; '(a . b) is a pair but not a list + + (check (not-null-list? '(a . b)) =\ #f) + + (check (list-not-null? '(a . b)) =\ #f) + + (check (list-null? '(a . b)) =\ #f) + + \; + + (check-catch 'type-error (not-null-list? 1)) + + (check (list-not-null? 1) =\ #f) + + (check (list-null? 1) =\ #f) + + \; + + + + + <\scm-chunk|goldfish/liii/list.scm|true|false> + ) ; end of begin + + ) ; end of library + + \; + + + \; + + <\scm-chunk|tests/goldfish/liii/list-test.scm|true|false> + (check-report) + + \; + + + +<\initial> + <\collection> + + + + + +<\references> + <\collection> + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + > + + + +<\auxiliary> + <\collection> + <\associate|figure> + |1>|> + 使用list处理数据和使用list-view处理数据的对比 + |> + + |2>|> + 原理的可视化 + |> + + <\associate|idx> + |> + + |> + + |> + + |> + + |> + + <\associate|toc> + |math-font-series||font-size||1(liii list)> |.>>>>|> + + |math-font-series||1许可证> |.>>>>|> + + |math-font-series||2接口> |.>>>>|> + + |math-font-series||3测试> |.>>>>|> + + |math-font-series||4实现> |.>>>>|> + + |list-view |.>>>>|> > + + |flatmap |.>>>>|> > + + |not-null-list? |.>>>>|> > + + |list-null? |.>>>>|> > + + |list-not-null? |.>>>>|> > + + |math-font-series||5结尾> |.>>>>|> + + + diff --git a/tests/liii/base-test.scm b/tests/goldfish/liii/base-test.scm similarity index 100% rename from tests/liii/base-test.scm rename to tests/goldfish/liii/base-test.scm diff --git a/tests/liii/case-test.scm b/tests/goldfish/liii/case-test.scm similarity index 100% rename from tests/liii/case-test.scm rename to tests/goldfish/liii/case-test.scm diff --git a/tests/liii/error-test.scm b/tests/goldfish/liii/error-test.scm similarity index 100% rename from tests/liii/error-test.scm rename to tests/goldfish/liii/error-test.scm diff --git a/tests/liii/hash-table-test.scm b/tests/goldfish/liii/hash-table-test.scm similarity index 100% rename from tests/liii/hash-table-test.scm rename to tests/goldfish/liii/hash-table-test.scm diff --git a/tests/liii/list-test.scm b/tests/goldfish/liii/list-test.scm similarity index 100% rename from tests/liii/list-test.scm rename to tests/goldfish/liii/list-test.scm diff --git a/tests/liii/os-test.scm b/tests/goldfish/liii/os-test.scm similarity index 100% rename from tests/liii/os-test.scm rename to tests/goldfish/liii/os-test.scm diff --git a/tests/liii/path-test.scm b/tests/goldfish/liii/path-test.scm similarity index 100% rename from tests/liii/path-test.scm rename to tests/goldfish/liii/path-test.scm diff --git a/tests/liii/queue-test.scm b/tests/goldfish/liii/queue-test.scm similarity index 100% rename from tests/liii/queue-test.scm rename to tests/goldfish/liii/queue-test.scm diff --git a/tests/liii/stack-test.scm b/tests/goldfish/liii/stack-test.scm similarity index 100% rename from tests/liii/stack-test.scm rename to tests/goldfish/liii/stack-test.scm diff --git a/tests/liii/string-test.scm b/tests/goldfish/liii/string-test.scm similarity index 100% rename from tests/liii/string-test.scm rename to tests/goldfish/liii/string-test.scm diff --git a/tests/liii/uuid-test.scm b/tests/goldfish/liii/uuid-test.scm similarity index 100% rename from tests/liii/uuid-test.scm rename to tests/goldfish/liii/uuid-test.scm diff --git a/tests/scheme/base-test.scm b/tests/goldfish/scheme/base-test.scm similarity index 100% rename from tests/scheme/base-test.scm rename to tests/goldfish/scheme/base-test.scm diff --git a/tests/scheme/boot-test.scm b/tests/goldfish/scheme/boot-test.scm similarity index 100% rename from tests/scheme/boot-test.scm rename to tests/goldfish/scheme/boot-test.scm diff --git a/tests/scheme/case-lambda-test.scm b/tests/goldfish/scheme/case-lambda-test.scm similarity index 100% rename from tests/scheme/case-lambda-test.scm rename to tests/goldfish/scheme/case-lambda-test.scm diff --git a/tests/scheme/char-test.scm b/tests/goldfish/scheme/char-test.scm similarity index 100% rename from tests/scheme/char-test.scm rename to tests/goldfish/scheme/char-test.scm diff --git a/tests/scheme/process-context-test.scm b/tests/goldfish/scheme/process-context-test.scm similarity index 100% rename from tests/scheme/process-context-test.scm rename to tests/goldfish/scheme/process-context-test.scm diff --git a/tests/srfi/srfi-16-test.scm b/tests/goldfish/srfi/srfi-16-test.scm similarity index 100% rename from tests/srfi/srfi-16-test.scm rename to tests/goldfish/srfi/srfi-16-test.scm diff --git a/tests/srfi/srfi-39-test.scm b/tests/goldfish/srfi/srfi-39-test.scm similarity index 100% rename from tests/srfi/srfi-39-test.scm rename to tests/goldfish/srfi/srfi-39-test.scm diff --git a/tests/srfi/srfi-78-test.scm b/tests/goldfish/srfi/srfi-78-test.scm similarity index 100% rename from tests/srfi/srfi-78-test.scm rename to tests/goldfish/srfi/srfi-78-test.scm diff --git a/tests/srfi/srfi-8-test.scm b/tests/goldfish/srfi/srfi-8-test.scm similarity index 100% rename from tests/srfi/srfi-8-test.scm rename to tests/goldfish/srfi/srfi-8-test.scm diff --git a/tests/srfi/srfi-9-test.scm b/tests/goldfish/srfi/srfi-9-test.scm similarity index 100% rename from tests/srfi/srfi-9-test.scm rename to tests/goldfish/srfi/srfi-9-test.scm diff --git a/tests/test_all.scm b/tests/test_all.scm index 57d9089..d5a5a3b 100644 --- a/tests/test_all.scm +++ b/tests/test_all.scm @@ -25,7 +25,7 @@ ; (display (listdir2 "tests")) (define (all-tests) - (((list-view (listdir2 "tests")) + (((list-view (listdir2 "tests/goldfish")) filter path-dir? flatmap listdir2 filter (lambda (x) (path-file? x))