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.

Please log in or register to post a reply.

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.

im not lazy! its to help me read in data so i can apply my algorithm
which i coded!

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.

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

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)

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

```
:- 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
Copyright (C) 1999-2002 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. :)

- Upcoming Multiplatform Game Program...
- Our first game - looking for feedbacks
- Network Emulation Tool
- Trouble with accessing GLSL array
- Fiction
- Game Programming Patterns: Bytecode
- Interactive WebGL Water Demo
- Skeletal Animation Tutorial with GP...
- Unreal Engine 4
- Microsoft xbox one selling poorly

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