Ubuntu中常用的编程语言有哪些?

2026-05-25 11:501阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计3034个文字,预计阅读时间需要13分钟。

Ubuntu中常用的编程语言有哪些?

在上篇随笔中,我尝试用+C语言实现了一个简单的计算器功能。

在上一篇随笔中,使用 C# 和 Java 语言来求解一五八二年十月四日是星期几。现在,让我们在 Ubuntu 10.04 操作系统中使用多种编程语言来求解这个问题。其中有“2010年6月编程语言排行榜”中前二十名中的:Java、C、C++、(Visual)Basic、C#、Python、JavaScript、Ruby,还有排名四十以后的:Scala、F#。下面就具体讲述如何在 Ubuntu 操作系统中安装这些编程语言。然后通过编写求解这个具体问题的程序,并编译和运行,或者解释执行,或者在交互窗口中执行,使得对这些编程语言有最初步的了解。

Java

让我们从2010年6月编程语言排行榜中的第一名 Java 开始吧。下面就是 GregorianTest.java 程序:

import java.util.*; public class GregorianTest { public static void main(String[] args) { GregorianCalendar dt = new GregorianCalendar(1582, 10 - 1, 4); System.out.println(dt.getTime()); dt.add(Calendar.DAY_OF_MONTH, 1); System.out.println(dt.getTime()); } }

注意,java.util.GregorianCalendar 类的构造函数中月份的取值范围是从 0 到 11。

安装 OpenJDK,编译和运行:

ben@ben-1520:~/work$ sudo apt-get install openjdk-6-jdk ben@ben-1520:~/work$ java -version java version "1.6.0_18" OpenJDK Runtime Environment (IcedTea6 1.8) (6b18-1.8-0ubuntu1) OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode) ben@ben-1520:~/work$ javac GregorianTest.java ben@ben-1520:~/work$ java GregorianTest Thu Oct 04 00:00:00 CST 1582 Fri Oct 15 00:00:00 CST 1582 ben@ben-1520:~/work$

可以看出,Java 语言很好地解决了这个问题:儒略历1582年10月4日星期四的下一天是格里历1582年10月15日星期五。

Scala

在2010年6月编程语言排行榜中排名四十三位的 Scala 是 Java 平台上的一门新兴的语言。老赵在“在.NET 平台上使用Scala语言(上):初尝”中说:“我非常希望它可以取代Java这种劣质语言,让Java平台的生产力上一个台阶。”

Scala 也支持 .NET 平台,请参见“也谈在 .NET 平台上使用 Scala 语言(上)”。

下面就是 GregorianTest.scala 程序:

object GregorianTest extends Application { val dt = new java.util.GregorianCalendar(1582, 10 - 1, 4) println(dt.getTime()) dt.add(java.util.Calendar.DAY_OF_MONTH, 1) println(dt.getTime()) }

安装 Scala SDK,编译和运行(scalac 是编译器,scala 用于运行编译后的程序,也可以当作交互窗口使用):

Ubuntu中常用的编程语言有哪些?

ben@ben-1520:~/work$ sudo apt-get install scala ben@ben-1520:~/work$ scala Welcome to Scala version 2.7.7final (OpenJDK 64-Bit Server VM, Java 1.6.0_18). Type in expressions to have them evaluated. Type :help for more information. scala> Math.Pi res0: Double = 3.141592653589793 scala> :quit ben@ben-1520:~/work$ scalac -version Scala compiler version 2.7.7final -- (c) 2002-2008 LAMP/EPFL ben@ben-1520:~/work$ scalac GregorianTest.scala ben@ben-1520:~/work$ scala GregorianTest Thu Oct 04 00:00:00 CST 1582 Fri Oct 15 00:00:00 CST 1582 ben@ben-1520:~/work$

由于使用 Java 平台的 java.util.GregorianCalendar 类,运行结果和 Java 语言是一样的。

