Home > Archive > Prolog > September 2007 > Quoting singleton variables
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 |
Quoting singleton variables
|
|
| andrea 2007-09-02, 7:13 pm |
| I have in one file stuff like
Como.Mantova.
cities divided by a dot.
Now I parse them with
get_word(Temp, Word) :-
get_char(C),
(C == '.',
reverse(Temp,Word);
(C == end_of_file,
Word=C);
(C \== '.',
get_word([C | Temp],Word))).
And I get
?- get_word([],W).
|: Milano.
W = ['M', i, l, a, n, o]
Now I need to merge the word and write to output to another file, but
in this process 'Milano' becomes a singleton variable...
out_cities(Cities) :-
maplist(atom_codes,Y,Cities), term_to_atom(Y,T), %% trasformo liste
di caratteri in atomi
string_concat(T,').',Z), string_concat('cities(',Z,Ris),
writeln(Ris).
?- out_cities([['M',i,l,a,n,o],['C',o,m,o]]
).
cities(['Milano', 'Como']).
Yes
Which should be fine, but if I consult that file gives me error being
'Milano' a singleton variable, so how should I fix that??
Thanks
| |
| Pierpaolo BERNARDI 2007-09-02, 7:13 pm |
| On Sun, 02 Sep 2007 23:47:45 +0200, andrea <kerny404@gmail.com> wrote:
> I have in one file stuff like
> Como.Mantova.
>
> cities divided by a dot.
> Now I parse them with
> get_word(Temp, Word) :-
> get_char(C),
> (C == '.',
> reverse(Temp,Word);
> (C == end_of_file,
> Word=C);
> (C \== '.',
> get_word([C | Temp],Word))).
che formattato umanamente diventa:
get_word(Temp, Word) :-
get_char(C),
( C == '.',
reverse(Temp, Word)
; C == end_of_file,
Word = C
; C \== '.',
get_word([C|Temp], Word)
).
or better:
get_word(Temp, Word) :-
get_char(C),
( C == '.'
-> reverse(Temp, Word)
; C == end_of_file
-> Word = C
; get_word([C|Temp], Word)
).
> Now I need to merge the word and write to output to another file, but
> in this process 'Milano' becomes a singleton variable...
>
> out_cities(Cities) :-
> maplist(atom_codes,Y,Cities), term_to_atom(Y,T), %% trasformo liste
> di caratteri in atomi
> string_concat(T,').',Z), string_concat('cities(',Z,Ris),
> writeln(Ris).
>
> ?- out_cities([['M',i,l,a,n,o],['C',o,m,o]]
).
> cities(['Milano', 'Como']).
Why not simply:
out_cities(Cities) :-
writeq(cities(Cities)),
writeln('.\n').
Or:
out_cities(Cities) :-
format('cities(~q)~n', [Cities]).
> Which should be fine, but if I consult that file gives me error being
> 'Milano' a singleton variable, so how should I fix that??
That's strange. You should post a complete transcript.
P.
| |
| andrea 2007-09-03, 8:08 am |
| On 3 Set, 00:44, "Pierpaolo BERNARDI" <pierpa...@secondbox.net> wrote:
> On Sun, 02 Sep 2007 23:47:45 +0200, andrea <kerny...@gmail.com> wrote:
>
>
> che formattato umanamente diventa:
>
> get_word(Temp, Word) :-
> get_char(C),
> ( C == '.',
> reverse(Temp, Word)
> ; C == end_of_file,
> Word = C
> ; C \== '.',
> get_word([C|Temp], Word)
> ).
>
> or better:
>
> get_word(Temp, Word) :-
> get_char(C),
> ( C == '.'
> -> reverse(Temp, Word)
> ; C == end_of_file
> -> Word = C
> ; get_word([C|Temp], Word)
> ).
>
>
>
>
> Why not simply:
>
> out_cities(Cities) :-
> writeq(cities(Cities)),
> writeln('.\n').
>
> Or:
>
> out_cities(Cities) :-
> format('cities(~q)~n', [Cities]).
>
>
> That's strange. You should post a complete transcript.
>
> P.
Well, this is the result of get_cities
cities(['Roma', 'Pavia', 'Milano', 'Bergamo', 'Mantova', 'Genova',
'Venezia', 'Napoli', 'Palermo', 'Pistoia', 'Firenze', 'Reggio',
'Varese', 'Montecatini', 'Pescia', 'Verona', 'Treviso']).
But if I do this
?- consult('dist_file').
Warning: (/Users/andrea/programmi/navigator/dist_file.pl:2):
Singleton variables: [Roma]
Warning: (/Users/andrea/programmi/navigator/dist_file.pl:3):
Singleton variables: [Pavia]
Warning: (/Users/andrea/programmi/navigator/dist_file.pl:4):
Singleton variables: [Milano]
Why doesn't it see my quotes??
| |
| Bart Demoen 2007-09-03, 8:08 am |
| andrea wrote:
> Well, this is the result of get_cities
> cities(['Roma', 'Pavia', 'Milano', 'Bergamo', 'Mantova', 'Genova',
> 'Venezia', 'Napoli', 'Palermo', 'Pistoia', 'Firenze', 'Reggio',
> 'Varese', 'Montecatini', 'Pescia', 'Verona', 'Treviso']).
>
> But if I do this
> ?- consult('dist_file').
> Warning: (/Users/andrea/programmi/navigator/dist_file.pl:2):
> Singleton variables: [Roma]
> Warning: (/Users/andrea/programmi/navigator/dist_file.pl:3):
> Singleton variables: [Pavia]
> Warning: (/Users/andrea/programmi/navigator/dist_file.pl:4):
> Singleton variables: [Milano]
>
>
> Why doesn't it see my quotes??
>
Is what you wrote really the literal contents of the file ?
Because Roma, Pavia and Milano are on the same line, and the message
says they are on lines 2,3,4.
Cheers
Bart Demoen
| |
| andrea 2007-09-03, 8:08 am |
| On 3 Set, 13:18, Bart Demoen <b...@cs.kuleuven.ac.be> wrote:
> andrea wrote:
>
>
>
> Is what you wrote really the literal contents of the file ?
> Because Roma, Pavia and Milano are on the same line, and the message
> says they are on lines 2,3,4.
>
> Cheers
>
> Bart Demoen
Mm right I'm an idiot :D
The problems is caused by these
idx(Roma,0).
idx(Pavia,1).
idx(Milano,2).
idx(Bergamo,3).
idx(Mantova,4).
Which I haven't fixed yet, thanks anyway...
|
|
|
|
|