1

As input data I have a file containing 100 lines. On each line I have a number followed by dot. I need to print into another file the sorted numbers increasingly.

I know reading from a file and printing into a file with see and tell but when it comes to going through all of them from 1 to 100, without doing a read(X),read(Y) etc. for each line in the see() I am lost. So how would you approach this problem?

false
  • 10,264
  • 13
  • 101
  • 209
Max Bummer
  • 59
  • 9

1 Answers1

0

Please note that this description it's far more verbose than the actual code required.

?- read_input(L), msort(L, S), write_output(S).

I.e. (using Edinburgh-style I/O) see the input file, keep reading, storing in a list til the input = end_of_file (then close the list), after that use msort to order the list, then tell the output file, and with a loop write each element of the list. You can check if actually there are 100 numbers using length/2.

edit untested code to read a list from current input

read_list(L) :-
  read(N), N \= end_of_file
  -> L = [N|Ns], !, read_list(Ns)
  ;  L = []
  .
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • But how do you go through the list? So far in my programs I read line by line, such as {read(X), read(Y)} this bit reading the first two lines. So for the 100 numbers in the file it would be a lot. How do you put them in a list? – Max Bummer Nov 21 '12 at 09:48
  • It works, though what is the -> ? I used: see('C://Documents and Settings//numbers.txt'),read(L),read_list(L),msort(L,X),seen, tell('C://Documents and Settings//numbout.txt'),told. – Max Bummer Nov 21 '12 at 10:12
  • it's the if ... then .. else construct. See the [control predicates](http://www.swi-prolog.org/pldoc/doc_for?object=section%282,%274.8%27,swi%28%27/doc/Manual/control.html%27%29%29) – CapelliC Nov 21 '12 at 10:15
  • I'd have another one that I could need help on. http://stackoverflow.com/questions/13490900/delete-vowels-in-a-list-prolog – Max Bummer Nov 21 '12 at 10:29