1 Star 0 Fork 16

TREE_3/Goldfish Scheme

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
liii_stack.tmu 6.93 KB
一键复制 编辑 原始数据 按行查看 历史
<TMU|<tuple|1.1.0|2025.0.7>>
<style|<tuple|generic|chinese|goldfish|literate|reduced-margins>>
<\body>
<\hide-preamble>
<assign|r7rs|<flag|R7RS|dark cyan>>
<assign|srfi|<flag|SRFI|dark red>>
<assign|font|math=Latin Modern Math,cjk=Noto CJK SC,CMU>
</hide-preamble>
<chapter|(liii stack)>
<section|许可证>
<\scm-chunk|goldfish/liii/stack.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>
<\scm-chunk|tests/goldfish/liii/stack-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|接口>
<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define-library (liii stack)
(import (liii lang))
(export stack)
(begin
\;
</scm-chunk>
<section|测试>
<\goldfish-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(import (liii stack) (liii check))
\;
(check-set-mode! 'report-failed)
\;
</goldfish-chunk>
<section|实现>
<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define-case-class stack ((data list?))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
</scm-chunk>
<subsection|选择器>
<paragraph|stack%length>
<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define (%length) (length data))
\;
</scm-chunk>
<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(check ((stack (list 1 2 3)) :length) =\<gtr\> 3)
\;
</scm-chunk>
<paragraph|stack%size>
<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define (%size) (length data))
\;
</scm-chunk>
<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(check ((stack (list 1 2 3)) :size) =\<gtr\> 3)
\;
</scm-chunk>
<paragraph|stack%top>
<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define (%top)
\ \ (if (null? data)
\ \ \ \ \ \ (error 'out-of-range)
\ \ \ \ \ \ (car data)))
\;
</scm-chunk>
<\goldfish-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(check ((stack (list 1 2)) :top) =\<gtr\> 1)
(check-catch 'out-of-range ((stack (list )) :top))
\;
</goldfish-chunk>
<subsection|转换器>
<paragraph|stack%to-list>
<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define (%to-list) data)
\;
</scm-chunk>
<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(check ((stack (list 1 2 3)) :to-list) =\<gtr\> (list 1 2 3))
(check ((stack (list)) :to-list) =\<gtr\> (list))
\;
</scm-chunk>
<paragraph|stack%to-rich-list>
<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define (%to-rich-list) (rich-list data))
\;
</scm-chunk>
<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(check ((stack (list 1 2 3)) :to-rich-list) =\<gtr\> ($ (list 1 2 3)))
\;
</scm-chunk>
<subsection|静态方法>
<paragraph|stack@empty>
<\scm-chunk|goldfish/liii/stack.scm|true|true>
(define (@empty) (stack (list )))
\;
</scm-chunk>
<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(check ((stack :empty) :length) =\<gtr\> 0)
\;
</scm-chunk>
<subsection|修改器>
<paragraph|stack%pop>
<\scm-chunk|goldfish/liii/stack.scm|true|true>
(chained-define (%pop)
\ \ (if (null? data)
\ \ \ \ \ \ (error 'out-of-range "Cannot pop from an empty stack")
\ \ \ \ \ \ (stack (cdr data))))
\;
</scm-chunk>
<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(check ((stack (list 1 2)) :pop) =\<gtr\> (stack (list 2)))
(check ((stack (list 1 2 3)) :pop :pop) =\<gtr\> (stack (list 3)))
(check-catch 'out-of-range ((stack :empty) :pop))
\;
</scm-chunk>
<paragraph|stack%pop!>
<\scm-chunk|goldfish/liii/stack.scm|true|true>
(chained-define (%pop!)
\ \ (if (null? data)
\ \ \ \ \ \ (error 'out-of-range)
\ \ \ \ \ \ (stack (set! data (cdr data))))
\ \ (%this))
\;
</scm-chunk>
<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(let1 t (stack (list 1 2 3))
\ \ (check (t :pop!) =\<gtr\> (stack (list 2 3)))
\ \ (check (t :pop! :pop!) =\<gtr\> (stack (list )))
\ \ (check-catch 'out-of-range ((stack :empty) :pop!)))
\;
</scm-chunk>
<paragraph|stack%push>
<\scm-chunk|goldfish/liii/stack.scm|true|true>
(chained-define (%push element)
\ \ (stack (cons element data)))
\;
</scm-chunk>
<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(let1 t (stack (list 1 2 3))
\ \ (check (t :push 1) =\<gtr\> (stack (list 1 1 2 3)))
\ \ (check (t :push 1 :push 1) =\<gtr\> (stack (list 1 1 1 2 3))))
\;
</scm-chunk>
<paragraph|stack%push!>
<\scm-chunk|goldfish/liii/stack.scm|true|true>
(chained-define (%push! element)\
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (stack (set! data (cons element data)))\
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (%this))
\;
</scm-chunk>
<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|true>
(let1 t (stack (list 1 2 3))
\ \ (check (t :push! 1) =\<gtr\> (stack (list 1 1 2 3)))
\ \ (check (t :push! 1 :push! 1) =\<gtr\> (stack (list 1 1 1 1 2 3)))
\ \ (check (t :pop! :push! 2) =\<gtr\> (stack (list 2 1 1 1 2 3))))
\;
</scm-chunk>
<subsection|结尾>
<\scm-chunk|goldfish/liii/stack.scm|true|false>
) ; end of define-case-class
) ; end of begin
) ; end of define-library
\;
</scm-chunk>
<\scm-chunk|tests/goldfish/liii/stack-test.scm|true|false>
(check-report)
\;
</scm-chunk>
</body>
<\initial>
<\collection>
<associate|font-base-size|12>
<associate|page-height|auto>
<associate|page-orientation|landscape>
<associate|page-screen-margin|false>
<associate|page-type|a4>
<associate|page-width|auto>
<associate|preamble|false>
<associate|save-aux|false>
</collection>
</initial>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Scheme
1
https://gitee.com/tree_3/goldfish.git
git@gitee.com:tree_3/goldfish.git
tree_3
goldfish
Goldfish Scheme
main

搜索帮助