List 球拍:检查列表中是否有列表
这是我的第一个问题!:) 我需要一个函数来检查列表中是否有列表。当列表中有一个列表时,它应该给出false。我尝试过一些简单的事情,比如:List 球拍:检查列表中是否有列表,list,lambda,racket,List,Lambda,Racket,这是我的第一个问题!:) 我需要一个函数来检查列表中是否有列表。当列表中有一个列表时,它应该给出false。我尝试过一些简单的事情,比如: (定义(列表中的列表?ls) (if(或(list?(first ls))(list?(rest ls)))false-true)) 我可能需要lambda但我不知道怎么做? 非常感谢您的帮助 空列表中没有列表 否则,如果 它的第一个元素是列表 或者如果在列表的其余部分中有一个列表 诀窍是将其转化为代码,特别仔细地思考如何表达最后一种情况:要做到这
(定义(列表中的列表?ls)
(if(或(list?(first ls))(list?(rest ls)))false-true))
我可能需要lambda但我不知道怎么做?
非常感谢您的帮助 - 空列表中没有列表
- 否则,如果
- 它的第一个元素是列表
- 或者如果在列表的其余部分中有一个列表
- 空列表中没有列表
- 否则,如果
- 它的第一个元素是列表
- 或者如果在列表的其余部分中有一个列表
诀窍是将其转化为代码,特别仔细地思考如何表达最后一种情况:要做到这一点,您可能需要编写一个函数来确定列表中是否有列表。。。那么,您编写的函数是什么呢?热烈欢迎使用StackOverflow 我没有经过严格的测试,但也许这种方法可以帮助您:
(check-expect (contains-no-sublist? (list )) #true)
(check-expect (contains-no-sublist? (list "red" "green" "blue")) #true)
(check-expect (contains-no-sublist? (list (list "light red" "dark red") "green" "blue")) #false)
;; contains-no-sublist? checks if any element in the list is a list itself and returns #false, if it finds a list in the list (nested list).
(define contains-no-sublist? ;; define a function with the name "contains-no-sublist?"
(lambda [L] ;; define the function as a lambda expression over a given input list L
(cond ;; the function returns either #t or #f
[(empty? L) #true] ;; an empty list doesn't contain a sublist, so #t = #true can be returned
[(cons? L) ;; else still a list is given
(cond
[(list? (first L)) #false] ;; either the first element of the list is a list itself, then return false.
[else (contains-no-sublist? (rest L))] ;; or the first element is not a list itself, then check for the rest of the list if it contains any sublist
)
]
)
)
)
热烈欢迎来到这里 我没有经过严格的测试,但也许这种方法可以帮助您:
(check-expect (contains-no-sublist? (list )) #true)
(check-expect (contains-no-sublist? (list "red" "green" "blue")) #true)
(check-expect (contains-no-sublist? (list (list "light red" "dark red") "green" "blue")) #false)
;; contains-no-sublist? checks if any element in the list is a list itself and returns #false, if it finds a list in the list (nested list).
(define contains-no-sublist? ;; define a function with the name "contains-no-sublist?"
(lambda [L] ;; define the function as a lambda expression over a given input list L
(cond ;; the function returns either #t or #f
[(empty? L) #true] ;; an empty list doesn't contain a sublist, so #t = #true can be returned
[(cons? L) ;; else still a list is given
(cond
[(list? (first L)) #false] ;; either the first element of the list is a list itself, then return false.
[else (contains-no-sublist? (rest L))] ;; or the first element is not a list itself, then check for the rest of the list if it contains any sublist
)
]
)
)
)
使用cond
#lang racket
(define (no-list-inside?-by-cond ls)
(cond
[(empty? ls) #t]
[(list? (first ls))
#f]
[else
(no-list-inside?-by-cond (rest ls))]))
;;; TEST
(no-list-inside?-by-cond '(1 2 3)) ; should be #t
(no-list-inside?-by-cond '(1 2 3 '(3) 4)) ; should be #f
(no-list-inside?-by-cond '(1 2 3 '() 5)) ; should be #f
使用和map
#lang racket
(define (no-list-inside?-by-andmap ls)
(andmap (lambda (x) (not (list? x))) ls))
;;; TEST
(no-list-inside?-by-andmap '(1 2 3 2)) ; should be #t
(no-list-inside?-by-andmap '(1 2 3 '(3) 4)) ; should be #f
(no-list-inside?-by-andmap '(1 2 3 '() 5)) ; should be #f
使用过滤器
#lang racket
(define (no-list-inside?-by-filter lst)
(empty? (filter list? lst)))
;;; TEST
(no-list-inside?-by-filter '(1 2 3)) ; should be #t
(no-list-inside?-by-filter '(1 2 3 '(3) 4)) ; should be #f
(no-list-inside?-by-filter '(1 2 3 '() 5)) ; should be #f
使用cond
#lang racket
(define (no-list-inside?-by-cond ls)
(cond
[(empty? ls) #t]
[(list? (first ls))
#f]
[else
(no-list-inside?-by-cond (rest ls))]))
;;; TEST
(no-list-inside?-by-cond '(1 2 3)) ; should be #t
(no-list-inside?-by-cond '(1 2 3 '(3) 4)) ; should be #f
(no-list-inside?-by-cond '(1 2 3 '() 5)) ; should be #f
使用和map
#lang racket
(define (no-list-inside?-by-andmap ls)
(andmap (lambda (x) (not (list? x))) ls))
;;; TEST
(no-list-inside?-by-andmap '(1 2 3 2)) ; should be #t
(no-list-inside?-by-andmap '(1 2 3 '(3) 4)) ; should be #f
(no-list-inside?-by-andmap '(1 2 3 '() 5)) ; should be #f
使用过滤器
#lang racket
(define (no-list-inside?-by-filter lst)
(empty? (filter list? lst)))
;;; TEST
(no-list-inside?-by-filter '(1 2 3)) ; should be #t
(no-list-inside?-by-filter '(1 2 3 '(3) 4)) ; should be #f
(no-list-inside?-by-filter '(1 2 3 '() 5)) ; should be #f
亲爱的Tjorcht:对我来说,这似乎不是一个完全正确的解决方案,因为它不符合以下条件:“当列表中有一个列表时,它应该给出false。”您的函数的作用正好相反。但仍然是所有正确的方法…@BenJordan谢谢你提醒我。我更新了我的答案。亲爱的Tjorcht:对我来说,这似乎不是一个完全正确的解决方案,因为它不符合以下条件:“当列表中有一个列表时,它应该给出false。”你的函数正好相反。但仍然是所有正确的方法…@BenJordan谢谢你提醒我。我更新我的答案。