Skip to content

Commit

Permalink
(liii list): value-error for invalid expected len in length=? (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii authored Sep 3, 2024
1 parent 32892d9 commit 8a74626
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 115 deletions.
163 changes: 52 additions & 111 deletions Goldfish.tmu
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,40 @@
\;
</scm-chunk>

<\scm-chunk|tests/goldfish/liii/list-test.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.

;

\;
</scm-chunk>

<section|接口>

Liii List函数库是金鱼标准库中的List函数库,其中的大部分函数来自函数库<scm|(srfi srfi-1)>,小部分是三鲤自己设计的函数。来自SRFI 1的,我们只是在Liii List中导出相关函数名,相关实现和单元测试都在SRFI 1中维护。
Expand Down Expand Up @@ -94,7 +128,9 @@

)

(import (srfi srfi-1))
(import (srfi srfi-1)

\ \ \ \ \ \ \ \ (liii error))

(begin

Expand All @@ -103,7 +139,7 @@

<section|测试>

<\scm-chunk|tests/goldfish/liii/list-test.scm|false|true>
<\scm-chunk|tests/goldfish/liii/list-test.scm|true|true>
(import (liii list)

\ \ \ \ \ \ \ \ (liii check))
Expand All @@ -115,26 +151,6 @@
\;
</scm-chunk>

<\session|elvish|default>
<\output>
Elvish 0.19.2
</output>

<\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 =\<gtr\> #\<less\>unspecified\<gtr\>
</unfolded-io>

<\input|Elvish] >
\;
</input>
</session>

<section|实现>

<paragraph|length=?><scm|(x l) =\<gtr\> boolean>
Expand All @@ -148,18 +164,28 @@
快速判断一个列表<scm|l>的长度是否为<scm|x>。由于<scm|(= x (length l))>这种判断方式的复杂度是<math|O<around*|(|n|)>>,故而需要<scm|length=?>这种快速的判断方式。

