Reading a user-input string in prolog -
i beginner in prolog. using swi prolog(just started using it) , need split user input string list. tried following code, error stating 'full stop in clause-body?cannot redefine ,/2'
write('enter string'),nl,read('i'). tokenize("",[]). tokenize(i,[h|t]):-fronttoken(i,h,x),tokenize(x,t). can me pls...
from error message, it's apparent you're using swi-prolog. can use library support:
?- read_line_to_codes(user_input,cs), atom_codes(a, cs), atomic_list_concat(l, ' ', a). |: hello world ! cs = [104, 101, 108, 108, 111, 32, 119, 111, 114|...], = 'hello world !', l = [hello, world, !]. to work more directly on 'strings' (really, string list of codes), i've built splitter, string//1
:- use_module(library(dcg/basics)). %% splitter(+sep, -chunks) % % split input on sep: minimal implementation % splitter(sep, [chunk|r]) --> string(chunk), ( sep -> !, splitter(sep, r) ; [], {r = []} ). being dcg, should called phrase/2
?- phrase(splitter(" ", strings), "hello world !"), maplist(atom_codes,atoms,strings). strings = [[72, 101, 108, 108, 111], [119, 111, 114, 108, 100], [33]], atoms = ['hello', world, !] .
Comments
Post a Comment