Skip to content

Instantly share code, notes, and snippets.

@tautologico
Created July 29, 2011 17:24
Show Gist options
  • Save tautologico/1114263 to your computer and use it in GitHub Desktop.
Save tautologico/1114263 to your computer and use it in GitHub Desktop.
Read a string from input and break it into a list of atoms.
/*
read_word_list(Words)
Le uma lista de palavras da entrada e coloca o resultado como atomos na
lista Words. A string lida da entrada deve terminar com um caractere de
ponto final '.'.
Baseado em exemplo do livro "The Art of Prolog" de Leon Sterling e Ehud Shapiro.
*/
read_word_list(Words) :- get_char(FirstChar), read_words(FirstChar, Words).
read_words(Char, [Word|Words]) :-
word_char(Char), read_word(Char, Word, NextChar), read_words(NextChar, Words).
read_words(Char, Words) :-
fill_char(Char), get_char(NextChar), read_words(NextChar, Words).
read_words(Char, []) :- end_of_words_char(Char).
read_word(Char, Word, NextChar) :-
word_chars(Char, Chars, NextChar), atom_codes(Word, Chars).
word_chars(Char, [Char|Chars], FinalChar) :-
word_char(Char), !, get_char(NextChar), word_chars(NextChar, Chars, FinalChar).
word_chars(Char, [], Char) :- \+word_char(Char).
word_char(Char) :- char_type(Char, alnum).
fill_char(Char) :- char_type(Char, white).
end_of_words_char('.').
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment