简体   繁体   中英

racket/scheme list manipulation

I wish to extract certain elements from a list of strings, starting from the string "ENTITIES" and ending at the string "OBJECTS"

So far, i have this to give me elements from the string ENTITIES onwards, but no way to remove elements from the string "OBJECTS" onwards.

(define (test-string2 lst keyword)
  (member (string-upcase keyword) lst))

I have also looked at the for function, and this is my attempt but obviously does not work

(define (test-string2 lst keyword)
  (cdr (member (string-upcase keyword) lst)))
;return list that matches keyword

(define (test-string3 lst keyword)
  (if (string=? (car lst) (string-upcase keyword))
      '()
      (begin (car lst)
             (test-string3 (cdr lst) keyword))))
;return elements in list until keyword is matched

(test-string3 (test-string2 list-of-strings "entities") "objects")




> list-of-strings
'("SECTION"
  "  2"
  "ENTITIES"
  "  0"
  "SPLINE"
  "  5"
  "F7"
  "330"
  "1F"
  "100"
  "AcDbEntity"
  "  8"
  "0"
  "  6"
  "Continuous"
  " 62"
  "     5"
  "370"
  "     0"
  "100"
  "AcDbSpline"
  "OBJECTS"
  "3"
  "5"
  "6"
  "6"
  "7"
  "78")

In Racket I'd use for/list :

(define (test-string3 lst keyword1 keyword2)
  (define kw2 (string-upcase keyword2))
  (for/list ((e (in-list (member (string-upcase keyword1) lst)))
             #:break (string=? e kw2))
    e))

then

> (test-string3 '("a" "b" "ENTITIES" "c" "d" "OBJECTS" "e" "f") "entities" "objects")
'("ENTITIES" "c" "d")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM