For Programmers: Free Programming Magazines  


Home > Archive > Prolog > March 2008 > Re: splitting strings (swi-prolog) (got it (fix))









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: splitting strings (swi-prolog) (got it (fix))
Dustin Kick

2008-03-03, 8:06 pm


I had to change one thing to make the munching work, this is functional
just the way I wanted:
split_string(String,Collected_strings):-
string_to_list(String,Charlist),
char_code(' ',Space),
collect_strings(Charlist,Space,Collected
_strings).

collect_strings([],_,[]):-!.
collect_strings(Charlist,Last,[String|Co
llected_strings]):-
collect_chars(Charlist,Nextlist,Last,Col
lected_chars),
string_to_list(String,Collected_chars),
collect_strings(Nextlist,Last,Collected_
strings).

collect_chars([],_,_,[]):-!.
collect_chars([32|Charlist],Charlist,Las
t,[]):-
Last\==32,!.
collect_chars([32|Charlist],Nextlist,32,
Collected_chars):- % <----I
wasn't passing the Nextlist through before. collect_chars(Charlist,Nextlist,32,Coll
ected_chars),!.
collect_chars([Code|Charlist],Nextlist,_
,[Code|Collected_chars]):-
Code\==32,
Last1=Code,
collect_chars(Charlist,Nextlist,Last1,Co
llected_chars),!.



Dustin Kick<mac_vieuxnez@mac.com> wrote:
>
>
>
>I forgot to put in the boundary condition for the collect_chars,
>thinking that the condition for collect_strings could do it by itself... or
>rather, not thinking of it at all. anyway, thanks for help rendered, this
>was my final solution, if anyone wants to see it. (or not)
>split_string(String,Collected_strings):-
> string_to_list(String,Charlist),
> char_code(' ',Space),
> collect_strings(Charlist,Space,Collecte
d_strings).
>
>collect_strings([],_,[]):-!.
> collect_strings(Charlist,Last,[String|Co
llected_strings]):-
> collect_chars(Charlist,Nextlist,Last,Co
llected_chars),
> string_to_list(String,Collected_chars),

> collect_strings(Nextlist,Last,Collected
_strings).
>
>collect_chars([],_,_,[]):-!.
> collect_chars([32|Charlist],Charlist,Las
t,[]):-
> Last\==32,!.
> collect_chars([32|Charlist],_,32,Collect
ed_chars):-
> collect_chars(Charlist,_,32,Collected_c
hars),!.
> collect_chars([Code|Charlist],Nextlist,_
,[Code|Collected_chars]):-
> Code\==32,
> Last1=Code,
> collect_chars(Charlist,Nextlist,Last1,C
ollected_chars),!.
>
>
>
>
>mac_vieuxnez@mac.com (Dustin Kick) wrote:
>
>
>
>--
>
>Dustin Kick
>http://homepage.mac.com/mac_vieuxnez
>




--

Dustin Kick
http://homepage.mac.com/mac_vieuxnez

Dustin Kick

2008-03-07, 7:15 pm


I just got around to testing your solution, and it works nicely, just as
you said it would, not that I doubted, but I don't understand the code,
yet. Is there a goal I can run DCGs through to see the expanded code?

Markus Triska <triska@logic.at> wrote:
>
>
>Dustin Kick<mac_vieuxnez@mac.com> writes:
>
>
>Consider DCGs for convenience - for example:
>
> string_tokens(Cs, Ts) :- phrase(tokens(Cs, []), Ts).
>
> tokens([], Ts) --> token(Ts).
> tokens([C|Cs], Ts) -->
> ( { C == 0' } -> token(Ts), tokens(Cs, [])
> ; tokens(Cs, [C|Ts])
> ).
>
> token([]) --> [].
> token([T|Ts]) --> { reverse([T|Ts], Token) }, [Token].
>
>Yielding:
>
> ?- string_tokens("this is a test ", ["this", "is", "a", "test"]).
> %@ true.
>
>--
>comp.lang.prolog FAQ: http://www.logic.at/prolog/faq/




--

Dustin Kick
http://homepage.mac.com/mac_vieuxnez

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com