🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.pascal.borland
10 messages
10 total messages Started by br-pede Thu, 18 May 2000 00:00
WHAT COULD BE WRONG?
#645
Author: br-pede
Date: Thu, 18 May 2000 00:00
47 lines
2215 bytes

I am trying to make a software, where the user can put in some word, and the
program will look and see if the word(s) is in a wordlist file. Everything
is working pretty well, except this: The procedure is running in a recursive
way, that if you want to look up more than one word, you only put it in next
time you are asked. To exit this, you only have to input an empty string,
that is to press the ENTER key. And here is the trouble. The program will
not stop this looping, unless you enter several empty words.

Here is the snip of the program that is causing trouble:

procedure lookup;
begin
    writeln;
    write('please enter word to be looked up: ');
    readln( s ); { s : STRING; }
    if s = '' then exit; { this doesn't work,
                           unless running a couple of
    assign(f,'wordlist.dat'); reset(f);
    repeat
         read(f,t); { t : STRING }
         if t = s then found := true   else found := false; {found : BOOLEAN}
    until eof(f);
    close(f); {f : FILE OF STRING}
    if not found then begin
         writeln('the word ', s, 'is not found in the list');
         lookup;
      end;{if}
    writeln('the word ', s, ' is found in the list');
end;{ procedure lookup}

Begin
    { call a menu}
    Repeat read(keychoice) until keychoice in [#0 .. #255];
    { keychoice : CHAR }
    if keychoice = '1' then lookup;
    { call the menu}
    ...
end.

Hope anybody out there could help. It will be greatly appriciated.

regards,
Brian
times}
Net-Tamer V 1.11 - Test Drive
Re: WHAT COULD BE WRONG?
#646
Author: Palinkas Tibor
Date: Thu, 18 May 2000 00:00
53 lines
2596 bytes
Use halt command instead of exit. Anyway not the nicest solution, but
should work :)

On Thu, 18 May 2000 br-pede@online.no wrote:

