Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ada actual for "S" must be a variable

Tags:

ada

So here is a piece of my body file. I am getting the error "words.adb:75:42: actual for "S" must be a variable".

procedure Remove_Character(S : in out Ustring; C : in Character; Successful : out Boolean) is
begin
    for I in 1..length(S) loop
        if Element(S, I) = C then
            Delete(S, I, I);
            Successful := true;
            return;
        end if;
    end loop;
    Successful := false;
end Remove_Character;

function Is_Subset(Subset : Ustring; S : Ustring) return Boolean is
Could_Remove : Boolean;
begin
    for I in 1..length(Subset) loop
        Remove_Character(S , Element(Subset, I), Could_Remove);
        if Could_Remove = false then
            return false;
        end if;
    end loop;
    return True;
end Is_Subset;

I understand where my error is coming from. Remove_Character uses S : in out Ustring while function Is_Subset uses S : in Ustring. My question is how do I change the variable from Remove_Character into only an in Ustring? Sorry if this is a tad jumbled, I'm fairly new to both programming and the site.

like image 510
user2396120 Avatar asked May 18 '13 05:05

user2396120


1 Answers

You can't, at least not directly.

I don't know what a UString is, but I presume the Delete procedure modifies it. If you changed the declaration of S in Remove_Character to S: in Ustring, you'd presumably get an error on the call to Delete.

The simplest approach I can think of would be to make a copy of S in Is_Subset:

Copy_Of_S: UString := S;

and then pass the (modifiable) copy to Remove_Character.

By "simplest", I mean it makes the smallest change to your existing code. But you should probably consider reorganizing it. Determining whether one UString is a subset of another by modifying one of the strings doesn't seem like the best approach; I'm sure there's a more efficient way to do it.

A minor and irrelevant point: this:

if Could_Remove = false then

is better written as:

if not Could_Remove then
like image 162
Keith Thompson Avatar answered Oct 16 '22 17:10

Keith Thompson