Given a decently long text, I need to find how many times a certain word appears into it. Like the sherlock novels, if I type in Sherlock, to give me 200 times or something similar.
So far I know how to read a list with this function I implemented, posted below. I appreciate all the help, don’t know what to do next or how.
read_list(L) :-
read(N), N \= end_of_file
-> L = [N|Ns], !, read_list(Ns)
; L = []
.
Thank you.
read/1 fetch a term followed by . but for sake of discussion let’s ignore this fact.
If you are just interested in word frequency, why building a list? just count the words and the matches, and at end of file compute the frequency:
test:
edit Instead of read/1, let’s define a read_word(W), where a word is simply a sequence of alphanumerics
equipped with such ugly code, and replaced read/1 with read_word/1, we get
Note that now I’m passing a string, not an atom.