8 replies to this topic

### #1rego

Member

• Members
• 46 posts

Posted 07 December 2004 - 09:36 PM

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

Senior Member

• Members
• 785 posts

Posted 08 December 2004 - 12:48 AM

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.
Jesse Coyle

### #3rego

Member

• Members
• 46 posts

Posted 08 December 2004 - 08:15 AM

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!

Senior Member

• Members
• 785 posts

Posted 08 December 2004 - 08:27 AM

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.
Jesse Coyle

### #5rego

Member

• Members
• 46 posts

Posted 09 December 2004 - 10:23 AM

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

### #6Trident

New Member

• Members
• 25 posts

Posted 09 December 2004 - 11:01 AM

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)

### #7rego

Member

• Members
• 46 posts

Posted 10 December 2004 - 03:19 PM

thank you very much!

much appreciated! :)

### #8Trident

New Member

• Members
• 25 posts

Posted 10 December 2004 - 07:32 PM

: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...

### #9Trident

New Member

• Members
• 25 posts

Posted 10 December 2004 - 09:46 PM

:- 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. :)

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users