Hello World
The obligatory Hello world program, which prints the string "Hello World" to the console.
module HelloWorld
{
imports
{
console = new Heron.Windows.Console();
}
methods
{
Main()
{
WriteLine("Hello World");
}
}
}Fibonacci
This program prints out the first 10 numbers in the Fibonacci sequence. It demonstrates functions signatures and the if statement.
module Fibonacci
{
imports
{
Console = new Heron.Windows.Console();
}
fields
{
max = 10;
}
methods
{
Fib(n : Int) : Int
{
if (n <= 0)
return 0;
else if (n == 1)
return 1;
else
return Fib(n - 1) + Fib(n - 2);
}
Main()
{
foreach (i in 0..max)
WriteLine(Fib(i).ToString());
}
}
}Sieve of Eratosthenes
This program prints out the prime numbers up to 100. It demonstrates ranges and the select operator.
module Sieve
{
imports
{
console = new Heron.Windows.Console();
}
fields
{
max = 10;
}
methods
{
Main()
{
var primes = 0..(max * max);
foreach (i in 2..max)
primes = select (j from primes)
j % i != 0;
foreach (i in primes)
Console.WriteLine(i);
}
}
}
Heron language looks utterly stupid after Ruby.
The same Sieve of Eratosthenes in Ruby looks like:
$max = 10 puts primes = (2..$max * $max).to_a.delete_if {|i| (2..Math::sqrt(i).floor).any?{|j| i % j == 0 } }I find the Heron version to be more readable, though more verbose. I don't know what you find that makes Heron look "utterly" stupid.
the only problem with ruby is that it allows you to write code which is very hard to read... just as it was demonstrated in this line. personally I prefer a more structured approach to a program.
@alecn2: Wow, that ruby looks ugly, try Haskell: primes = euler [2..] euler (p : xs) = p : euler (xs minus map (p) (p : xs))
Correcting the formatting:
Standard language bigotry in full effect in these comments. Stereotypes personified:
Ruby: That's so stupid! Look at this one-liner!
Haskell: That's so complicated! Look at this much more complex version! <gets it wrong>
Any more?
Well, just because you can write it in one line in Haskell and Ruby, and Python, is no reason to do so in production code. Production code should be written so that it is perfectly clear and obvious how it works, or if it doesn't work, makes it perfectly clear how to fix it so that it works.
Personally I don't see many advantages over C#, or Python. I subjectively hate Ruby syntax, and think Functional languages are for Dweebs. Is that enough language bigotry? Did I miss any obvious tropes here?
Warren Warren