Scheme 计划/骗局:将列表拆分为列表列表
如果我有一个包含3个不同对象的列表,它们之间用全大写字符串分隔开,那么如何将它们绑定到一个包含3个列表的列表中 我只知道Scheme 计划/骗局:将列表拆分为列表列表,scheme,racket,Scheme,Racket,如果我有一个包含3个不同对象的列表,它们之间用全大写字符串分隔开,那么如何将它们绑定到一个包含3个列表的列表中 我只知道 #lang racket (define (test-string4 lst keyword) (let ((kw (string-upcase keyword))) (cond ((null? lst) '()) ((string-upper-case? (car lst)) (list 对于“(“多段线”2“3”
#lang racket
(define (test-string4 lst keyword)
(let ((kw (string-upcase keyword)))
(cond ((null? lst) '())
((string-upper-case? (car lst))
(list
对于“(“多段线”2“3”…“直线”2“3”…)的列表
它应该被分解成“(“多段线”“2”“3”“)(“线”“2”“3”“)”)这似乎是您想要的,尽管
字符串大写?
似乎没有在racket中定义
(define (splitter lst curr)
(cond ((null? lst) ; Put current "object" in a list
(cons curr '()))
((string-upper-case? (car lst)) ; Starting a new "object"
(let ((rslt (splitter (cdr lst) (list (car lst)))))
(if (null? curr)
rslt ; This is the only object
(cons curr rslt)))) ; Add last-finished object to front of result
(else ; Continue w/ current "object"
(splitter (cdr lst) (append curr (list (car lst)))))))
(define (test-string4 lst)
(splitter lst '()))
这似乎是你想要的,尽管
字符串大写?
在racket中似乎没有定义
(define (splitter lst curr)
(cond ((null? lst) ; Put current "object" in a list
(cons curr '()))
((string-upper-case? (car lst)) ; Starting a new "object"
(let ((rslt (splitter (cdr lst) (list (car lst)))))
(if (null? curr)
rslt ; This is the only object
(cons curr rslt)))) ; Add last-finished object to front of result
(else ; Continue w/ current "object"
(splitter (cdr lst) (append curr (list (car lst)))))))
(define (test-string4 lst)
(splitter lst '()))
假设已经定义了
字符串大写?
(例如,使用):
…我们可以使用from SRFI-1编写一个通用的、更简单的、可以说更惯用的实现,以将列表拆分为以给定条件开头的元素的子列表,在本例中为全大写字符串:
(require srfi/1)
(define (splitter pred? lst)
(if (empty? lst)
empty
(let-values ([(data tail) (break pred? (rest lst))])
(cons (cons (first lst) data)
(splitter pred? tail)))))
不管每个元素序列有多长,只要我们遵守关键字都是大写字符串的惯例,我们甚至不必传递关键字列表。例如:
(splitter string-upper-case?
'("POLYLINE" "2" "3" "4" "LINE" "2" "3" "TEST" "1"))
=> '(("POLYLINE" "2" "3" "4") ("LINE" "2" "3") ("TEST" "1"))
假设已经定义了
字符串大写?
(例如,使用):
…我们可以使用from SRFI-1编写一个通用的、更简单的、可以说更惯用的实现,以将列表拆分为以给定条件开头的元素的子列表,在本例中为全大写字符串:
(require srfi/1)
(define (splitter pred? lst)
(if (empty? lst)
empty
(let-values ([(data tail) (break pred? (rest lst))])
(cons (cons (first lst) data)
(splitter pred? tail)))))
不管每个元素序列有多长,只要我们遵守关键字都是大写字符串的惯例,我们甚至不必传递关键字列表。例如:
(splitter string-upper-case?
'("POLYLINE" "2" "3" "4" "LINE" "2" "3" "TEST" "1"))
=> '(("POLYLINE" "2" "3" "4") ("LINE" "2" "3") ("TEST" "1"))
我想知道您的数据结构是否真的适合您的需要,但现在我们开始: 首先,我们将定义
右转直到
,它将根据谓词f
拆分最右边的子列表:
(define (take-right-until lst f)
(let loop ((spl1 (reverse lst)) (spl2 null) (found #f))
(if (or found (null? spl1))
(values (reverse spl1) spl2)
(let ((c (car spl1)))
(loop (cdr spl1) (cons c spl2) (f c))))))
测试:
> (take-right-until '("POLYLINE" "2" "3" "LINE" "4" "5" ) (curryr member '("POLYLINE" "LINE")))
'("POLYLINE" "2" "3")
'("LINE" "4" "5")
> (take-right-until '("POLYLINE" "2" "3") (curryr member '("POLYLINE" "LINE")))
'()
'("POLYLINE" "2" "3")
> (test-string4 '("POLYLINE" "2" "3" "LINE" "4" "5" ) '("polyline" "line"))
'(("POLYLINE" "2" "3") ("LINE" "4" "5"))
> (test-string4 '("POLYLINE" "2" "3" "LINE" "4" "5" "SQUARE" "6" "7" "8") '("polyline" "square" "line"))
'(("POLYLINE" "2" "3") ("LINE" "4" "5") ("SQUARE" "6" "7" "8"))
然后test-string4
:
(define (test-string4 lst kwds)
(define kw (map string-upcase kwds))
(define f (curryr member kw))
(let loop ((lst lst) (res null))
(if (null? lst)
res
(let-values (((spl1 spl2) (take-right-until lst f)))
(loop spl1 (cons spl2 res))))))
测试:
> (take-right-until '("POLYLINE" "2" "3" "LINE" "4" "5" ) (curryr member '("POLYLINE" "LINE")))
'("POLYLINE" "2" "3")
'("LINE" "4" "5")
> (take-right-until '("POLYLINE" "2" "3") (curryr member '("POLYLINE" "LINE")))
'()
'("POLYLINE" "2" "3")
> (test-string4 '("POLYLINE" "2" "3" "LINE" "4" "5" ) '("polyline" "line"))
'(("POLYLINE" "2" "3") ("LINE" "4" "5"))
> (test-string4 '("POLYLINE" "2" "3" "LINE" "4" "5" "SQUARE" "6" "7" "8") '("polyline" "square" "line"))
'(("POLYLINE" "2" "3") ("LINE" "4" "5") ("SQUARE" "6" "7" "8"))
我想知道您的数据结构是否真的适合您的需要,但现在我们开始: 首先,我们将定义
右转直到
,它将根据谓词f
拆分最右边的子列表:
(define (take-right-until lst f)
(let loop ((spl1 (reverse lst)) (spl2 null) (found #f))
(if (or found (null? spl1))
(values (reverse spl1) spl2)
(let ((c (car spl1)))
(loop (cdr spl1) (cons c spl2) (f c))))))
测试:
> (take-right-until '("POLYLINE" "2" "3" "LINE" "4" "5" ) (curryr member '("POLYLINE" "LINE")))
'("POLYLINE" "2" "3")
'("LINE" "4" "5")
> (take-right-until '("POLYLINE" "2" "3") (curryr member '("POLYLINE" "LINE")))
'()
'("POLYLINE" "2" "3")
> (test-string4 '("POLYLINE" "2" "3" "LINE" "4" "5" ) '("polyline" "line"))
'(("POLYLINE" "2" "3") ("LINE" "4" "5"))
> (test-string4 '("POLYLINE" "2" "3" "LINE" "4" "5" "SQUARE" "6" "7" "8") '("polyline" "square" "line"))
'(("POLYLINE" "2" "3") ("LINE" "4" "5") ("SQUARE" "6" "7" "8"))
然后test-string4
:
(define (test-string4 lst kwds)
(define kw (map string-upcase kwds))
(define f (curryr member kw))
(let loop ((lst lst) (res null))
(if (null? lst)
res
(let-values (((spl1 spl2) (take-right-until lst f)))
(loop spl1 (cons spl2 res))))))
测试:
> (take-right-until '("POLYLINE" "2" "3" "LINE" "4" "5" ) (curryr member '("POLYLINE" "LINE")))
'("POLYLINE" "2" "3")
'("LINE" "4" "5")
> (take-right-until '("POLYLINE" "2" "3") (curryr member '("POLYLINE" "LINE")))
'()
'("POLYLINE" "2" "3")
> (test-string4 '("POLYLINE" "2" "3" "LINE" "4" "5" ) '("polyline" "line"))
'(("POLYLINE" "2" "3") ("LINE" "4" "5"))
> (test-string4 '("POLYLINE" "2" "3" "LINE" "4" "5" "SQUARE" "6" "7" "8") '("polyline" "square" "line"))
'(("POLYLINE" "2" "3") ("LINE" "4" "5") ("SQUARE" "6" "7" "8"))
你能提供一个例子(输入和预期输出)吗?当然,我已经在编辑中这样做了!
关键字的意义是什么?你似乎没有在任何地方使用它。你能提供一个例子(输入和预期输出)吗?当然,我已经在编辑中这样做了!关键字的意义是什么?您似乎没有在任何地方使用它。在方案中,约定使用else
,而不是#t
作为最后一个条件。在方案中,约定使用else
,而不是#t
作为最后一个条件。