Visual Basic.NET

(Visual)Basic 语言在2010年6月编程语言排行榜中排名第五位。下面就是 GregorianTest.vb 程序:

01: Module GregorianTest 02: Sub Main() 03: Dim dt As DateTime 04: dt = New DateTime(1582, 10, 4) 05: Console.WriteLine(dt.ToString("dddd yyyy-MM-dd")) 06: Console.WriteLine(dt.AddDays(1).ToString("dddd yyyy-MM-dd")) 07: End Sub 08: End Module

安装 Visual Basic.NET 编译器,编译和运行:

ben@ben-1520:~/work$ sudo apt-get install mono-vbnc ben@ben-1520:~/work$ vbnc GregorianTest.vb Visual Basic.Net Compiler version 0.0.0.5914 (Mono 2.4.2 - r) Copyright (C) 2004-2008 Rolf Bjarne Kvinge. All rights reserved. Assembly 'GregorianTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' saved successfully to '/home/ben/work/GregorianTest.exe'. Compilation successful Compilation took 00:00:01.9895840 ben@ben-1520:~/work$ ./GregorianTest.exe 星期一 1582-10-04 星期二 1582-10-05 ben@ben-1520:~/work$

在 .NET 平台中,DateTime 把格里历外推到1582年10月15日之前,取代儒略历,从而错误地认为1582年10月4日是星期一(实际上应该是星期四)。

C#

C# 语言在2010年6月编程语言排行榜中排名第六位。下面就是 GregorianTest.cs 程序:

01: using System; 02: 03: class GregorianTest 04: { 05: static void Main() 06: { 07: var dt = new DateTime(1582, 10, 4); 08: Console.WriteLine(dt.ToString("dddd yyyy-MM-dd")); 09: Console.WriteLine(dt.AddDays(1).ToString("dddd yyyy-MM-dd")); 10: } 11: }

安装 C# 编译器,编译和运行:

ben@ben-1520:~/work$ sudo apt-get install mono-devel ben@ben-1520:~/work$ gmcs --version Mono C# compiler version 2.4.4.0 ben@ben-1520:~/work$ gmcs GregorianTest.cs ben@ben-1520:~/work$ ./GregorianTest.exe 星期一 1582-10-04 星期二 1582-10-05 ben@ben-1520:~/work$

不出所料,运行结果和 Visual Basic.NET 是一样的。

.NET Framework Base Class Library 中有 System.Globalization.GregorianCalendar 类,我们来看看使用该类的 GregorianTest2.cs 程序:

01: using System; 02: using System.Globalization; 03: 04: class Program 05: { 06: static void Main() 07: { 08: var dt = new JulianCalendar().ToDateTime(1582, 10, 4, 0, 0, 0, 0); 09: Console.WriteLine(dt == new DateTime(1582, 10, 15).AddDays(-1)); 10: WriteLine(new JulianCalendar(), dt); 11: WriteLine(new GregorianCalendar(), dt.AddDays(1)); 12: } 13: 14: static void WriteLine(Calendar cal, DateTime dt) 15: { 16: Console.WriteLine("{0,-9} {1:D4}-{2:D2}-{3:D2}", 17: cal.GetDayOfWeek(dt), cal.GetYear(dt), cal.GetMonth(dt), cal.GetDayOfMonth(dt)); 18: } 19: }

该程序的运行结果如下所示:

ben@ben-1520:~/work$ gmcs GregorianTest2.cs ben@ben-1520:~/work$ ./GregorianTest2.exe True Thursday 1582-10-04 Friday 1582-10-15 ben@ben-1520:~/work$

看来如果由用户自己指定使用儒略历还是格里历,.NET 平台的 System.Globalization.Calendar 相关的类还是能够正常工作的。

F#

F# 语言在2010年6月编程语言排行榜中排名第四十五位。下面就是 GregorianTest.fs 程序:

