我将确定提供字符串列表中的任何字符串是否比所提供的字符串长。我试着用刺长度来计算字符串的长度,但是它不起作用。我知道los不是一个字符串,但是我如何才能转换LoS ->字符串。或者我不需要使用字符串长度。我看了文件。有一个list-string函数,但是列表只包含char。
(define LOS-0 '())
(define LOS-1 (cons "POG" LOS-0))
(define LOS-2 (cons "WooooW" LOS-1))
(define LOS-3 (cons "Yee" LOS-2))
(define (any-longer? los)
(cond
[(empty? los) '()]
[(cons? los)
(if
(< (string-length (first los) (string-length (rest los))))
#true
#false)]))
发布于 2022-05-30 16:04:09
或者只是递归:
(define (any-longer? s los)
(cond ((empty? los) #false)
((< (string-length s) (string-length (car los))) #true)
(else (any-longer? s (cdr los)))))
发布于 2022-05-29 13:35:34
您需要查看字符串列表中的任何元素是否长于给定的字符串。球拍函数memf
是一种简单的方法,结合一个比较字符串长度的函数。它返回其car满足谓词函数的列表的尾,如果没有,则返回#f
。
例如,
(define (any-longer? los str)
(if (memf (lambda (s) (> (string-length s) (string-length str))) los)
#t
#f))
额外的好处是:将其重写为只调用(string-length str)
一次,并编写您自己版本的memf
。(如果这是家庭作业,后者可能是强制性的)
发布于 2022-05-30 02:53:45
您想知道字符串列表中的长度( any )元素是否比字符串的长度长。
若要计算字符串列表中的任何元素是否长于字符串的长度,请执行以下操作:
l
。现在,我们需要计算字符串列表中的任何元素是否比l
长。
1. Is the list empty? If it is then no element of it is longer than `l`.
2. Is the first element of the list not a string? If it's not a string this is an error. This is an optional step but any competent program will include it.
3. So the first element is a string: Is the length of the first element greater than `l`? If it is then that element is longer than the string, and we're done.
4. otherwise, work out whether any element of the _rest_ of the list of strings is longer than `l`. Which you now know how to do!
https://stackoverflow.com/questions/72424010
复制相似问题