Scheme 计划车辆通过错误类型?
我的任务是制作一个scheme函数,它包含两个文件,一个“mainfile”和一个“wordfile”。然后,该函数统计主文件中的行数、字数和字符数。单词由换行符/制表符/空格分隔。该函数还必须使用wordfile(包含一组单词)并计算在mainfile中找到wordfile中的单词的时间。然后打印出行数、字数和字符数。。。然后是按从大到小排序的wordfile。这是一个示例输入/输出,说明它应该做什么:Scheme 计划车辆通过错误类型?,scheme,Scheme,我的任务是制作一个scheme函数,它包含两个文件,一个“mainfile”和一个“wordfile”。然后,该函数统计主文件中的行数、字数和字符数。单词由换行符/制表符/空格分隔。该函数还必须使用wordfile(包含一组单词)并计算在mainfile中找到wordfile中的单词的时间。然后打印出行数、字数和字符数。。。然后是按从大到小排序的wordfile。这是一个示例输入/输出,说明它应该做什么: 1 ]=> (load "ws") ;Loading "ws.scm"... don
1 ]=> (load "ws")
;Loading "ws.scm"... done
;Value: filestatistics
1 ]=> (filestatistics "aa" "word0")
8 10 51
Word usage:
aaaa 4
1234 3
;Value: ()
这是我当前的代码。它还没有排序功能,还包括一些额外的显示
s以进行调试:
(define wc
(lambda (srcf l w c)
(if (eof-object? (peek-char srcf))
(begin ;; end of file
(close-port srcf)
(display l)(display " ")
(display w)(display " ")(display c))
(case (read-char srcf) ;; find a word?
((#\space #\tab) (wc srcf l w (+ c 1)))
((#\newline) (wc srcf (+ l 1) w (+ c 1))) ;; new line, increment
(else ;; found a word
(let loop ((i 2)) ;; eat word, 2 for both read-chars
(if (eof-object? (peek-char srcf))
(wc srcf l (+ w 1) (+ c 1))
(case (read-char srcf) ;; end of word?
((#\space #\tab) (wc srcf l (+ w 1) (+ c i)))
((#\newline) (wc srcf (+ l 1) (+ w 1) (+ c i)))
(else (loop (+ i 1)))))))))))
(define countw
(lambda (mfile cfile)
(procw mfile '() (mklist cfile '() '()))))
(define procw
(lambda (file word wset)
(if (eof-object? (peek-char file))
(if (null? word)
(for-each (lambda (w n)
(display w)(display " ")(display n)(newline)))
(procw file '() (addword word wset '())))
(case (peek-char file)
((#\space #\tab #\newline)
(read-char file)
(display ".")
(if (null? word)
(procw file word wset)
(procw file '() (addword word wset '()))))
(else
(display "?")
(if (null? word)
(procw file (string (read-char file)) wset)
(procw file (string-append word (string (read-char file))) wset)))))))
(define addword ;; returns new list
(lambda (word wset nset)
(if (null? wset)
nset
(if (eq? (car (car wset)) word) ;; comparing to front of list in list
(begin
(display "|")
(append nset (append ((car(car(wset))) (+ (cadr(car(wset))) 1)) (cdr wset))))
(addword word (cdr wset) (append nset (car wset)))))))
(define mklist ;; make word list
(lambda (file word wset)
(if (eof-object? (peek-char file))
(begin
(close-port file)
(if (null? word)
wset
(append wset '((word 0)))))
(case (peek-char file)
((#\space #\tab #\newline)
(read-char file)
(if (eq? word '())
(mklist file word wset)
(mklist file '() (append wset '((word 0))))))
(else
(if (eq? word '())
(mklist file (string (read-char file)) wset)
(mklist file (string-append word (string (read-char file))) wset)))))))
(define filestatistics
(lambda (src1 src2)
(begin
(wc (open-input-file src1) 0 0 0)(newline)(newline)
(countw (open-input-file src1) (open-input-file src2)))))
这是我为它得到的输出:
1 ]=> (load "ws")
;Loading "ws.scm"... done
;Value: filestatistics
1 ]=> (filestatistics "aa" "word0")
8 10 51
????.????.
;The object word, passed as the first argument to car, is not the correct type.
;To continue, call RESTART with an option number:
; (RESTART 2) => Specify an argument to use in its place.
; (RESTART 1) => Return to read-eval-print level 1.
我不明白为什么我会犯这个错误。因为word
甚至应该是car
的第一个参数。同样奇怪的是,代码在出现错误之前一直运行到第二个字,并且正在运行的第一个字没有显示“|”(在addword
中的调试输出)
我忘了添加输入文件,它们是:
aa:
字0:
aaaa 1234
更新1
因此,我将(eq?(car(car-wset))word)
更改为(string=?(car(car-wset))word)
,因为我正在比较字符串,这是我的新错误
;The object word, passed as an argument to string=?, is not a string.
不过,word应该是字符串。此外,在出现错误之前,调试输出是?。
更新2
所以我发现了我真正的问题。我用
((单词0))
制作了一个列表(成对),它只是制作了成对的word
。但是当我把它改成(cons(单词0))
我收到一个错误,aaaa
不适用。有没有办法制作一个字符串和数字对的列表?像散列一样?对不起,我对scheme很陌生。这一行看起来很可疑:
(append nset (append ((car(car(wset))) (+ (cadr(car(wset))) 1)) (cdr wset))))
首先,append
是非破坏性的。它不会更改nset
的值或内容,它只是返回一个新列表,其中包含所附加的内容。您似乎不会对append
的返回值做任何操作,这意味着它是禁止操作的
其次,
((car(car(wset)))
向我暗示你在考虑其他语言的语法,你可能是指(car(car(car wset))
(括号在Scheme中很重要。你不能有多于或少于所需的值。)你的(cadr(car(wset))
,我认为你的意思是(cadr>)(car wset))
如果单词与(car(car wset))匹配,append
实际上是addword
调用的返回值。但是感谢您指出括号。这是该行的新版本:(append nset(append(list(car(car wset))(+(cadr(car wset))1)(cdr wset)))
如果word设置为'()
,我是否应该设置为其他值?addword
在我的文件中仅在3个实例中调用。两个实例是(如果(null?word)
为false,并且在addword
中递归一次。因此,当它等于时,它不应该调用()
.Re update 2:当然,您可以通过两种方式来实现:1.(列表(cons word 0))
.2.`(,word.0))
。谢谢!这很有效。现在我需要弄清楚如何获取值。当我尝试使用(display(cadr It))显示它时,它会抛出这个值
来自对于每个函数:;作为参数传递给safe car的对象0不是一对。
只需使用cdr
而不是cadr
。
(append nset (append ((car(car(wset))) (+ (cadr(car(wset))) 1)) (cdr wset))))