1: let dt = System.DateTime(1582, 10, 4) 2: printfn "%s" (dt.ToString("dddd yyyy-MM-dd")) 3: printfn "%s" (dt.AddDays(1.0).ToString("dddd yyyy-MM-dd"))

安装 F# 编译器:

ben@ben-1520:~/work$ wget download.microsoft.com/ download/1/3/B/13BE2B98-E487-4032-9441-22D4D2F4FAAC/fsharp.zip ben@ben-1520:~/work$ sudo unzip -q fsharp.zip -d /usr/local/bin/ ben@ben-1520:~/work$ rm fsharp.zip ben@ben-1520:~/work$ cd /usr/local/bin/FSharp-2.0.0.0 ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0$ sudo chmod +x install-mono.sh ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0$ sudo wget anonsvn.mono-project.com/ source/trunk/mcs/class/mono.snk ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0$ sudo ./install-mono.sh -- Resigning FSharp.Core.dll with mono.snk Assembly bin/FSharp.Core.dll signed. -- Installing FSharp DLLS into the GAC Installed bin/FSharp.Core.dll into the gac (/usr/lib/mono/gac) ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0$ cd ~/work ben@ben-1520:~/work$ cat >> ~/.bashrc <<! > > # set PATH for Microsoft F# > if [ -d "/usr/local/bin/FSharp-2.0.0.0/bin" ] ; then > PATH="/usr/local/bin/FSharp-2.0.0.0/bin:\$PATH" > fi > ! ben@ben-1520:~/work$ exit

fsi.exe 是 F# 交互窗口,fsc.exe 是 F# 编译器:

ben@ben-1520:~/work$ fsi.exe Microsoft (R) F# 2.0 Interactive build 2.0.0.0 Copyright (c) Microsoft Corporation. All Rights Reserved. For help type #help;; > System.Environment.OSVersion;; val it : System.OperatingSystem = Unix 2.6.32.22 {Platform = Unix; ServicePack = ""; Version = 2.6.32.22; VersionString = "Unix 2.6.32.22";} > #quit;; - Exit... ben@ben-1520:~/work$ fsc.exe GregorianTest.fs Microsoft (R) F# 2.0 Compiler build 2.0.0.0 Copyright (c) Microsoft Corporation. All Rights Reserved. ben@ben-1520:~/work$ ./GregorianTest.exe 星期一 1582-10-04 星期二 1582-10-05 ben@ben-1520:~/work$

运行结果和 GregorianTest.cs 的一样。但 F# 源程序比 C# 源程序更简洁。

C

C 语言在2010年6月编程语言排行榜中排名第二位。下面就是 GregorianTest.c 程序:

01: #include <stdio.h> 02: #include <time.h> 03: 04: int main() 05: { 06: struct tm date = { 0, 0, 0, 4, 10 - 1, 1582 - 1900 }; 07: time_t seconds = mktime(&date); 08: 09: fputs(asctime(localtime(&seconds)), stdout); 10: seconds += 24 * 3600; 11: fputs(asctime(localtime(&seconds)), stdout); 12: return 0; 13: }

注意 tm 结构中年份是从 1900 年起始的,月份的取值范围是从 0 到 11。虽然 C 语言的标准库中没有计算某一日期的下一天的函数,但是通过将 seconds 变量增加 24 * 3600 秒可以达到同样的目的。

安装 C 编译器,编译和运行:

ben@ben-1520:~/work$ sudo apt-get install gcc ben@ben-1520:~/work$ gcc --version gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ben@ben-1520:~/work$ gcc -o GregorianTest GregorianTest.c ben@ben-1520:~/work$ ./GregorianTest Mon Oct 4 00:00:00 1582 Tue Oct 5 00:00:00 1582 ben@ben-1520:~/work$

运行结果和 .NET 平台的编程语言一样。

注:如果使用 Microsoft Visual Studio 2010 的 C++ 编译器,tm 结构不允许 1900 年以前的日期。

C++

C++ 语言在2010年6月编程语言排行榜中排名第三位。下面就是 GregorianTest.cpp 程序:

01: #include <iostream> 02: #include "boost/date_time/gregorian/gregorian.hpp" 03: 04: using namespace std; 05: using namespace boost::gregorian; 06: 07: void writeline(date dt); 08: 09: int main() 10: { 11: date dt(1582, 10, 4); 12: writeline(dt); 13: date_duration dd(1); 14: writeline(dt + dd); 15: } 16: 17: void writeline(date dt) 18: { 19: cout << dt.day_of_week() << " " << dt << endl; 20: }

安装 C++ 编译器、boost 库,编译和运行:

ben@ben-1520:~/work$ sudo apt-get install g++ libboost-dev ben@ben-1520:~/work$ g++ --version g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ben@ben-1520:~/work$ g++ –o GregorianTest GregorianTest.cpp ben@ben-1520:~/work$ ./GregorianTest Mon 1582-Oct-04 Tue 1582-Oct-05 ben@ben-1520:~/work$

运行结果和 .NET 平台的编程语言一样。

Python

Python 语言在2010年6月编程语言排行榜中排名第七位。下面就是 GregorianTest.py 程序:

from datetime import date, timedelta dt = date(1582, 10, 4) print dt.isoweekday(), dt.isoformat() dt = dt + timedelta(1) print dt.isoweekday(), dt.isoformat()

Ubuntu 操作系统中已经预装了 Python。可以把 python 作为交互窗口,也可以解释执行:

ben@ben-1520:~/work$ python Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.getcwd() '/home/ben/work' >>> exit() ben@ben-1520:~/work$ python GregorianTest.py 1 1582-10-04 2 1582-10-05 ben@ben-1520:~/work$

运行结果和 .NET 平台的编程语言一样。

Ruby

Ruby 语言在2010年6月编程语言排行榜中排名第十二位。下面就是 GregorianTest.rb 程序:

require 'date' dt = Date::civil(1582, 10, 4) puts dt.asctime() puts dt.next().asctime()

安装 ruby 和 irb (交互式 ruby),解释执行:

ben@ben-1520:~/work$ sudo apt-get install ruby irb ben@ben-1520:~/work$ ruby --version ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux] ben@ben-1520:~/work$ irb --version irb 0.9.5(05/04/13) ben@ben-1520:~/work$ irb irb(main):001:0> Time::now => Mon Jun 14 16:54:21 +0800 2010 irb(main):002:0> exit ben@ben-1520:~/work$ ruby GregorianTest.rb Thu Oct 4 00:00:00 1582 Fri Oct 15 00:00:00 1582

运行结果和 Java 平台的编程语言一样。

JavaScript

JavaScript 语言在2010年6月编程语言排行榜中排名第十一位。下面就是 GregorianTest.html 程序:

01: <html xmlns="www.w3.org/1999/xhtml"> 02: <head> 03: <title>Gregorian Calendar</title> 04: <script type="text/javascript"> 05: function testGregorian() 06: { 07: var date = new Date(1582, 10 - 1, 4); 08: document.write(date.toString()); 09: } 10: </script> 11: </head> 12: <body> 13: <script type="text/javascript"> 14: testGregorian(); 15: </script> 16: </body> 17: </html>

我没有在 JavaScript 语言的标准库中找到计算某一日期的下一天的函数。

在 Firefox 浏览器中的运行结果如下图所示:

运行结果和 C 语言一样。

更多的编程语言将在下一篇随笔中介绍。

参考资料
  1. Ubuntu
  2. Mono: DistroPackages/Ubuntu
  3. Mono: Visual Basic.NET support
  4. Microsoft F# April 2010 CTP
  5. GCC, the GNU Compiler Collection
  6. Boost C++ Libraries
  7. Ruby Programming Language
  8. Python Programming Language
  9. The Scala Programming Language
  10. OpenJDK

本文共计3034个文字,预计阅读时间需要13分钟。

Ubuntu中常用的编程语言有哪些?

在上篇随笔中,我尝试用+C语言实现了一个简单的计算器功能。

在上一篇随笔中,使用 C# 和 Java 语言来求解一五八二年十月四日是星期几。现在,让我们在 Ubuntu 10.04 操作系统中使用多种编程语言来求解这个问题。其中有“2010年6月编程语言排行榜”中前二十名中的:Java、C、C++、(Visual)Basic、C#、Python、JavaScript、Ruby,还有排名四十以后的:Scala、F#。下面就具体讲述如何在 Ubuntu 操作系统中安装这些编程语言。然后通过编写求解这个具体问题的程序,并编译和运行,或者解释执行,或者在交互窗口中执行,使得对这些编程语言有最初步的了解。

Java

让我们从2010年6月编程语言排行榜中的第一名 Java 开始吧。下面就是 GregorianTest.java 程序:

import java.util.*; public class GregorianTest { public static void main(String[] args) { GregorianCalendar dt = new GregorianCalendar(1582, 10 - 1, 4); System.out.println(dt.getTime()); dt.add(Calendar.DAY_OF_MONTH, 1); System.out.println(dt.getTime()); } }

注意,java.util.GregorianCalendar 类的构造函数中月份的取值范围是从 0 到 11。

安装 OpenJDK,编译和运行:

ben@ben-1520:~/work$ sudo apt-get install openjdk-6-jdk ben@ben-1520:~/work$ java -version java version "1.6.0_18" OpenJDK Runtime Environment (IcedTea6 1.8) (6b18-1.8-0ubuntu1) OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode) ben@ben-1520:~/work$ javac GregorianTest.java ben@ben-1520:~/work$ java GregorianTest Thu Oct 04 00:00:00 CST 1582 Fri Oct 15 00:00:00 CST 1582 ben@ben-1520:~/work$

可以看出,Java 语言很好地解决了这个问题:儒略历1582年10月4日星期四的下一天是格里历1582年10月15日星期五。

Scala

在2010年6月编程语言排行榜中排名四十三位的 Scala 是 Java 平台上的一门新兴的语言。老赵在“在.NET 平台上使用Scala语言(上):初尝”中说:“我非常希望它可以取代Java这种劣质语言,让Java平台的生产力上一个台阶。”

Scala 也支持 .NET 平台,请参见“也谈在 .NET 平台上使用 Scala 语言(上)”。

下面就是 GregorianTest.scala 程序:

object GregorianTest extends Application { val dt = new java.util.GregorianCalendar(1582, 10 - 1, 4) println(dt.getTime()) dt.add(java.util.Calendar.DAY_OF_MONTH, 1) println(dt.getTime()) }

安装 Scala SDK,编译和运行(scalac 是编译器,scala 用于运行编译后的程序,也可以当作交互窗口使用):

Ubuntu中常用的编程语言有哪些?

ben@ben-1520:~/work$ sudo apt-get install scala ben@ben-1520:~/work$ scala Welcome to Scala version 2.7.7final (OpenJDK 64-Bit Server VM, Java 1.6.0_18). Type in expressions to have them evaluated. Type :help for more information. scala> Math.Pi res0: Double = 3.141592653589793 scala> :quit ben@ben-1520:~/work$ scalac -version Scala compiler version 2.7.7final -- (c) 2002-2008 LAMP/EPFL ben@ben-1520:~/work$ scalac GregorianTest.scala ben@ben-1520:~/work$ scala GregorianTest Thu Oct 04 00:00:00 CST 1582 Fri Oct 15 00:00:00 CST 1582 ben@ben-1520:~/work$

由于使用 Java 平台的 java.util.GregorianCalendar 类,运行结果和 Java 语言是一样的。

Visual Basic.NET

