comp.lang.ada
 help / color / mirror / Atom feed
From: Robert Dewar <robert_dewar@my-deja.com>
Subject: Re: string operations
Date: 2000/11/19
Date: 2000-11-19T00:00:00+00:00	[thread overview]
Message-ID: <8v90vm$j5f$1@nnrp1.deja.com> (raw)
In-Reply-To: 3A16A4D5.CE941970@t-online.de

In article <3A16A4D5.CE941970@t-online.de>,
  Margit Gut <Margit.Gut@t-online.de> wrote:
> Hi,
>
> my name is Margit and I am new here. I just start to learn ADA
and I
> have some problems in understandig what I am doing.
>
> I try to read a string from the keyboard and then I want to
write a
> recursiv function to turn the characters  from the end to the
beginning.,


This assignment has nothing to do with Ada specifically. The
requirement for a recursive solution is the DOMINANT component
of the assignment. You need to formulate an appropriate
recursive approach completely independent of the language in
which you will write, and you have not got that far, so your
attempts to turn your thinking into Ada are very premature.

Here are some hints:

1. Your solution must be able to work for arbitrary length
input, so any constant like 255 is immediately wrong.

2. You need to think in terms of reading individual characters

Learning to formulate recursive solutions to problems is a
very challenging problem for many students, and I would say
that many professional programmers have never learned this
approach. One valuable excercise I have found is to write
a reasonably complex program with no assignments at all.

Here is my favorite example of recursion, to output an integer
without worrying about its size:

  with Text_IO; use Text_IO;
  procedure Print_Nat (N : Natural) is
  begin
     if N >= 10 then
        Print_Nat (N / 10);
     end if;

     Put (Character'Val (N mod 10 + Character'Pos ('0')));
  end Print_Nat;

I like this example since it is not simple tail recursion like
the factorial example, and it is in fact very hard work to turn
this into a loop without imposing some arbitrary limit on the
length. Note that this algorithm would work fine in a language
that had arbitrary precision integers.






Sent via Deja.com http://www.deja.com/
Before you buy.




  parent reply	other threads:[~2000-11-19  0:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-18  0:00 string operations Margit Gut
2000-11-18  0:00 ` Jeff Carter
2000-11-19  0:00 ` Robert Dewar [this message]
2000-11-20  0:00   ` Anders Wirzenius
2000-11-20  0:00     ` Robert Dewar
2000-11-21  6:30       ` Anders Wirzenius
2000-11-24  0:00         ` Margit Gut
2000-11-24  0:00         ` Margit Gut
  -- strict thread matches above, loose matches on Subject: below --
2000-11-24  0:00 String operations Margit Gut
replies disabled

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