Hello, I've been set the task to convert a string into a list of words within that string. As in, go through a string character by character, recording each character into a list, then when a space appears, break off that list and resume recording the next characters into a new list.
"Hello, world!" -> ("Hello" "world!")
I've been working on it for just over 2 days non-stop now, and everytime I try a solution, it just doesn't work. My main problem is trying to stop recording when I find a space, then start recording again. Since I've been working on it for so long, I have to much code to past all here, but here is what I think my most useful attempt is:
Note: I am trying to get this procedure to return a list of lists of characters, ie ( (#\I) (#\a #\m)). After I have this solved I can finish the rest by myself confidently.
(define [space? char] ; Check to see if a character is a space (equal? char #\space) ) (define [chars->list-of-chars char-list] ; If the list is empty, return a list with an empty list within it (if (empty? char-list) (list empty) ; otherwise if the first character in the list is a space (if (space? (first char-list)) empty ; return an empty list ; else add the first character within a list, to the rest of the lists of characters (Recurse) (append (cons (first char-list) empty) (first-word (rest char-list)))) ) )
As you can see, when it finds a space, it returns empty (an empty list), which stops the recording of characters into the first list. As a result, if the first character in a string is a space, it returns an empty list, otherwise it will just return the first word.
This is the last section of an assignment that I have to do, I have the entire program working correctly, but without this piece, none of my hardwork will show =[
It's due in approx 24 hrs, so I would appreciate speedy replies
Thanks in advance