enter code hereJust need to know the basics of peeking in scheme. I tried looking at the racket site for help but it didn’t have much on it. Or maybe I was looking in the wrong section. Anyways, the point of this is the following.
if I have
(#\x #\b #\o #\x #\space #\3 #\6 #\0)
I want to be able to identify x as a char and then continue peeking until space is reached and redefine this as a word. Then do the same for 360.
Any tips?
Please and thank you! : )
Here is what my code looks like in case it helps
(define (work x)
(cond
((null? x)(write '$$))
(char-numeric? (car x))
(write 'Num)
(toke (cdr x)))
((char-alphabetic? (car x))
(write 'ID)
(work (cdr x)))
(else (write "other")))
The problem with this is that it would give me IDIDIDID for “xbox” (which makes sense cause of the code) but I want to make it output just ID once for the whole word xbox
There are simpler ways to solve the problem, but they involve some extra knowledge of the language. For instance, using regular expressions for splitting a string at the spaces and
mapandfilterfor processing each word:Notice that the input received was a string containing a line in the input file (as returned by the procedure
file->lines). The general idea would be: read the file line by line, and process each one in turn with the above snippet of code.If you’re ok with using functionality a bit more advanced in your code, the above will do the trick.
EDIT :
I wrote a version using only list iteration and
read-char(notpeek-char, which reads only the first char and doesn’t advance to the next one), but you’ll see that this is far more complex to understand than the above procedure:Both solutions work as expected for the following tests: