维生素C.net
Talents come from diligence, and knowledge is gained by accumulation 天才源于勤奋,知识源于积累。
难忘的1654天
博客园  首页  新随笔  新文章  联系  管理  订阅 订阅
随笔- 220  文章- 1  评论- 1631 
出窥.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.

 using System;
 
using System.Console;
 
 
public class Adder
 
{
   
public static Main () : void
   
{
     WriteLine (
"The sum is {0}",
                Int32.Parse (ReadLine ()) 
+
                Int32.Parse (ReadLine ()));
   }

 }

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

我们换一种方式来实现

 using System;
 
 
public class Adder
 
{
   
//  It is private by default.
   static ReadInteger () : int
   
{
     Int32.Parse (Console.ReadLine ())
   }

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

 }

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

所以,我们可以这样用,new关键字也不需要了。
下面的这个程序是读出一个文本文件有多少行:



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

 }

这里体现了很多值得我们注意的地方。首先,(1)和(2)两个地方就非常不一样,(1)中
我们定义了一个不可变的值(immutable value),他们都是一旦建立就永远不会改变的。
(2)中我们定义了一个可变的值(mutable value),它很像C#里的变量,必须初始化才
能使用,并且还可以改变它的值。在(3)中我们使用的很像C#中的while语句,同样,
你也可以使用do…while的。
Functional programming (FP) is style in which you do not modify 
state of the machine with instructions, but rather evaluate functions
yielding new and new values. That is entire program is just one big
expression. In purely functional language (Haskell being main example)
you cannot modify any objects once they are created (there is no
assignment operator, like = in Nemerle). There are no loops,
just recursive functions.
在Nemerle里我们不一定非要使用FP编程方式,但是当你想用的时候可以随心所欲。修改
一下上面的代码:


 class LineCounterWithoutLoop
 
