计数和长度有什么区别?

时间:2014-12-18 20:06:21

标签: count scheme

例如在Scheme(count'(1 2 3))中给出3和(length'(1 2 3))也给出3。

2 个答案:

答案 0 :(得分:2)

这取决于您使用的口译员。在标准Scheme中,仅定义了length。在其他解释器中(比如,Racket)count存在但它不同,它接收一个列表和一个谓词,并返回列表中符合条件的元素数。

我不知道哪个解释器count被定义为返回列表长度的单参数函数,(在Racket (count '(1 2 3))中会导致错误),但似乎对我而言,count只是您翻译中length的别名(换句话说:他们是同一件事) - 当然,请查看文档。如果我必须选择一个,我会使用length,这是标准的,可以在任何地方使用。

答案 1 :(得分:2)

length返回列表中的元素数。

count不是任何官方计划报告中提到的标准程序(我搜索了R5RSR6RSR7RS),因此它不是Scheme的一部分。在许多实现中,您会收到某种错误,指出count不存在。这与您在问题中的预期结果完全不同,但如果您在5个Scheme实现中测试它,则更有可能产生结果。

当其结果与length相同时,没有理由使用依赖于实现的扩展。

我在斯大林(r4rs),scm(r5rs),鸡(r5rs),开局(r5rs),球拍(r5rs和r6rs),ikarus(r6rs),chibi-scheme(r7rs)中尝试(count '(1 2 3)) ),gauche / gosh(r7rs)。他们都没有count

sylwester@pussycat:/p/n/sylwester$ csi

CHICKEN
(c) 2008-2013, The Chicken Team
(c) 2000-2007, Felix L. Winkelmann
Version 4.8.0.5 (stability/4.8.0) (rev 5bd53ac)
linux-unix-gnu-x86-64 [ 64bit manyargs dload ptables ]
compiled 2013-10-03 on aeryn.xorinia.dim (Darwin)

#;1> (count '(1 2 3))

Error: unbound variable: count

    Call history:

    <syntax>      (count (quote (1 2 3)))
    <syntax>      (quote (1 2 3))
    <syntax>      (##core#quote (1 2 3))
    <eval>    (count (quote (1 2 3)))   <--
#;1> 
sylwester@pussycat:/p/n/sylwester$ gsi
Gambit v4.6.9

> (count '(1 2 3))
*** ERROR IN ##raise-unbound-global-exception -- Unbound variable: count
1> 
> 
*** EOF again to exit
sylwester@pussycat:/p/n/sylwester$ ikarus
Ikarus Scheme version 0.0.4-rc1+, 64-bit (revision 1870, build 2012-02-21)
Copyright (c) 2006-2009 Abdulaziz Ghuloum

> (count '(1 2 3))
Unhandled exception
 Condition components:
   1. &undefined
   2. &who: eval
   3. &message: "unbound variable"
   4. &irritants: (count)
> 
sylwester@pussycat:/p/n/sylwester$ plt-r5rs
Welcome to Racket v6.1.1.
R5RS legacy support loaded
> (count '(1 2 3))
count: undefined;
 cannot reference undefined identifier
  context...:
   /usr/share/racket/collects/racket/private/misc.rkt:87:7
   /usr/share/racket/pkgs/r5rs-lib/r5rs/run.rkt: [running body]

sylwester@pussycat:/p/n/sylwester$ echo "(import (rnrs))
                                         (display (count '(1 2 3)))" > test.scm
sylwester@pussycat:/p/n/sylwester$ plt-r6rs test.scm 
test.scm:2:10: count: unbound identifier in module
  in: count
  context...:
   /usr/share/racket/pkgs/r6rs-lib/r6rs/run.rkt: [running body]

sylwester@pussycat:/p/n/sylwester$ chibi-scheme 
> (count '(1 2 3))
ERROR on line 1: undefined variable: count
>
sylwester@pussycat:/p/n/sylwester$ 
sylwester@pussycat:/p/n/sylwester$ gosh
gosh> (length '(1 2 3))
3
gosh> (count '(1 2 3))
*** ERROR: unbound variable: count
Stack Trace:
_______________________________________
gosh> 
sylwester@pussycat:/p/n/sylwester$ scm
SCM version 5e5, Copyright (C) 1990-2006 Free Software Foundation.
SCM comes with ABSOLUTELY NO WARRANTY; for details type `(terms)'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `(terms)' for details.
;loading /usr/share/slib/require
;done loading /usr/share/slib/require.scm
;loading /usr/share/slib/require
;done loading /usr/share/slib/require.scm
;loading /usr/lib/scm/Link
;done loading /usr/lib/scm/Link.scm
;loading /usr/lib/scm/Transcen
;done loading /usr/lib/scm/Transcen.scm
> (count '(1 2 3))

;ERROR: "/usr/lib/scm/Iedline.scm": unbound variable:  count
; in expression: (count (quote (1 2 3)))
; in top level environment.
;STACK TRACE
1; (#@let ((tail (#@lambda (c) (#@if (#@char? #@c) #@c (#@let* (( ...
2; (count (quote (1 2 3)))

> 
sylwester@pussycat:/p/n/sylwester$ echo "(display (count '(1 2 3)))" > test.scm
sylwester@pussycat:/p/n/sylwester$ stalin -On  test.scm
test.scm:1:10:Unbound variable