我不知道你的數據結構是否真的適合您的需要,但在這裏我們去:
首先,我們將定義take-right-until
將分裂根據最右邊的子表到謂語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
:
(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))))))
測試:
> (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"))
你能否提供一個例子(輸入和預期輸出)? – 2014-12-07 12:23:30
當然,我在編輯中這麼做了! – KRC 2014-12-07 12:25:32
'keyword'有什麼意義?你似乎沒有在任何地方使用它。 – 2014-12-07 13:34:54