Scheme 在列表中循环
如何在scheme中循环此列表Scheme 在列表中循环,scheme,Scheme,如何在scheme中循环此列表 (define test-document '( ((h e l l o)) ((t h i s)(i s)(t e s t)) )) 我尝试的只是显示第一列。您拥有的是列表列表列表: (define test-document '(((h e l l o)) ((t h i s) (i s) (t e s t)))) 要在其元素上循环,必须
(define test-document '(
((h e l l o))
((t h i s)(i s)(t e s t))
))
我尝试的只是显示第一列。您拥有的是列表列表列表:
(define test-document '(((h e l l o)) ((t h i s) (i s) (t e s t))))
要在其元素上循环,必须创建循环中的循环。为此,我们可以使用map
和curry
如下:
(map (curry map (curry map
(compose string->symbol string-upcase symbol->string)))
test-document)
(define (curry func . args)
(lambda x (apply func (append args x))))
这将产生以下输出:
(((H E L L O)) ((T H I S) (I S) (T E S T)))
如果您的方案解释器没有内置的curry
功能,则可以按如下方式定义一个功能:
(map (curry map (curry map
(compose string->symbol string-upcase symbol->string)))
test-document)
(define (curry func . args)
(lambda x (apply func (append args x))))
希望这能有所帮助。您拥有的是一个列表列表:
(define test-document '(((h e l l o)) ((t h i s) (i s) (t e s t))))
要在其元素上循环,必须创建循环中的循环。为此,我们可以使用map
和curry
如下:
(map (curry map (curry map
(compose string->symbol string-upcase symbol->string)))
test-document)
(define (curry func . args)
(lambda x (apply func (append args x))))
这将产生以下输出:
(((H E L L O)) ((T H I S) (I S) (T E S T)))
如果您的方案解释器没有内置的curry
功能,则可以按如下方式定义一个功能:
(map (curry map (curry map
(compose string->symbol string-upcase symbol->string)))
test-document)
(define (curry func . args)
(lambda x (apply func (append args x))))
希望这能有所帮助。
汽车
和cdr
功能系列是您浏览列表的朋友。这里有一些例子
(define test-document '(
((h e l l o))
((t h i s)(i s)(t e s t))
))
(car test-document) ;; `((h e l l o))
(caar test-document) ;; `(h e l l o)
(cadr test-document) ;; `((t h i s) (i s) (t e s t))
(car (cadr test-document) ;; `(t h i s)
(cadr (cadr test-document) ;; `(i s)
(caddr (cadr test-document) ;; `(test )
定义一个遍历列表的函数,并为每个非列表项调用函数
(define (walk-list lst fun)
(if (not (list? lst))
(fun lst)
(if (not (null? lst))
(begin
(walk-list (car lst) fun)
(walk-list (cdr lst) fun)))))
调用它来打印每个项目
(walk-list test-document print)
car
和cdr
功能系列是您浏览列表的朋友。这里有一些例子
(define test-document '(
((h e l l o))
((t h i s)(i s)(t e s t))
))
(car test-document) ;; `((h e l l o))
(caar test-document) ;; `(h e l l o)
(cadr test-document) ;; `((t h i s) (i s) (t e s t))
(car (cadr test-document) ;; `(t h i s)
(cadr (cadr test-document) ;; `(i s)
(caddr (cadr test-document) ;; `(test )
定义一个遍历列表的函数,并为每个非列表项调用函数
(define (walk-list lst fun)
(if (not (list? lst))
(fun lst)
(if (not (null? lst))
(begin
(walk-list (car lst) fun)
(walk-list (cdr lst) fun)))))
调用它来打印每个项目
(walk-list test-document print)
你在想这样的事吗
(define (walk-list lst)
(define (sub-walk lst)
(if (null? lst)
'()
(let ((x (car lst)))
(if (list? x)
(cons (sub-walk x) (sub-walk (cdr lst)))
(apply string-append (map symbol->string lst))))))
(flatten (sub-walk lst)))
然后
您可以使用常见的可疑对象(map
,filter
,…)对其进行处理
如果您的方案没有展平
过程,您可以使用此过程:
(define (flatten lst)
(reverse
(let loop ((lst lst) (res null))
(if (null? lst)
res
(let ((c (car lst)))
(loop (cdr lst) (if (pair? c) (loop c res) (cons c res))))))))
你在想这样的事吗
(define (walk-list lst)
(define (sub-walk lst)
(if (null? lst)
'()
(let ((x (car lst)))
(if (list? x)
(cons (sub-walk x) (sub-walk (cdr lst)))
(apply string-append (map symbol->string lst))))))
(flatten (sub-walk lst)))
然后
您可以使用常见的可疑对象(map
,filter
,…)对其进行处理
如果您的方案没有展平
过程,您可以使用此过程:
(define (flatten lst)
(reverse
(let loop ((lst lst) (res null))
(if (null? lst)
res
(let ((c (car lst)))
(loop (cdr lst) (if (pair? c) (loop c res) (cons c res))))))))
“循环此列表”是什么意思?能够按顺序访问列表中的每个单词。你说的“循环此列表”是什么意思?能够按顺序访问列表中的每个单词。它说curry未定义。你在使用哪个方案解释器?
mzscheme
中提供了curry
功能。无论如何,我用curry
的实现更新了我的答案。它说curry是未定义的。你在用哪个方案解释器?mzscheme
中提供了curry
功能。无论如何,我用curry
的实现更新了我的答案。但是如果您不知道文档的大小,该怎么办。@PhilipRego我在我的答案中添加了一些内容,以说明如何遍历列表并对其中的项目进行处理。我如何让它打印单词而不进行重复。它将单词作为一个列表来获取,但我不能用它做任何事情,因为它周围有哑的paretes(define(walk list lst fun)(如果(not)(equal)(car lst)())(fun lst)(如果(not)(null?lst))(begin(walk list(car lst)fun)(walk list(cdr lst)fun))(walk list测试文档显示)但是,如果您不知道文档的大小,该怎么办呢?@PhilipRego我在回答中添加了更多的内容,以说明如何遍历列表并对其中的项目进行处理。我如何让它打印单词而不进行重复。它将单词作为一个列表来获取,但我不能用它做任何事情,因为它周围有哑的paretes(define(walk list lst fun)(如果(not)(equal)(car lst)())(fun lst)(如果(not)(null?lst))(begin(walk list(car lst)fun)(walk list(cdr lst)fun))(walk list测试文档显示)我没有这个功能,所以我在答案中加了一个!我没有这个功能,所以我在答案中加了一个!