comp.lang.ada
 help / color / mirror / Atom feed
* Continuation Passing Style in Ada
@ 2009-05-08  3:36 Gene
  0 siblings, 0 replies; only message in thread
From: Gene @ 2009-05-08  3:36 UTC (permalink / raw)


As a recreation, I've been coding, in different languages, a little
program that prints all possible ways to parenthesize a given
arithmetic expression. A way to avoid storing all the possiblities is
to use continuation passing style. The Ada version is quite pleasing,
so thought I'd share it. (You don't want to see the C version):

with Ada.Text_IO;

procedure Paren_Search is

   procedure Parenthesize(Expr : in String;
                          Continue : access procedure (Expr : in
String)) is

      Op : Positive := Expr'First + 1;

      procedure Parenthesize_Rhs(Lhs : in String) is

         procedure Apply_Op(Rhs : in String) is
         begin
            Continue("(" & Lhs & Expr(Op) & Rhs & ")");
         end Apply_Op;

      begin
         Parenthesize(Expr(Op + 1 .. Expr'Last), Apply_Op'Access);
      end Parenthesize_Rhs;

   begin
      if Expr'Length = 1 then
         Continue(Expr);
      else
         while Op < Expr'Last loop
            Parenthesize(Expr(Expr'First .. Op - 1),
Parenthesize_Rhs'Access);
            Op := Op + 2;
         end loop;
      end if;
   end Parenthesize;

begin
   Parenthesize("2/2-3/3-4/4-5/5", Ada.Text_IO.Put_Line'Access);
end Paren_Search;



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2009-05-08  3:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-08  3:36 Continuation Passing Style in Ada Gene

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