In Dr. Racket, how would I convert a list containing strings (each string is separated by a space) to a list containing a list of the separated strings. If you can't understand the question too well, here is an example: Given a list like:
(list "abc def" "ghj elm" "ref tio" "pol mnb")
I want to produce:
(list (list "abc" "def") (list "ghj" "elm") (list "ref" "tio") (list "pol" "mnb"))
I wrote up a code for it but I quickly got stuck. Here is my code so far:
(define (a los)
(cond
[(empty? los) empty]
[(char-whitespace? (first los)) (substring (list->string los) 1)]
[else (a (rest los))]))
For example:
(a (string->list "abc def"))
Produces "def". I was hoping to first separate the strings in the list based on the whitespace and then I would piece them together to form new lists. However, I don't know how to do this. Any tips/hints on how to do this are much appreciated.
There's a built-in procedure called string-split
that does just what you need, it's as simple as this:
(define (split-list los)
(map string-split los))
It works as expected:
(split-list '("abc def" "ghj elm" "ref tio" "pol mnb"))
=> '(("abc" "def") ("ghj" "elm") ("ref" "tio") ("pol" "mnb"))
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.