(Visual)Basic 语言在2010年6月编程语言排行榜中排名第五位。下面就是 GregorianTest.vb 程序:

01: Module GregorianTest 02: Sub Main() 03: Dim dt As DateTime 04: dt = New DateTime(1582, 10, 4) 05: Console.WriteLine(dt.ToString("dddd yyyy-MM-dd")) 06: Console.WriteLine(dt.AddDays(1).ToString("dddd yyyy-MM-dd")) 07: End Sub 08: End Module

安装 Visual Basic.NET 编译器,编译和运行:

ben@ben-1520:~/work$ sudo apt-get install mono-vbnc ben@ben-1520:~/work$ vbnc GregorianTest.vb Visual Basic.Net Compiler version 0.0.0.5914 (Mono 2.4.2 - r) Copyright (C) 2004-2008 Rolf Bjarne Kvinge. All rights reserved. Assembly 'GregorianTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' saved successfully to '/home/ben/work/GregorianTest.exe'. Compilation successful Compilation took 00:00:01.9895840 ben@ben-1520:~/work$ ./GregorianTest.exe 星期一 1582-10-04 星期二 1582-10-05 ben@ben-1520:~/work$

在 .NET 平台中,DateTime 把格里历外推到1582年10月15日之前,取代儒略历,从而错误地认为1582年10月4日是星期一(实际上应该是星期四)。

C#

C# 语言在2010年6月编程语言排行榜中排名第六位。下面就是 GregorianTest.cs 程序:

01: using System; 02: 03: class GregorianTest 04: { 05: static void Main() 06: { 07: var dt = new DateTime(1582, 10, 4); 08: Console.WriteLine(dt.ToString("dddd yyyy-MM-dd")); 09: Console.WriteLine(dt.AddDays(1).ToString("dddd yyyy-MM-dd")); 10: } 11: }

安装 C# 编译器,编译和运行:

ben@ben-1520:~/work$ sudo apt-get install mono-devel ben@ben-1520:~/work$ gmcs --version Mono C# compiler version 2.4.4.0 ben@ben-1520:~/work$ gmcs GregorianTest.cs ben@ben-1520:~/work$ ./GregorianTest.exe 星期一 1582-10-04 星期二 1582-10-05 ben@ben-1520:~/work$

不出所料,运行结果和 Visual Basic.NET 是一样的。

.NET Framework Base Class Library 中有 System.Globalization.GregorianCalendar 类,我们来看看使用该类的 GregorianTest2.cs 程序:

01: using System; 02: using System.Globalization; 03: 04: class Program 05: { 06: static void Main() 07: { 08: var dt = new JulianCalendar().ToDateTime(1582, 10, 4, 0, 0, 0, 0); 09: Console.WriteLine(dt == new DateTime(1582, 10, 15).AddDays(-1)); 10: WriteLine(new JulianCalendar(), dt); 11: WriteLine(new GregorianCalendar(), dt.AddDays(1)); 12: } 13: 14: static void WriteLine(Calendar cal, DateTime dt) 15: { 16: Console.WriteLine("{0,-9} {1:D4}-{2:D2}-{3:D2}", 17: cal.GetDayOfWeek(dt), cal.GetYear(dt), cal.GetMonth(dt), cal.GetDayOfMonth(dt)); 18: } 19: }

该程序的运行结果如下所示:

ben@ben-1520:~/work$ gmcs GregorianTest2.cs ben@ben-1520:~/work$ ./GregorianTest2.exe True Thursday 1582-10-04 Friday 1582-10-15 ben@ben-1520:~/work$

看来如果由用户自己指定使用儒略历还是格里历,.NET 平台的 System.Globalization.Calendar 相关的类还是能够正常工作的。

F#

F# 语言在2010年6月编程语言排行榜中排名第四十五位。下面就是 GregorianTest.fs 程序:

1: let dt = System.DateTime(1582, 10, 4) 2: printfn "%s" (dt.ToString("dddd yyyy-MM-dd")) 3: printfn "%s" (dt.AddDays(1.0).ToString("dddd yyyy-MM-dd"))

安装 F# 编译器:

ben@ben-1520:~/work$ wget download.microsoft.com/ download/1/3/B/13BE2B98-E487-4032-9441-22D4D2F4FAAC/fsharp.zip ben@ben-1520:~/work$ sudo unzip -q fsharp.zip -d /usr/local/bin/ ben@ben-1520:~/work$ rm fsharp.zip ben@ben-1520:~/work$ cd /usr/local/bin/FSharp-2.0.0.0 ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0$ sudo chmod +x install-mono.sh ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0$ sudo wget anonsvn.mono-project.com/ source/trunk/mcs/class/mono.snk ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0$ sudo ./install-mono.sh -- Resigning FSharp.Core.dll with mono.snk Assembly bin/FSharp.Core.dll signed. -- Installing FSharp DLLS into the GAC Installed bin/FSharp.Core.dll into the gac (/usr/lib/mono/gac) ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0$ cd ~/work ben@ben-1520:~/work$ cat >> ~/.bashrc <<! > > # set PATH for Microsoft F# > if [ -d "/usr/local/bin/FSharp-2.0.0.0/bin" ] ; then > PATH="/usr/local/bin/FSharp-2.0.0.0/bin:\$PATH" > fi > ! ben@ben-1520:~/work$ exit

fsi.exe 是 F# 交互窗口,fsc.exe 是 F# 编译器:

ben@ben-1520:~/work$ fsi.exe Microsoft (R) F# 2.0 Interactive build 2.0.0.0 Copyright (c) Microsoft Corporation. All Rights Reserved. For help type #help;; > System.Environment.OSVersion;; val it : System.OperatingSystem = Unix 2.6.32.22 {Platform = Unix; ServicePack = ""; Version = 2.6.32.22; VersionString = "Unix 2.6.32.22";} > #quit;; - Exit... ben@ben-1520:~/work$ fsc.exe GregorianTest.fs Microsoft (R) F# 2.0 Compiler build 2.0.0.0 Copyright (c) Microsoft Corporation. All Rights Reserved. ben@ben-1520:~/work$ ./GregorianTest.exe 星期一 1582-10-04 星期二 1582-10-05 ben@ben-1520:~/work$

运行结果和 GregorianTest.cs 的一样。但 F# 源程序比 C# 源程序更简洁。

C

C 语言在2010年6月编程语言排行榜中排名第二位。下面就是 GregorianTest.c 程序:

01: #include <stdio.h> 02: #include <time.h> 03: 04: int main() 05: { 06: struct tm date = { 0, 0, 0, 4, 10 - 1, 1582 - 1900 }; 07: time_t seconds = mktime(&date); 08: 09: fputs(asctime(localtime(&seconds)), stdout); 10: seconds += 24 * 3600; 11: fputs(asctime(localtime(&seconds)), stdout); 12: return 0; 13: }

注意 tm 结构中年份是从 1900 年起始的,月份的取值范围是从 0 到 11。虽然 C 语言的标准库中没有计算某一日期的下一天的函数,但是通过将 seconds 变量增加 24 * 3600 秒可以达到同样的目的。

安装 C 编译器,编译和运行:

ben@ben-1520:~/work$ sudo apt-get install gcc ben@ben-1520:~/work$ gcc --version gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ben@ben-1520:~/work$ gcc -o GregorianTest GregorianTest.c ben@ben-1520:~/work$ ./GregorianTest Mon Oct 4 00:00:00 1582 Tue Oct 5 00:00:00 1582 ben@ben-1520:~/work$

运行结果和 .NET 平台的编程语言一样。

注:如果使用 Microsoft Visual Studio 2010 的 C++ 编译器,tm 结构不允许 1900 年以前的日期。

C++

C++ 语言在2010年6月编程语言排行榜中排名第三位。下面就是 GregorianTest.cpp 程序:

01: #include <iostream> 02: #include "boost/date_time/gregorian/gregorian.hpp" 03: 04: using namespace std; 05: using namespace boost::gregorian; 06: 07: void writeline(date dt); 08: 09: int main() 10: { 11: date dt(1582, 10, 4); 12: writeline(dt); 13: date_duration dd(1); 14: writeline(dt + dd); 15: } 16: 17: void writeline(date dt) 18: { 19: cout << dt.day_of_week() << " " << dt << endl; 20: }

安装 C++ 编译器、boost 库,编译和运行:

ben@ben-1520:~/work$ sudo apt-get install g++ libboost-dev ben@ben-1520:~/work$ g++ --version g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ben@ben-1520:~/work$ g++ –o GregorianTest GregorianTest.cpp ben@ben-1520:~/work$ ./GregorianTest Mon 1582-Oct-04 Tue 1582-Oct-05 ben@ben-1520:~/work$

运行结果和 .NET 平台的编程语言一样。

Python

Python 语言在2010年6月编程语言排行榜中排名第七位。下面就是 GregorianTest.py 程序:

from datetime import date, timedelta dt = date(1582, 10, 4) print dt.isoweekday(), dt.isoformat() dt = dt + timedelta(1) print dt.isoweekday(), dt.isoformat()

Ubuntu 操作系统中已经预装了 Python。可以把 python 作为交互窗口,也可以解释执行:

ben@ben-1520:~/work$ python Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.getcwd() '/home/ben/work' >>> exit() ben@ben-1520:~/work$ python GregorianTest.py 1 1582-10-04 2 1582-10-05 ben@ben-1520:~/work$

运行结果和 .NET 平台的编程语言一样。

Ruby

Ruby 语言在2010年6月编程语言排行榜中排名第十二位。下面就是 GregorianTest.rb 程序:

require 'date' dt = Date::civil(1582, 10, 4) puts dt.asctime() puts dt.next().asctime()

安装 ruby 和 irb (交互式 ruby),解释执行:

ben@ben-1520:~/work$ sudo apt-get install ruby irb ben@ben-1520:~/work$ ruby --version ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux] ben@ben-1520:~/work$ irb --version irb 0.9.5(05/04/13) ben@ben-1520:~/work$ irb irb(main):001:0> Time::now => Mon Jun 14 16:54:21 +0800 2010 irb(main):002:0> exit ben@ben-1520:~/work$ ruby GregorianTest.rb Thu Oct 4 00:00:00 1582 Fri Oct 15 00:00:00 1582

运行结果和 Java 平台的编程语言一样。

JavaScript

JavaScript 语言在2010年6月编程语言排行榜中排名第十一位。下面就是 GregorianTest.html 程序:

01: <html xmlns="www.w3.org/1999/xhtml"> 02: <head> 03: <title>Gregorian Calendar</title> 04: <script type="text/javascript"> 05: function testGregorian() 06: { 07: var date = new Date(1582, 10 - 1, 4); 08: document.write(date.toString()); 09: } 10: </script> 11: </head> 12: <body> 13: <script type="text/javascript"> 14: testGregorian(); 15: </script> 16: </body> 17: </html>

我没有在 JavaScript 语言的标准库中找到计算某一日期的下一天的函数。

在 Firefox 浏览器中的运行结果如下图所示:

运行结果和 C 语言一样。

更多的编程语言将在下一篇随笔中介绍。

参考资料
  1. Ubuntu
  2. Mono: DistroPackages/Ubuntu
  3. Mono: Visual Basic.NET support
  4. Microsoft F# April 2010 CTP
  5. GCC, the GNU Compiler Collection
  6. Boost C++ Libraries
  7. Ruby Programming Language
  8. Python Programming Language
  9. The Scala Programming Language
  10. OpenJDK