Ada, the next Python-killer

That’s the most beautiful Haskell code I’ve ever read.

1 Like

When doing this sort of recursive string manipulation, I typically like case Str'Length as it allows putting the base case(s) on the When X and allowing Others to handle the recursion. It also works in the instances where you’re doing array-processing, which is why I’m surprised at how many people come in hyped on functional programming and immediately start making an Optional type… rather than leveraging unconstrained arrays.

function Trim_Trailing_CRLF (S : String) return String is
begin
   for I in reverse S'Range loop
      if Character'Pos (S (I)) not in 10 | 13 then
         return S (S'First..I);
      end if;
   end loop;
   return "";
end Trim_Trailing_CRLF;

Wasn’t this supposed to be

   Subtype Non_Graphic is ASCII
     with Static_Predicate =>  Non_Graphic in Character'Val(0)..Character'Val( Natural'Pred(Character'Pos(' ')) ) | ASCII'Last;

?

Wasn’t this

supposed to be

   function Without_Nongraphics (Str : String) return String is
     ([for C of Str when C not in Non_Graphic => C]);

?

(This isn’t intended to simply be nitpicking. I’m still learning the details of the newer Ada standards, so checking the original intent of the poster helps me and others who are also not completely familiar with the specifics.)

1 Like

You’re right. Just a few typos due to my typing.

I like it, because I have tried a procedural approach and it was ugly. But I won’t consider any approach adequate in ALL circumpstances. (A pure functionnal language could frustrate me. I have tried Haskell a bit…). And sure, we can write hard to read programs in any languages (and functionnal languages may be quite usefull to do so, see The Evolution of a Haskell Programmer), and sure the “terser” the language is, the more difficult to read a complex given code if it is not well structured (even if in the Haskell joke I have quoted, the terser proposition is easier to read!).
But I agree that some functionnal functions are hard to read. The magical List.group function I have used can need 9 lines of recursive code (and much more if optimised !). The idea is to isolate such code and avoid to mix too many things in the same function, then none of the “domain specific” functions are recursive, and all of them are easy to read.