<\scm-chunk|tests/goldfish/liii/list-test.scm|true|true>
(check (length=? 3 (list 1 2 3)) =\<gtr\> #t)
(check-true (length=? 3 (list 1 2 3)))

(check (length=? 2 (list 1 2 3)) =\<gtr\> #f)
(check-false (length=? 2 (list 1 2 3)))

(check (length=? 4 (list 1 2 3)) =\<gtr\> #f)
(check-false (length=? 4 (list 1 2 3)))

\;

(check-true (length=? 0 (list )))

(check-catch 'value-error (length=? -1 (list )))

\;
</scm-chunk>

<\scm-chunk|goldfish/liii/list.scm|true|true>
(define (length=? x scheme-list)

\ \ (when (\<less\> x 0)

\ \ \ \ (value-error "length=?: expected non-negative integer x but received ~d" x))

\ \ (cond ((and (= x 0) (null? scheme-list)) #t)

\ \ \ \ \ \ \ \ ((or (= x 0) (null? scheme-list)) #f)
Expand Down Expand Up @@ -450,91 +476,6 @@
<\collection>
<associate|page-medium|paper>
<associate|page-screen-margin|false>
<associate|save-aux|false>
</collection>
</initial>

<\references>
<\collection>
<associate|auto-1|<tuple|1|1|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-10|<tuple|2|3|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-11|<tuple|3|3|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-12|<tuple|flatmap|3|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-13|<tuple|4|3|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-14|<tuple|not-null-list?|4|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-15|<tuple|5|4|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-16|<tuple|null-list?|4|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-17|<tuple|6|4|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-18|<tuple|list-not-null?|4|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-19|<tuple|5|?|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-2|<tuple|1|1|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-3|<tuple|2|1|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-4|<tuple|3|2|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-5|<tuple|4|2|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-6|<tuple|1|2|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-7|<tuple|2|2|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-8|<tuple|list-view|2|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|auto-9|<tuple|1|3|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-goldfish/liii/list.scm-1|<tuple|goldfish/liii/list.scm|1|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-goldfish/liii/list.scm-2|<tuple|goldfish/liii/list.scm|1|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-goldfish/liii/list.scm-3|<tuple|goldfish/liii/list.scm|3|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-goldfish/liii/list.scm-4|<tuple|goldfish/liii/list.scm|3|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-goldfish/liii/list.scm-5|<tuple|goldfish/liii/list.scm|3|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-goldfish/liii/list.scm-6|<tuple|goldfish/liii/list.scm|4|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-goldfish/liii/list.scm-7|<tuple|goldfish/liii/list.scm|4|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-goldfish/liii/list.scm-8|<tuple|goldfish/liii/list.scm|4|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-goldfish/liii/list.scm-9|<tuple|goldfish/liii/list.scm|?|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-tests/goldfish/liii/list-test.scm-1|<tuple|tests/goldfish/liii/list-test.scm|2|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-tests/goldfish/liii/list-test.scm-2|<tuple|tests/goldfish/liii/list-test.scm|2|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-tests/goldfish/liii/list-test.scm-3|<tuple|tests/goldfish/liii/list-test.scm|4|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-tests/goldfish/liii/list-test.scm-4|<tuple|tests/goldfish/liii/list-test.scm|5|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
<associate|chunk-tests/goldfish/liii/list-test.scm-5|<tuple|tests/goldfish/liii/list-test.scm|?|../../.local/share/XmacsLabs/texts/scratch/no_name_47.tm>>
</collection>
</references>

<\auxiliary>
<\collection>
<\associate|figure>
<tuple|normal|<\surround|<hidden-binding|<tuple>|1>|>
使用list处理数据和使用list-view处理数据的对比
</surround>|<pageref|auto-8>>

<tuple|normal|<\surround|<hidden-binding|<tuple>|2>|>
原理的可视化
</surround>|<pageref|auto-9>>
</associate>
<\associate|idx>
<tuple|<tuple|list-view>|<pageref|auto-7>>

<tuple|<tuple|flatmap>|<pageref|auto-11>>

<tuple|<tuple|not-null-list?>|<pageref|auto-13>>

<tuple|<tuple|null-list?>|<pageref|auto-15>>

<tuple|<tuple|list-not-null?>|<pageref|auto-17>>
</associate>
<\associate|toc>
<vspace*|2fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|font-size|<quote|1.19>|1<space|2spc>(liii list)> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>> <no-break><pageref|auto-1><vspace|1fn>

<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|1<space|2spc>许可证> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>> <no-break><pageref|auto-2><vspace|0.5fn>

<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|2<space|2spc>接口> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>> <no-break><pageref|auto-3><vspace|0.5fn>

<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|3<space|2spc>测试> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>> <no-break><pageref|auto-4><vspace|0.5fn>

<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|4<space|2spc>实现> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>> <no-break><pageref|auto-5><vspace|0.5fn>

<with|par-left|<quote|3tab>|list-view <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>> <no-break><pageref|auto-6>>

<with|par-left|<quote|3tab>|flatmap <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>> <no-break><pageref|auto-10>>

<with|par-left|<quote|3tab>|not-null-list? <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>> <no-break><pageref|auto-12>>

<with|par-left|<quote|3tab>|list-null? <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>> <no-break><pageref|auto-14>>

<with|par-left|<quote|3tab>|list-not-null? <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>> <no-break><pageref|auto-16>>

<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|5<space|2spc>结尾> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>> <no-break><pageref|auto-18><vspace|0.5fn>
</associate>
</collection>
</auxiliary>
5 changes: 4 additions & 1 deletion goldfish/liii/list.scm
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@
list-null? list-not-null? not-null-list?
length=?
)
(import (srfi srfi-1))
(import (srfi srfi-1)
(liii error))
(begin

(define (length=? x scheme-list)
(when (< x 0)
(value-error "length=?: expected non-negative integer x but received ~d" x))
(cond ((and (= x 0) (null? scheme-list)) #t)
((or (= x 0) (null? scheme-list)) #f)
(else (length=? (- x 1) (cdr scheme-list)))))
Expand Down
25 changes: 22 additions & 3 deletions tests/goldfish/liii/list-test.scm
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
;
; 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.
;

(import (liii list)
(liii check))

(check-set-mode! 'report-failed)

(check (length=? 3 (list 1 2 3)) => #t)
(check (length=? 2 (list 1 2 3)) => #f)
(check (length=? 4 (list 1 2 3)) => #f)
(check-true (length=? 3 (list 1 2 3)))
(check-false (length=? 2 (list 1 2 3)))
(check-false (length=? 4 (list 1 2 3)))

(check-true (length=? 0 (list )))
(check-catch 'value-error (length=? -1 (list )))

(check ((list-view (list 1 2 3))) => (list 1 2 3))

Expand Down

0 comments on commit 8a74626

Please sign in to comment.