Week: xx Nemerle
written by AdamSpeight2008
Warning!
Spoiler
Nemerle
Nemerle.org said:
Nemerle is a high-level statically-typed programming language for the .NET platform. It offers
functional, object-oriented and imperative features. It has a simple C#-like syntax and a powerful meta-programming system.
functional, object-oriented and imperative features. It has a simple C#-like syntax and a powerful meta-programming system.
Download & Resources
Nemerle on Google Code
Nemerle .org (many of the links no longer work.)
Example of Nemerle SourceCode.
I've chosed the 99 Bottle of Beer example because it demonstrates and utilises, the syntax of
#pragma indent using System; using Nemerle.Collections; using Nemerle.Text; using Nemerle.Utility; using System.Collections.Generic; using System.Console; using System.Linq; module Program { Main() : void { def capitalize=(str)=>{ Char.ToUpper(str[0]).ToString() + str.Substring(1);} def beers(n){ | 0 => "no more bottles of beer" | 1 => "1 more bottle of beer" | _ => $"$n bottles of beer"} def passAround(n){ | 0 => $"Go to the store and buy some more, $(beers(99)) on the wall.\n" | _ => $"Take one down and pass it around, $(beers(n-1)) on the wall.\n" } def onTheWall = n => $"$(beers(n)) on the wall, $(beers(n)).\n"; $[99, 98 .. 0].Iter(n => Console.WriteLine(capitalize(onTheWall(n)) + passAround(n))); _ = System.Console.ReadKey(true); } }
It first glance it looks a lot like C#, it is but with a cut and trim.
Quick overview.
In Nemerle all methods are Functions, which makes it like a Functional language like OCaml.
This means there are no subroutines. The value return by a method is the last expression on the stack before it exits. To simulate a subroutine you can return the void literal.
def beers(n) | 0 => "no more bottles of beer" | 1 => "1 more bottle of beer" | _ => $"$n bottles of beer"
Think of this as a Select Case ... End Select on steroids.
1st option is when n == 1
2nd option is when n == 0
3rd option is the wild card / default.
but cases don't have to be simple values, they can be nearly any expression.
It also use C# style strings, plus it has version that allows you the embedded the value of expressions inside them. Eg
$"$(beers(n)) on the wall, $(beers(n)).\n" outputs (lets say n=12
"10 bottles of beer on the wall, 10 bottles of beer.
In Nemerle all methods are Functions, which makes it like a Functional language like OCaml.
passAround(n) | 0 => $"Go to the store and buy some more, $(beers(99)) on the wall.\n" | _ => $"Take one down and pass it around, $(beers(n-1)) on the wall.\n"
Some of the more advance things to Nemerle can do. Do to it having a meta-programming system is that the syntax, grammar and operators of Nemerle, can be user defined just by writing Nemerle code.
Nemerle is partly written in Nemerle.