For Programmers: Free Programming Magazines  


Home > Archive > Prolog > August 2007 > List of chars









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 List of chars
andrea

2007-08-21, 4:31 am

I have to parse a string like this
2.London.Paris.

2 indicates the number of the cities I will have.

get_cities(0,Temp,X) :- reverse(Temp,X).
get_cities(N,Temp,X) :-
N > 0, M is N-1, get_word([],C), get_cities(M,[C | Temp],X).
get_num(N) :-
get_word([],M), number_chars(N,M).
get_word(Temp, Word) :-
get_char(C),
( C == '.' ->
reverse(Temp,Word);
(C \== '.' ->
get_word([C | Temp],Word))).

Doing this in swipl
?- see('stradario. txt'),get_num(N),get_cities(N,[],C),seen
.
Seems working now, but I have a result like this
N = 10,
C = [['R', o, m, a], ['P', a, v, i, a], ['M', i, l, a, n, o], ['B', e,
r, g, a|...], ['M', a, n, t|...], ['G', e, n|...], ['V', e|...],
['N'|...], [...|...]|...]

Yes

Why is ['R',o,m,a]?? is only the 'R' considered as a character??
I read that a string is a list of chars, so why ['a','b'] == "ab"
fails??

Thanks a lot, any stylistic/logic suggestions are welcome of course...

andrea

2007-08-21, 4:31 am

Hum writing to a file and consulting it gives troubles

cities(X).

X = [[_G252, o, m, a], [_G267, a, v, i, a], [_G285, i, l, a, n, o],
[_G306, e, r, g, a|...], [_G285, a, n, t|...], [_G354, e, n|...],
[_G375, e|...], [_G399|...], [...|...]|...]

I suppose because upper letters are singleton variables, so how could
I manage it keeping the upper char?

Sponsored Links







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

Copyright 2008 codecomments.com