出窥.net上的Nemerle语言

3月31日,Nemerle发布了它的0.2.10版本。在这篇文章中,我们通过Nemerle的Tutorial来看看这种.net上的新语言。

Nemerle is a new functional language designed from the ground up for the .NET. In this paper we have focused on features absent in traditional ML-like and object-oriented languages: variant inheritance, assertions and powerful code-generating macros. We also gave concern for the syntax and the “spirit” of Nemerle that makes it a good transition language for programmers with C# background.

这是Nemerle的intro里对它的介绍,为了保证准确性,我们引用了全文。而它的一些特性我们通过下面的几个例子来概览Nemerle.

None.gif   using  System;
None.gif 
using  System.Console;
None.gif 
None.gif 
public   class  Adder
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
InBlock.gif   
public static Main () : void
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif     WriteLine (
"The sum is {0}",
InBlock.gif                Int32.Parse (ReadLine ()) 
+
InBlock.gif                Int32.Parse (ReadLine ()));
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif }

这个程序实现了输入两个数并输出他们的和。从这段代码里,我们可以清晰的看到void等返回类型都写到了右边的冒号后面,而且在使用using的时候,引用的不单单是命名空间(namespace),还可以引用类的成员了,这样使得我们的WriteLine()和ReadLine()函数都可以直接的使用。

我们换一种方式来实现

None.gif   using  System;
None.gif 
None.gif 
public   class  Adder
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
InBlock.gif   
//  It is private by default.
InBlock.gif
   static ReadInteger () : int
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif     Int32.Parse (Console.ReadLine ())
ExpandedSubBlockEnd.gif   }

InBlock.gif 
InBlock.gif   
public static Main () : void
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif     def x 
= ReadInteger (); //  Variable definition.
InBlock.gif
     def y = ReadInteger ();
InBlock.gif     
//  Use standard .NET function for formatting output.
InBlock.gif
     Console.WriteLine ("{0} + {1} = {2}", x, y, x + y);
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif }

事实上我们更希望使用这种方法来实现我们的目的。在Main()函数里我们
定义了两个值x和y,是通过def关键字实现的,您也看到了我们并没有写出
变量究竟是什么类型的,编译器看到ReadInteger返回int型后就把x定义
为int型变量了。我们称之为类型推断(Type Inference)

 
 
 
 
None.gif   class  LineCounter
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
InBlock.gif   
public static Main () : void
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif     
//  Open a file.
InBlock.gif
     def sr = System.IO.StreamReader ("SomeFile.txt");   //  (1)
InBlock.gif
     mutable line_no = 0;                               //  (2)
InBlock.gif
     mutable line = sr.ReadLine ();
ExpandedSubBlockStart.gifContractedSubBlock.gif     
while (line != nulldot.gif{              //  (3)
InBlock.gif
       System.Console.WriteLine (line);
InBlock.gif       line_no 
= line_no + 1;            //  (4)
InBlock.gif
       line = sr.ReadLine ();
ExpandedSubBlockEnd.gif     }
;                                  //  (5)
InBlock.gif
     System.Console.WriteLine ("Line count: {0}", line_no);
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif }
 
 
 
 
 
 
 
 
 
 
None.gif   class  LineCounterWithoutLoop
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
InBlock.gif   
public static Main () : void
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif     def sr 
= System.IO.StreamReader ("SomeFile.txt");
InBlock.gif     mutable line_no 
= 0;
InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif     def read_lines () : 
void dot.gif{            //  (1)
InBlock.gif
       def line = sr.ReadLine ();
ExpandedSubBlockStart.gifContractedSubBlock.gif       when (line 
!= nulldot.gif{               //  (2)
InBlock.gif
         System.Console.WriteLine (line);  //  (3)
InBlock.gif
         line_no = line_no + 1;            //  (4)
InBlock.gif
         read_lines ()                     //  (5)
ExpandedSubBlockEnd.gif
       }

ExpandedSubBlockEnd.gif     }
;
InBlock.gif 
InBlock.gif     read_lines ();      
//  (6)
InBlock.gif
 
InBlock.gif     System.Console.WriteLine (
"Line count: {0}", line_no); //  (7)
ExpandedSubBlockEnd.gif
   }

ExpandedBlockEnd.gif }
 
 
 
 
 
 
None.gif   class  FunctionalLineCounter
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
InBlock.gif   
public static Main () : void
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif     def sr 
= System.IO.StreamReader ("SomeFile.txt");
ExpandedSubBlockStart.gifContractedSubBlock.gif     def read_lines (line_no : 
int) : int dot.gif{   //  (1)
InBlock.gif
       def line = sr.ReadLine ();
InBlock.gif       
if (line == null)      //  (2)
InBlock.gif
         line_no              //  (3)
ExpandedSubBlockStart.gifContractedSubBlock.gif
       else dot.gif{
InBlock.gif         System.Console.WriteLine (line);  
//  (4)
InBlock.gif
         read_lines (line_no + 1)          //  (5)
ExpandedSubBlockEnd.gif
       }

ExpandedSubBlockEnd.gif     }
;
InBlock.gif 
InBlock.gif     System.Console.WriteLine (
"Line count: {0}", read_lines (0)); //  (6)
ExpandedSubBlockEnd.gif
   }

ExpandedBlockEnd.gif }


这段程序从(3)处结束并返回结果,而参数的累加结果正是最后输出的line_no的值.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值