如何从scheme中的文件中删除给定的单个字符?
我有一个txt文件“blah.txt”,上面写着如何从scheme中的文件中删除给定的单个字符?,scheme,racket,Scheme,Racket,我有一个txt文件“blah.txt”,上面写着 word 3 + 6 现在考虑下面的代码: (define c (read-char file)) (define (toke c) (cond ((null? c)(write "empty")) ((equal? (read-char c) #\space)(write 'space) ; (toke (with the next character)
word 3 + 6
现在考虑下面的代码:
(define c (read-char file))
(define (toke c)
(cond
((null? c)(write "empty"))
((equal? (read-char c) #\space)(write 'space)
; (toke (with the next character)
)
((equal? (read-char c) #\+) (write '+)
; (toke (with the next character)
)
((equal? (read-char c) #\-) (write '-)
; (toke (with the next character)
)
((equal? (read-char c) #\*) (write '*)
; (toke (with the next character)
)
((equal? (read-char c) #\/) (write '/)
; (toke (with the next character)
)
((equal? (read-char c) #\=) (write '=)
; (toke (with the next character)
)
((equal? (read-char c) #\() (write 'LPAREN)
; (toke (with the next character)
)
((equal? (read-char c) #\)) (write 'RPAREN)
; (toke (with the next character)
)
((char-numeric? (read-char c)) (write 'Num)
; (toke (with the next character)
)
((char-alphabetic? (read-char c))(write 'ID)
; (toke (with the next character)
)
(else
(write "error"))))
我不希望输出到文本文件。我只是想继续演下一个角色。
我曾试图解决它,但没有成功。
也因为某些原因,我不断得到字符数字错误?那字母呢?告诉我我给它的是eof,而不是当我将文件另存为
3 a+r(例如)
我知道这涉及到把它变成一个字符串,但我一直得到一个字符串“单词3+6”,我不能这样做(车)。
让我进一步解释一下
我知道你能行
(定义列表)
文件->列表“blah.txt”)
其中将包含“(单词3+4)
但是接下来呢
(定义str)
文件->字符串“blah.txt”)
它将包含“word 3+4”
而且我不能做(汽车str)把“单词”拿出来,因为整个(汽车)是“单词3+4”
如果我不能正确解释我自己,请原谅,因为我是scheme的新手使用
(文件->行“blah.txt”)
读取文件内容,该文件将返回一个列表,文件的每一行都作为列表中的字符串
在那之后,操作每一行就很简单了;例如,使用过程
string->list
(请参阅)将一行转换为字符列表,并通过cdr
-ing在列表上迭代每个字符。我建议阅读一些关于如何在Scheme中实现扫描器的内容。
埃里克·希尔斯代尔和克里斯托弗·海恩斯写了一些非常好的笔记
首先,扫描仪:
然后是大局:
Racket(更一般地说,Scheme)提供了一种端口数据类型,允许您按顺序访问文件或流。您可以查看并从中读取单个字节
这里有一些参考Racket文档:
作为这种扫描器的一个例子,你可以看看我的关于制作一种基于拍子的语言的迷你教程
请注意,字符串本身不是端口,但我们可以创建一个源来自字符串的端口。Racket提供一个打开输入字符串
功能,将字符串转换为输入端口。因此,您应该能够这样做:
#lang racket
(define myport (open-input-string "hello"))
(printf "I see: ~s\n" (read-char myport))
(printf "I see: ~s\n" (read-char myport))
(printf "I see: ~s\n" (read-char myport))
(printf "I see: ~s\n" (read-char myport))
(printf "I see: ~s\n" (read-char myport))
来看看各个角色的表演。将
read char
更改为peek char
,您将看到peek char
不会将数据从端口中取出,而是允许您查看端口。@ColeJohnson Racket(Scheme),它在标记中。@scarLópez我从未听说过它。所以我不知道Lindsay Lohan对堆栈溢出没有多大帮助,是吗?:)@OscarLopez哈哈!你应该加上这句话作为回答,看看你获得了多少赞成票。奥斯卡,我知道你要我做什么,但我无法让它发挥作用。在过去的几天里,你或你的同学一次又一次地问同样的问题。点击scheme
标签并查看答案,答案就在那里,真的,你只需要在自己身上付出一点努力。