递增序言中的计数器
这是我正在处理的问题的当前代码。它应该从文件中读取,并每次遇到元音时,将计数器(r)添加。 目前,我在到达元音时停止了,但是我希望它增加计数器,然后继续处理。完成后,我希望它将R打印到控制台。提前致谢!
readWord(InStream, W) :-
get0(InStream,Char),
checkChar_readRest(Char,Chars,InStream, R),
atom_codes(Code,Chars),
write(Code).
%checkChar_readRest(10,[],_) :- !. % Return
%checkChar_readRest(32,[],_) :- !. % Space
checkChar_readRest(-1,[],_,_) :- !. % End of Stream
checkChar_readRest(97,[],_,R) :- !. % a
checkChar_readRest(101,[],_,R) :- !. % e
checkChar_readRest(105,[],_,R) :- !. % i
checkChar_readRest(111,[],_,R) :- incr(R,R1), write(R1). % o
checkChar_readRest(117,[],_,R) :- !. % u
%checkChar_readRest(end_of_file,[],_,_) :- !.
checkChar_readRest(Char,[Char|Chars],InStream,R) :-
get0(InStream,NextChar),
checkChar_readRest(NextChar,Chars,InStream,R).
incr(X, X1) :- X1 is X+1.
vowel(InStream, R) :-
open(InStream, read, In),
repeat,
readWord(In, W),
close(In).
This is the current code I have for a problem I am working on. It is supposed to read in from a file, and increment a counter, R, every time it comes across a vowel.
Currently, I have it stop when reaching a vowel, but I would like it to increment a counter, then continue processing. Once done, I want it to print R to the console. Thanks in advance!
readWord(InStream, W) :-
get0(InStream,Char),
checkChar_readRest(Char,Chars,InStream, R),
atom_codes(Code,Chars),
write(Code).
%checkChar_readRest(10,[],_) :- !. % Return
%checkChar_readRest(32,[],_) :- !. % Space
checkChar_readRest(-1,[],_,_) :- !. % End of Stream
checkChar_readRest(97,[],_,R) :- !. % a
checkChar_readRest(101,[],_,R) :- !. % e
checkChar_readRest(105,[],_,R) :- !. % i
checkChar_readRest(111,[],_,R) :- incr(R,R1), write(R1). % o
checkChar_readRest(117,[],_,R) :- !. % u
%checkChar_readRest(end_of_file,[],_,_) :- !.
checkChar_readRest(Char,[Char|Chars],InStream,R) :-
get0(InStream,NextChar),
checkChar_readRest(NextChar,Chars,InStream,R).
incr(X, X1) :- X1 is X+1.
vowel(InStream, R) :-
open(InStream, read, In),
repeat,
readWord(In, W),
close(In).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的尝试(ISO谓词):
最大的区别是我使用两个变量来跟踪计数。
count
(等效于您的r
)是当前计数,total
是代表最终计数的变量。我们将与count
统一在我们完成计数时:在流的末尾。在发布的原始程序中,有许多Singleton变量(变量永远不会与任何内容统一,例如
w
)。这通常表示错误,并会引起警告。还记得Prolog是一种逻辑语言,退后一步并认为“我实际上试图用这些变量来做些什么?”。它还可以帮助将问题分解为较小的块,而不是试图编写一个可以完成所有操作的谓词。Here's my attempt (ISO predicates):
The biggest difference is that I use two variables for keeping track of the count.
Count
(equivalent to yourR
) is the current count, andTotal
is a variable representing the final count. We unifyTotal
withCount
when we are finished counting: at the end of the stream.In the original program posted, there were many singleton variables (variables that are never unified with anything, for example
W
). This is usually indicative of a bug and will generate warnings. Remember that Prolog is a logical language, it can be good to take a step back and think "what am I actually trying to do with these variables?". It can also help to break the problem down into smaller chunks instead of trying to write one predicate that does everything.我可能会这样处理:
类似的东西
应该足以使文字含糊。
然后,沿着这些界线:
I might approach it like this:
Something like
should suffice for slurping the text.
And then, something along these lines: