comp.lang.ada
 help / color / mirror / Atom feed
* RECURSION PROBLEM
@ 1997-10-28  0:00 Robert Byrne
  1997-11-04  0:00 ` Jon S Anthony
  0 siblings, 1 reply; 2+ messages in thread
From: Robert Byrne @ 1997-10-28  0:00 UTC (permalink / raw)



I need to use recursion to display all pairs (combinations) of
characters from a set of characters read from a file given on the
command line.  This is easy without recursion; the below program works
correctly.  I am thinking that the easiest way to modify this program
would be to change the inner for loop in the procedure
DISPLAY_COMBINATIONS to a recursive procedure.  Does anyone have any
idea how to go about doing this?  I would appreciate any help.  Thanks.

byrner@db.erau.edu
------------------------------------------------------------------------

with ADA.TEXT_IO, ADA.COMMAND_LINE;
use ADA.TEXT_IO;

procedure PROG12 is 

  INPUT_STRING: FILE_TYPE;    --file to read from
  LENGTH: NATURAL:= 0;        --length of string containing all
characters
  TEMP_LENGTH: NATURAL:= 0;   --length of string for each line
  LETTER: STRING(1..80);      --string containing all characters
  TEMP_LETTER: STRING(1..80); --string for each line
  N: INTEGER:= 1;             --counter for # of lines read
   

procedure DISPLAY_COMBINATIONS(LETTER: in STRING; LENGTH: in NATURAL)
is  
  
begin --DISPLAY_COMBINATIONS

for I in 1..(LENGTH-1) loop 
  for J in (I+1)..LENGTH loop
    PUT("("); PUT(LETTER(I)); PUT(", "); PUT(LETTER(J)); PUT(")");
    NEW_LINE;          
  end loop;  
end loop;

end DISPLAY_COMBINATIONS;


begin --PROG12
  
  --open file to read from
  OPEN(INPUT_STRING, IN_FILE, ADA.COMMAND_LINE.ARGUMENT(1));
    
  --input string from file INPUT_STRING
  while not END_OF_FILE(INPUT_STRING) loop       --check for end of
file    
    GET_LINE(INPUT_STRING, TEMP_LETTER, TEMP_LENGTH);  
    if TEMP_LENGTH /= 0 then                     --check for characters
in line
      LENGTH:= LENGTH + TEMP_LENGTH;
      if N = 1 then 
        LETTER(1..LENGTH):= TEMP_LETTER(1..LENGTH);
      else
        LETTER(1..LENGTH):= LETTER(1..LENGTH-TEMP_LENGTH) &
TEMP_LETTER(1..TEMP_LENGTH);
      end if;
      N:= N+1;
    end if;  
  end loop;
    
  --close input file  
  CLOSE(INPUT_STRING); 
    
  --determine and display all combinations of 2 characters
  NEW_LINE;
  DISPLAY_COMBINATIONS(LETTER, LENGTH);

end PROG12;
------------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: RECURSION PROBLEM
  1997-10-28  0:00 RECURSION PROBLEM Robert Byrne
@ 1997-11-04  0:00 ` Jon S Anthony
  0 siblings, 0 replies; 2+ messages in thread
From: Jon S Anthony @ 1997-11-04  0:00 UTC (permalink / raw)



Robert Byrne <byrner@db.erau.edu> writes:

> 
> I need to use recursion to display all pairs (combinations) of
> characters from a set of characters read from a file given on the

I note that you say _combinations_, not permutations.  The following
is a simple solution in Lisp.  Translate into Ada and you are done...

(defun combos (letters i)
  (labels ((a-set (cur letters next)
		  (if (= next (length letters)) nil
		    (cons (cons cur (aref letters next))
			  (a-set cur letters (1+ next))))))
	  (if (= i (length letters))
	      nil
	    (append
	     (a-set (aref letters i) letters (1+ i))
	     (combos letters (1+ i))))))

COMBOS
* (combos "abcdef12345" 0)

((#\a . #\b) (#\a . #\c) (#\a . #\d) (#\a . #\e) (#\a . #\f) (#\a . #\1)
 (#\a . #\2) (#\a . #\3) (#\a . #\4) (#\a . #\5) (#\b . #\c) (#\b . #\d)
 (#\b . #\e) (#\b . #\f) (#\b . #\1) (#\b . #\2) (#\b . #\3) (#\b . #\4)
 (#\b . #\5) (#\c . #\d) (#\c . #\e) (#\c . #\f) (#\c . #\1) (#\c . #\2)
 (#\c . #\3) (#\c . #\4) (#\c . #\5) (#\d . #\e) (#\d . #\f) (#\d . #\1)
 (#\d . #\2) (#\d . #\3) (#\d . #\4) (#\d . #\5) (#\e . #\f) (#\e . #\1)
 (#\e . #\2) (#\e . #\3) (#\e . #\4) (#\e . #\5) (#\f . #\1) (#\f . #\2)
 (#\f . #\3) (#\f . #\4) (#\f . #\5) (#\1 . #\2) (#\1 . #\3) (#\1 . #\4)
 (#\1 . #\5) (#\2 . #\3) (#\2 . #\4) (#\2 . #\5) (#\3 . #\4) (#\3 . #\5)
 (#\4 . #\5))
*

/Jon

-- 
Jon Anthony
Synquiry Technologies, Ltd., Belmont, MA 02178, 617.484.3383
"Nightmares - Ha!  The way my life's been going lately,
 Who'd notice?"  -- Londo Mollari




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1997-11-04  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-10-28  0:00 RECURSION PROBLEM Robert Byrne
1997-11-04  0:00 ` Jon S Anthony

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox