代码拉取完成,页面将自动刷新
同步操作将从 墨客实验室/Goldfish Scheme 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<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>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。