{
   
public static Main () : void
   
{
     def sr 
= System.IO.StreamReader ("SomeFile.txt");
     mutable line_no 
= 0;
 
     def read_lines () : 
void {            //  (1)
       def line = sr.ReadLine ();
       when (line 
!= null) {               //  (2)
         System.Console.WriteLine (line);  //  (3)
         line_no = line_no + 1;            //  (4)
         read_lines ()                     //  (5)
       }

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

 }

这段代码的思路还是比较好理解的,就是一段递归程序。文本文件有多少
行,read_lines()函数就要执行几次,像使用while循环一样,只不过稍
微换了一种思路。这时候可能您会考虑到使用递归而影响效率的问题,事实是
当一个函数主体在调用另一个函数后执行完毕,没有创建任何新的堆栈,这
被称作尾端调用(Tail Call),所以这段代码和while循环在效率上一样的。
Nemerle尤其注重将循环写成递归的深入的理解,文中用Grok来形容这种理
解的程度,可见其重要性。
需要提到的一点是,使用if的时候必须使用else语句。
上面的这段程序还不是完全符合FP要求的,改写一下:

 class FunctionalLineCounter
 
{
   
public static Main () : void
   
{
     def sr 
= System.IO.StreamReader ("SomeFile.txt");
     def read_lines (line_no : 
int) : int {   //  (1)
       def line = sr.ReadLine ();
       
if (line == null)      //  (2)
         line_no              //  (3)
       else {
         System.Console.WriteLine (line);  
//  (4)
         read_lines (line_no + 1)          //  (5)
       }

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

 }


这段程序从(3)处结束并返回结果,而参数的累加结果正是最后输出的line_no的值.
最后需要提到的是类型推断还可以用在函数的参数和返回类型上,当然是有一定说法
的,
更详细的内容从其官方网站上可以看到
posted on 2005-04-07 10:45 维生素C.NET 阅读(1452) 评论(2)  编辑 收藏 所属分类: XHTML & Web Standard

发表评论
  回复  引用    
2005-04-09 11:47 | CsOver [未注册用户]
又是一种新语言体的诞生!
  回复  引用  查看    
2005-04-13 15:03 | bluefee      
初窥才对,我刚才也打成出窥了!
社区  新闻  新用户注册  刷新评论列表  

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2005-04-07 10:49 编辑过
 
另存  打印
所属分类的其他文章:
· http的基础知识帮助减少代码量和复杂度的一个Demo
· Microsoft Expression Web Designer
· ASP.NET: State Server Gems
· lifework总结的的Enterprise Library v1.0概述
· Microsoft Visual Studio Extensions for WinFX Beta 1
· Unable to get the project ile from the web server错误的解决方法
· 一个OOP的课程设计,不难实现,贴出来请大家指正。
· 出窥.net上的Nemerle语言
· Mono 1.1.5 正式发布,密切关联.net 2.0
· 春天终于来了
最新IT新闻:
· 浅析facebook的信息架构
· Mozilla将于本周五发布Firefox 3.1第一个预览版
· 瑞星将向个人用户免费1年
· 中国互联网历史上最伟大的产品TOP10(二)
· 盖茨官方否认天价租楼看奥运 纯属地产商炒作
博客园新闻频道  博客园首页  社区
 






公告

view my mvp profile 看看有多少人来访问我的Blog了!
hotmail

<2005年4月>
日一二三四五六
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

与我联系

  • 发短消息

常用链接

  • 我的随笔
  • 我的空间
  • 我的短信
  • 我的评论
  • 更多链接
  • 我的参与
  • 我的新闻
  • 最新评论
  • 我的标签

留言簿(168)

  • 给我留言
  • 查看留言

我参与的团队

  • 北京.NET俱乐部(1/1359)
  • 烟台.NET俱乐部(0/47)
  • ASP.NET AJAX (Atlas)学习(4/1318)
  • MVP团队(2/497)
  • 博客园培训团队(0/110)
  • Silverlight学习与研究(1/276)
  • CLR基础研究团队(0/356)

随笔分类(148)

  • ASP.NET(26)
  • Code Warehouse(20)
  • IronRuby,DLR(2)
  • LINQ(3)
  • Reading(3)
  • Training@cnblogs(23)
  • Ubuntu(4)
  • Windows Live(6)
  • Windows Mobile(7)
  • XHTML & Web Standard(54)

随笔档案(220)

  • 2008年3月 (2)
  • 2008年1月 (3)
  • 2007年12月 (3)
  • 2007年9月 (1)
  • 2007年8月 (2)
  • 2007年7月 (3)
  • 2007年6月 (3)
  • 2007年3月 (4)
  • 2007年2月 (3)
  • 2007年1月 (1)
  • 2006年12月 (1)
  • 2006年11月 (8)
  • 2006年10月 (6)
  • 2006年9月 (11)
  • 2006年8月 (5)
  • 2006年7月 (4)
  • 2006年6月 (1)
  • 2006年5月 (10)
  • 2006年4月 (8)
  • 2006年2月 (2)
  • 2006年1月 (1)
  • 2005年12月 (11)
  • 2005年11月 (13)
  • 2005年10月 (3)
  • 2005年9月 (1)
  • 2005年8月 (4)
  • 2005年7月 (3)
  • 2005年6月 (4)
  • 2005年4月 (5)
  • 2005年3月 (10)
  • 2005年2月 (7)
  • 2005年1月 (28)
  • 2004年12月 (15)
  • 2004年11月 (10)
  • 2004年10月 (5)
  • 2004年9月 (1)
  • 2004年6月 (13)
  • 2004年5月 (5)

文章档案(1)

  • 2005年5月 (1)

相册

  • ASPNET2tutorial
  • BlogUsing
  • My love and my friends
  • newGallery
  • 下一代网络图片

.net网站收藏

  • ASP.NET2.0 Tutorial
  • CodeBetter.com
  • F#
  • IIS.net
  • MS NewsGroup
  • NewsGroups
  • OnlyVC.org
  • VWD2005GuidedTour
  • ZDNet China软件技术专区

OSS 2007

  • Charsh
  • Kaneboy
  • Official Team Blog

Python

  • BeginnersGuide

好友的BLOG

  • DemoFox@JoyCode
  • DflyingChen
  • dudu
  • EricLee
  • hbifts
  • idior
  • Jesee Qing
  • Lion
  • Rickie
  • Samuel
  • Steph`s Website
  • 翱翔.Net
  • 陈敬熙
  • 发条木偶
  • 葛涵涛
  • 古道风
  • 寒枫天伤
  • 老猫の理想
  • 刘老师
  • 刘彦博
  • 吕震宇
  • 木野狐
  • 佘广
  • 王sir
  • 小涛
  • 小新
  • 肖老师
  • 旋哥

搜索

  •  

积分与排名

  • 积分 - 389830
  • 排名 - 51

最新评论

  • 1. re: .NET Beginner Training Step by Step开始启动
  • 申请加入
  • --paulo
  • 2. re: 在配置使用Membership或其他的Providers的ASP.NET2.0时一定要设置applicationName属性
  • 我正急切需要知道这个applicationname有什么用处,真是太感谢博主了!
  • --激动了
  • 3. re: .NET Beginner Training Step by Step开始启动
  • 我要加入 我要加入
  • --赵岩
  • 4. re: .NET Beginner Training Step by Step开始启动
  • dd
  • --Simens
  • 5. re: .NET Beginner Training Step by Step开始启动
  • ID: kettle8

    申请加入
  • --wangbo

阅读排行榜

  • 1. 英文名字及含义(24897)
  • 2. SQL Server 2005 Remote Access(14542)
  • 3. Visual Studio 2005 Team Edition和SQL Server 2005的下载(14237)
  • 4. Windows Installer 3.1(11082)
  • 5. Visual Studio 2005 Professional Released(10840)

评论排行榜

  • 1. .NET Beginner Training Step by Step开始启动(309)
  • 2. Windows Live Messenger 8.0 Beta 的邀请(100)
  • 3. 加入[ 下一代网络web技术(Next Generation Web Application)团队Blog ](85)
  • 4. 博客园新手.net技术培训活动(55)
  • 5. 为什么在vista上做开发?(54)
Copyright ©2008 维生素C.NET