0
101 Dec 07, 2004 at 21:36

hi all,
if i have a file called ‘testdata.txt’ containing these numbers in the following format:
‘ 0.1 2.2 4.3 9.6 5.0’ for example
how do i create a prolog program to copy the data from the text file ‘testdata.txt’ into a list called ‘NewList’ in my prolog program???

many thanks

#### 8 Replies

0
101 Dec 08, 2004 at 00:48

Sounds like homework to me.

Try reading your book. The idea of the homework is to have you figure it out, not to have other people do it for you.

0
101 Dec 08, 2004 at 08:15

actually im at uni and we dont get homework! ive been looking through a book and i cant find anything that inputs integers only chars!
im not lazy! its to help me read in data so i can apply my algorithm which i coded!

0
101 Dec 08, 2004 at 08:27

I dont know prolog at all, but if it can only read in characters, you can always perform the string parsing manually.

Read it all in, and then split it based on whitespace.

start with 0. For each digit convert it to an integer, multiply your number by 10 and add the digit. If you find a period, mark this point and count the number of digits you find after that. Then divide by 10\^n where n is that number of digits past the decimal.

0
101 Dec 09, 2004 at 10:23

its ok thanks anyway i worked out how to do it on integers but it doesnt work on decimal numbers!

0
101 Dec 09, 2004 at 11:01

Well, my prolog knowledge has rusted long ago, (and there are too many prolog dialects), but it should look like this:

mknum(input,output):-mknum(input,output,0)
mknum([],_,_).
mknum([remaining|”.”],output,power):-mknum(remaining, output*(1/(10**power*power)),0)

 mknum([remaining digit],output,power):-mknum(remaining,output+getnumberfromdigit(digit)*(10**power),power+1)

Basically a NomadRock idea, but in prolog it’s easier to parse from right to left.

EDIT: Code changed. (0 instead of 1 and _,_ instead of output,_)
EDIT2: Corrected stupid mistake (0*0=0) x**y means xy, if there’s no this operation wtrite a trivial predicate to implement it
pow(x,y):-pow(x,y,x)
pow(x,0,_):-pow(1,_,_)
pow(1,_,_).
pow(x,y,r):-pow(r*x,y-1,r)

0
101 Dec 10, 2004 at 15:19

thank you very much!

much appreciated! :)

0
101 Dec 10, 2004 at 19:32

:blush: This code won’t work at all! (I forgot that exsactly head and tail of a list are.) Shame on me… Damn, I really need to refresh that logic programming stuff…

I’ll try to make a working code, seems that I need to reprove thar “A” for logic progr…

0
101 Dec 10, 2004 at 21:46
:- discontiguous(ltof/3).
%digit from string
dfn("9",R) :- R is 9.
dfn("8",R) :- R is 8.
dfn("7",R) :- R is 7.
dfn("6",R) :- R is 6.
dfn("5",R) :- R is 5.
dfn("4",R) :- R is 4.
dfn("3",R) :- R is 3.
dfn("2",R) :- R is 2.
dfn("1",R) :- R is 1.
dfn("0",R) :- R is 0.
%list to float use ltof/2
ltof([],R,X) :- R is X,!.
ltof(L,R) :- ltof(L,R,0).
ltof([H|T],R,P) :- dfn(H,C), Q is P*10, F is Q+C,ltof(T,R,F).
ltof(["."|T],R,P) :- fltof(T,R,P,0.1).
%fraction list to float. Is called by ltof only
fltof([],R,X,_) :- R is X,!.
fltof([H|T],R,P,M) :- dfn(H,C), Q is C*M, Z is P+Q,N is M/10, fltof(T,R,Z,N).


This worked in GNU prolog like this:

GNU Prolog 1.2.16
By Daniel Diaz
| ?- [test].
compiling H:\GNU-Prolog\bin\test.pl for byte code...
H:\GNU-Prolog\bin\test.pl compiled, 26 lines read - 4270 bytes written, 20 ms

(10 ms) yes
| ?- ltof(["1","2","3",".","2","5","5"],R).

R = 123.255 ? a

no
| ?- ltof(["1","2","3",".","2","5","5"],R).

R = 123.255 ?

yes
| ?-


so, it needs more “!”’s for optimization, but i’m too lazy and too busy. :)