> 
> 
> I am trying to make a software, where the user can put in some word, and the
> program will look and see if the word(s) is in a wordlist file. Everything
> is working pretty well, except this: The procedure is running in a recursive
> way, that if you want to look up more than one word, you only put it in next
> time you are asked. To exit this, you only have to input an empty string,
> that is to press the ENTER key. And here is the trouble. The program will
> not stop this looping, unless you enter several empty words.
> 
> Here is the snip of the program that is causing trouble:
> 
> procedure lookup;
> begin
>     writeln;
>     write('please enter word to be looked up: ');
>     readln( s ); { s : STRING; }
>     if s = '' then exit; { this doesn't work,
>                            unless running a couple of
>     assign(f,'wordlist.dat'); reset(f);
>     repeat
>          read(f,t); { t : STRING }
>          if t = s then found := true   else found := false; {found : BOOLEAN}
>     until eof(f);
>     close(f); {f : FILE OF STRING}
>     if not found then begin
>          writeln('the word ', s, 'is not found in the list');
>          lookup;
>       end;{if}
>     writeln('the word ', s, ' is found in the list');
> end;{ procedure lookup}
> 
> Begin
>     { call a menu}
>     Repeat read(keychoice) until keychoice in [#0 .. #255];
>     { keychoice : CHAR }
>     if keychoice = '1' then lookup;
>     { call the menu}
>     ...
> end.
> 
> Hope anybody out there could help. It will be greatly appriciated.
> 
> regards,
> Brian
> times}
> Net-Tamer V 1.11 - Test Drive
> 
Re: WHAT COULD BE WRONG?
#647
Author: Osmo Ronkanen
Date: Thu, 18 May 2000 00:00
51 lines
2378 bytes
In article <D0OU4.6707$C9.118410@news1.online.no>,  <br-pede@online.no> wrote:
>
>
>Here is the snip of the program that is causing trouble:
>
>procedure lookup;
>begin
>    writeln;
>    write('please enter word to be looked up: ');
>    readln( s ); { s : STRING; }
>    if s = '' then exit; { this doesn't work,
>                           unless running a couple of
>    assign(f,'wordlist.dat'); reset(f);
>    repeat
>         read(f,t); { t : STRING }
>         if t = s then found := true   else found := false; {found : BOOLEAN}
>    until eof(f);

There is no reason whatsoever to set found false in the loop. You, however,
must set it false before the loop. Also once you have found the word
there is no need to continue.

Using read with string is problematic as it does not skip end of lines
but starts to return empty strings. If the items are one word per line
then use readln instead. If there are several entries per line, you
need to do some parsing.

Btw if the proper condition was really be that, then you should write:
found := t=s; That is far more readable.


>    close(f); {f : FILE OF STRING}

Is that really file of string???? That would be truly strange.

Btw where have you defined all the variables like found? Do not use
global variables.

>    if not found then begin
>         writeln('the word ', s, 'is not found in the list');
>         lookup;
>      end;{if}
>    writeln('the word ', s, ' is found in the list');
>end;{ procedure lookup}
>

What you really should do is write as function Find() which returns if
the string is found. It is better to isolate action from IO.


Osmo
Re: WHAT COULD BE WRONG?
#648
Author: Frank Peelo
Date: Thu, 18 May 2000 00:00
55 lines
2563 bytes
<br-pede@online.no> wrote in message
news:D0OU4.6707$C9.118410@news1.online.no...
>
>
> I am trying to make a software, where the user can put in some word, and
the
> program will look and see if the word(s) is in a wordlist file. Everything
> is working pretty well, except this: The procedure is running in a
recursive
> way, that if you want to look up more than one word, you only put it in
next
> time you are asked. To exit this, you only have to input an empty string,
> that is to press the ENTER key. And here is the trouble. The program will
> not stop this looping, unless you enter several empty words.
>
> Here is the snip of the program that is causing trouble:
>
> procedure lookup;
> begin
>     writeln;
>     write('please enter word to be looked up: ');
>     readln( s ); { s : STRING; }
>     if s = '' then exit; { this doesn't work,
>                            unless running a couple of
>     assign(f,'wordlist.dat'); reset(f);
>     repeat
>          read(f,t); { t : STRING }
>          if t = s then found := true   else found := false; {found :
BOOLEAN}
>     until eof(f);
>     close(f); {f : FILE OF STRING}
>     if not found then begin
>          writeln('the word ', s, 'is not found in the list');
>          lookup;

Why are you calling lookup recursively here? That would be your problem. You
would be better off redefining Lookup as a function returning a boolean, and
then calling it in a non-recursive loop.
Something like...

Repeat
  readln(s);
  If s='' then
exit;
  if Lookup(s) then
    writeln(s,' found')
  else
    writeln(s,' not found');
until false;

Recursion is useful for some things but should generally be avoided wherever
possible, especially until you have more experience.

FP
Re: WHAT COULD BE WRONG?
#649
Author: Huub van Dooren
Date: Thu, 18 May 2000 00:00
176 lines
5970 bytes
Here are some (maybe usefull) notes on your program.

1. Do not use the word _recursive_ when you are talking about a
loop. Recursive procedures are procedures where the outcome
depends on a previous call to that procedure.
Examples: Fibonacci numbers and fractals or scanning a
directory-tree (= each sub-directory can have zero or
more subdirectories, wich can be treated the same way as the
current sub-directory). Since the outcome of the previous found
word is not of influence to the current 'to be found' word, you
can
not speak of recursion. Use the word repetition (or iteration)
instead.

2. Try to use "exit" and "halt" as less as possible.
Why? When a procedure or a program has more ways to skip code
(=exit) or end (=halt), the readability of it becomes lost. The
proper
use of "if...then..." can prevent this. Make sure a procedure or
program has only one exit-point, namely at the end of it.

3. Declare variables in the procedure where they are used in. Try
to declare as less global variables as you can. These global var
need not to bother procedures where they have no use. It makes
the program also more readable (and you can save some memory).
Regard a procedure as a little program in itself.

Tips:
- Using a repeat...until EOF on an empty file means trouble.
- Never use a semicolon (;) before the else-, end- or
until-keyword
- Try to make a procedure do only one (simple) thing
- When in trouble with loops: place the inside of it in a
separate procedure.
- Use different names for procedure-parameters and variables that
have the same meaning. This also avoids confusion.
- Initialise: before doing anything with a variable, make sure it
has a legal and sensible value.

Keep up the good work. You're doing fine.

See if the following example makes some sense to you.
(Not tested, just practising)

Greetings. Huub.


program word_list_example(input,output);

var no_more:boolean;

function the_same(ss,tt:string):boolean;
begin
    the_same:=(ss=tt)
end; {the_same}

procedure display_found(ff:boolean; ss:string);
{
input:   ff - true=>ss is found in file, ss - word to be found
output: none
}
begin
    write(ss);
    if not ff
       then write(' not ');
    writeln('found.')
end; {display_found}

procedure search_this_word(s:string);
{input: s-word to be found, output:none}
var f:text; t:string;
      found:boolean
begin
     found:=false;
     assign(f,'wordlist.dat');
     reset(f);
     while ((not eof(f)) and (not found)) do
(*    = while not (eof(f) or found) do   *)
     begin
          readln(f,t);
          found:=the_same(s,t)
     end;
     close(f);
     display_found(found,s)
end; {search_this_word}

procedure give_the_word(var stop:boolean);
{input,output: stop = stop lookup words}
var find_me:string;
begin
     writeln('Give word to be found, <enter> to stop');
     readln(s);
     if s=''
        then stop:=true
        else search_this_word(find_me)
end; {give_the_word}

{main}
begin
     no_more:=false;
     repeat
        give_the_word(no_more)
        (* note 1*)
     until no_more
end. {main}

(*
note1: actually  "search_the_word" and "display_found"
should be placed here for better readability.
Preceded by 'if not no_more then begin'.
Variables "s" and "found" would then become
output-parameters to their successors.
To avoid all this complexity, maybe it would be better
to declare them global.
It comes to mind that you may not yet have had a
proper introduction to procedure-parameters in
your course. Sorry, my mistake.
*)


br-pede@online.no heeft geschreven in bericht ...
>
>
>I am trying to make a software, where the user can put in some
word, and the
>program will look and see if the word(s) is in a wordlist file.
Everything
>is working pretty well, except this: The procedure is running in
a recursive
>way, that if you want to look up more than one word, you only
put it in next
>time you are asked. To exit this, you only have to input an
empty string,
>that is to press the ENTER key. And here is the trouble. The
program will
>not stop this looping, unless you enter several empty words.
>
>Here is the snip of the program that is causing trouble:
>
>procedure lookup;
>begin
>    writeln;
>    write('please enter word to be looked up: ');
>    readln( s ); { s : STRING; }
>    if s = '' then exit; { this doesn't work,
>                           unless running a couple of
>    assign(f,'wordlist.dat'); reset(f);
>    repeat
>         read(f,t); { t : STRING }
>         if t = s then found := true   else found := false;
{found : BOOLEAN}
>    until eof(f);
>    close(f); {f : FILE OF STRING}
>    if not found then begin
>         writeln('the word ', s, 'is not found in the list');
>         lookup;
>      end;{if}
>    writeln('the word ', s, ' is found in the list');
>end;{ procedure lookup}
>
>Begin
>    { call a menu}
>    Repeat read(keychoice) until keychoice in [#0 .. #255];
>    { keychoice : CHAR }
>    if keychoice = '1' then lookup;
>    { call the menu}
>    ...
>end.
>
>Hope anybody out there could help. It will be greatly
appriciated.
>
>regards,
>Brian
>times}
>Net-Tamer V 1.11 - Test Drive
Re: WHAT COULD BE WRONG?
#650
Author: Bob Schor
Date: Thu, 18 May 2000 00:00
20 lines
1061 bytes
     Try reordering your recursion logic.

PROCEDURE lookup;

BEGIN   { lookup }
  getinputfromuser;
  IF validinput
   THEN
     BEGIN
       lookitup;
       lookup  { recursive call here }
     END
  END;

Note that it is also pretty easy to convert this recursive routine to an
iterative one, which may have some advantages (for one thing, the stack won't
keep growing).

Bob Schor
Pascal Enthusiast
Re: WHAT COULD BE WRONG?
#651
Author: Jeff Jones
Date: Thu, 18 May 2000 00:00
91 lines
4126 bytes
br-pede@online.no wrote:
> 
> I am trying to make a software, where the user can put in some word, and the
> program will look and see if the word(s) is in a wordlist file. Everything
> is working pretty well, except this: The procedure is running in a recursive
> way, that if you want to look up more than one word, you only put it in next
> time you are asked. To exit this, you only have to input an empty string,
> that is to press the ENTER key. And here is the trouble. The program will
> not stop this looping, unless you enter several empty words.
> 
> Here is the snip of the program that is causing trouble:
> 
> procedure lookup;
> begin
>     writeln;
>     write('please enter word to be looked up: ');
>     readln( s ); { s : STRING; }
>     if s = '' then exit; { this doesn't work,
>                            unless running a couple of
>     assign(f,'wordlist.dat'); reset(f);
>     repeat
>          read(f,t); { t : STRING }
>          if t = s then found := true   else found := false; {found : BOOLEAN}
>     until eof(f);
>     close(f); {f : FILE OF STRING}
>     if not found then begin
>          writeln('the word ', s, 'is not found in the list');
>          lookup;
>       end;{if}
>     writeln('the word ', s, ' is found in the list');
> end;{ procedure lookup}
> 
> Begin
>     { call a menu}
>     Repeat read(keychoice) until keychoice in [#0 .. #255];
>     { keychoice : CHAR }
>     if keychoice = '1' then lookup;
>     { call the menu}
>     ...
> end.


I don't see a need for "lookup" to call itself.  Try the following:

 procedure Lookup;
   const WORDLIST_FILE = 'wordlist.dat';
 begin
     assign(f,WORDLIST_FILE);
     reset(f);
     if eof(f) then
          writeln('Wordlist file ',WORDLIST_FILE,' is empty')
     else
     begin (* f is not empty *)
          writeln;
          write('Please enter word to be looked up or just Enter to
quit: ');
          readln( s ); { s : STRING; }

          repeat (* find the word and get new word to find until empty
string entered *)
               reset(f); (* reset the wordlist file to do further
lookups *)
               found := false;

               repeat (* try to find the word *)
                    readln(f,t);
                    found := (t = s);
               until found or eof(f);

               write('The word ',s,' is ');
               if not(found) then write('not ');
               writeln('found in the list');
               writeln;
               write('Please enter word to be looked up or just Enter to
quit: ');
               readln( s ); { s : STRING; }
          until s = ''; (* find the word, get new word to find until
empty string entered *)

     end; (* f is not empty *)
     close(f);
 end; (* Lookup *)

Ideally, you'd want some error-checking to make sure you could open the
wordlist file.  I suspect that many variables like "found", "s", and "t"
don't need to be global.  

Personally, I'd change some of the redundant code I posted above to be a
procedure declared inside "Lookup" as well as the repeat loop which
tries to find the matching word.  I'd also make the variable names
longer and more descriptive of their usage.
Re: WHAT COULD BE WRONG?
#652
Author: Osmo Ronkanen
Date: Thu, 18 May 2000 00:00
15 lines
1264 bytes
In article <8g100h$hj5$1@news.iaehv.nl>,
Huub van Dooren <hvdooren@iae.nl> wrote:
>Here are some (maybe usefull) notes on your program.
>
>1. Do not use the word _recursive_ when you are talking about a
>loop. Recursive procedures are procedures where the outcome
>depends on a previous call to that procedure.
>Examples: Fibonacci numbers

Argh! That has to be the absolutely worst example for recursion. It
takes exponential time to solve a linear problem when one uses
recursion with Fibonacci numbers.


Osmo
Re: WHAT COULD BE WRONG?
#653
Author: Huub van Dooren
Date: Fri, 19 May 2000 00:00
30 lines
1601 bytes
Osmo Ronkanen heeft geschreven in bericht
<8g1g4a$p63$1@kruuna.Helsinki.FI>...
>In article <8g100h$hj5$1@news.iaehv.nl>,
>Huub van Dooren <hvdooren@iae.nl> wrote:
<snip>
>>Examples: Fibonacci numbers
>
>Argh! That has to be the absolutely worst example for recursion.
It
>takes exponential time to solve a linear problem when one uses
>recursion with Fibonacci numbers.
>
>
>Osmo

Oops, wrong planet.

You're right. Bad example. But almost every textbook uses this
as an introduction to (the problems of using) recursion. BTW
I never found any use for them in all of programs anyhow.  ;-)
Has something to do with rabbits AFAIK.
The other examples were correct, I presume ?

And how about a recursive search for prime numbers?
(Argh! again. In the same overexposed category)

Gee, I really like this newsgroup.

Greetings. Huub.
Re: WHAT COULD BE WRONG?
#654
Author: Dr John Stockton
Date: Fri, 19 May 2000 00:00
36 lines
2484 bytes
JRS:  In article <8g1qgd$bmh$1@news.IAEhv.nl> of Fri, 19 May 2000
00:36:45 seen in news:comp.lang.pascal.borland, Huub van Dooren
<hvdooren@iae.nl> wrote:
>>Argh! That has to be the absolutely worst example for recursion.
>It
>>takes exponential time to solve a linear problem when one uses
>>recursion with Fibonacci numbers.
>>
>
>You're right. Bad example. But almost every textbook uses this
>as an introduction to (the problems of using) recursion. BTW
>I never found any use for them in all of programs anyhow.  ;-)


They are useful if you have a function known to have a single maximum
(and no minima) within a given range, and you wish to locate that
maximum to within a predefined accuracy while using the minimum number
of function evaluations.

At each step, you are considering two evaluated points within the
remaining range, dividing the range into three, and the maximum cannot
lie between the smaller and its nearer end-of-range.

The larger point is reused in the next step, and Fibonacci-based
positioning ensures that each step reduces the range as much as
possible.  In the final step, the points are at 1/3 & 2/3 of possible
range.


They are also useful in the design of sunflowers and pine-cones.
 
-- 
� John Stockton, Surrey, UK.  jrs@merlyn.demon.co.uk   Turnpike v4.00   MIME. �
 Web <URL: http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
 Proper 4-line sig. separator is as above, a line exactly "-- " (SonOfRFC1036)
 Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads