[英]length of list at racket
我试图弄清楚清单的长度
但没有这样做
展示
expected: number?
given: #<procedure:list>
argument position: 1st
other arguments...:
我的代码 :
(define (length len)
(list '(1 2 3 4 5 6 7))
(if (= list null)
(len)
(cdr list)
)
(+ len 1)
(length)
)
(length 0)
我打算做的是
printf len
如果你们有任何错误,请告诉我
我大约两周前试图研究球拍,却不知道该怎么做...
谢谢!
您需要考虑Scheme。 如果您已经知道一种编程语言,那么它一开始将无济于事。
(define (my-lenght lst) ; lst is the list argument
(if (null? lst) ; call function null? to check for empty list
0 ; an empty list has zero i length
(+ 1 ; length is one more
(my-length (cdr lst))))) ; than the list with first element omitted
这是如何调用函数
(my-lenght '(1 2 3)) ; ==> 3
如果您查看代码,则将len
作为函数调用。 函数名称只是指向函数对象的变量,因此+
是变量,而(+ ab)
是具有3个变量的代码。 一个变成函数,另外两个变成数字。
这就是我将系统地使函数查找列表长度的方法。
最终结果将是
;; length : [Listof Element] -> Number
;; Produces the number of elements in the list
(define (length lst)
(cond
[(empty? lst) 0]
[(cons? lst) (+ 1 (length (rest lst)))]))
但是更重要的是系统的过程,您可以用来编写更多的程序。 我在这里使用的过程是《 如何设计程序 》一书中介绍的设计食谱 。
列表为空,或者是第一个元素与其余元素的组合。 此“休息”是另一个列表。
在球拍中,“空”表示为'()
,列表的“组合”表示为cons
。
A [Listof Element] is one of:
- '()
- (cons Element [Listof Element])
列表的一些示例:
'() ; empty
(cons "I'm alone" '()) ; one element "I'm alone"
(cons "Hello" (cons "there" '())) ; two elements "Hello" and "there"
(cons 1 (cons 3 (cons 5 (cons 7 (cons 9 '()))))) ; five elements, odd numbers
length
做什么的? length
函数获取一个列表,并产生一个表示列表中元素数量的数字。 空列表作为长度0
,和上述的实施例应具有的长度0
, 1
, 2
,和5
。
;; length : [Listof Element] -> Number
;; Produces the number of elements in the list
;; (length '()) = 0
;; (length (cons "I'm alone" '())) = 1
;; (length (cons "Hello" (cons "there" '()))) = 2
;; (cons 1 (cons 3 (cons 5 (cons 7 (cons 9 '()))))) = 5
(define (length lst)
???)
列表为空或不利。 要测试其是否为空,我们可以将cond
与问题(empty? lst)
用于空案例,将问题(cons? lst)
用于cons案例。
(define (length lst)
(cond
[(empty? lst) ???]
[(cons? lst) ???]))
空列表没有子项。
但是, (cons Element [Listof Element])
具有两个子部分:
Element
, [Listof Element]
。 要将它们放入球拍中,可以分别使用first
和rest
。
(define (length lst)
(cond
[(empty? lst) ???]
[(cons? lst) ... (first lst) ... (rest lst) ...]))
(first lst)
只是一个Element
。 就length
,它并不复杂,我们不必进一步处理。
(rest lst)
是另一个[Listof Element]
,这很复杂。 我们该如何处理? 通过使用辅助功能。
在这种情况下,我们需要一个与长度相关的辅助函数,并以[Listof Element]
作为参数。 在这种情况下,该辅助函数恰好是length
,即我们当前正在定义的函数! 我们可以在(rest lst)
上递归使用它,因为它是一个较小的子部分。
(define (length lst)
(cond
[(empty? lst) ???]
[(cons? lst) ... (first lst) ... (length (rest lst)) ...]))
第一个示例(length '()) = 0
告诉我们第一个???
孔应填充0
。
(define (length lst)
(cond
[(empty? lst) 0]
[(cons? lst) ... (first lst) ... (length (rest lst)) ...]))
第二孔,所述...
S周围的第一和剩余部分的长度,是更硬。 但是您对长度的直觉应该告诉您,“第一”和“其余”的“组合”列表的长度应为一个加上其余部分的长度。 将其转换为代码:
(define (length lst)
(cond
[(empty? lst) 0]
[(cons? lst) (+ 1 (length (rest lst)))]))
欢迎来到Lisp,@ danny lee!
(define (length-1 lst (acc 0))
(if (null? lst)
acc
(length-1 (cdr lst) (+ 1 acc))))
我使用名字length-1
,是因为Racket基本函数的length
恰好可以完成此函数应做的事情。 (为了不覆盖它,我选择了另一个名称)。
啊,你调用的函数:
(define lst (list 1 3 5 8 7 3))
(length-1 lst) ;; calls length-1 on lst defined above
;; this returns you the value 6, since this is the length of the list
我强烈建议您阅读《 The little Schemer
》一书-它确切地教给您-递归思考-并提供所有与此类似的解决方案-并很好地解释了这些解决方案。 很好的书!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.