在上一篇随笔中介绍了四种编程语言。这次再介绍四种编程语言:Fortran、Lua、Lisp 和 Logo。
Fortran
Fortran 语言在2010年6月编程语言排行榜中排名第三十一位。下面就是 GregorianTest.for 程序:
我没有在 Fortran 语言的标准库中找到设置指定日期的函数,只好从 1970-01-01 往回倒数 141,438 天得到 1582-10-04 。
安装 GNU Fortran 编译器,编译和运行:
- ben@ben-1520:~/work$ <strong>sudo apt-get install gfortran</strong>
- ben@ben-1520:~/work$ <strong>gfortran --version</strong>
- GNU Fortran (Ubuntu 4.4.3-4ubuntu5) 4.4.3
- Copyright (C) 2010 Free Software Foundation, Inc.
- GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
- You may redistribute copies of GNU Fortran
- under the terms of the GNU General Public License.
- For more information about these matters, see the file named COPYING
- ben@ben-1520:~/work$ <strong>gfortran GregorianTest.for</strong>
- ben@ben-1520:~/work$ <strong>./a.out</strong>
- <font color="#ff0000">Mon Oct 4 08:05:52 1582
- Tue Oct 5 08:05:52 1582</font>
- ben@ben-1520:~/work$
复制代码 运行结果和 C 语言一样。
Lua
Lua 语言在2010年6月编程语言排行榜中排名第十七位。下面就是 GregorianTest.lua 程序:
安装 Lua 软件包。luac 是编译器,用于将源程序编译为字节码,默认的文件名是 luac.out,luac –l 可以查看字节码。lua 可以作为交互窗口,也可以解释执行 lua 源程序,还可以用于运行编译后的字节码:- ben@ben-1520:~/work$ <strong>sudo apt-get install lua5.1</strong>
- ben@ben-1520:~/work$ <strong>lua</strong>
- Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
- > <strong>print(math.pi)</strong>
- 3.1415926535898
- > <strong>(^D)</strong>
- ben@ben-1520:~/work$ <strong>lua GregorianTest.lua</strong>
- <font color="#ff0000">Mon 1582-10-04
- Tue 1582-10-05</font>
- ben@ben-1520:~/work$ <strong>luac GregorianTest.lua</strong>
- ben@ben-1520:~/work$ <strong>luac -l</strong>
- main <GregorianTest.lua:0,0> (24 instructions, 96 bytes at 0x17bf530)
- 0+ params, 4 slots, 0 upvalues, 0 locals, 13 constants, 0 functions
- 1 [1] GETGLOBAL 0 -2 ; os
- 2 [1] GETTABLE 0 0 -3 ; "time"
- 3 [1] NEWTABLE 1 0 3
- 4 [1] SETTABLE 1 -4 -5 ; "year" 1582
- 5 [1] SETTABLE 1 -6 -7 ; "month" 10
- 6 [1] SETTABLE 1 -8 -9 ; "day" 4
- 7 [1] CALL 0 2 2
- 8 [1] SETGLOBAL 0 -1 ; dt
- 9 [2] GETGLOBAL 0 -10 ; print
- 10 [2] GETGLOBAL 1 -2 ; os
- 11 [2] GETTABLE 1 1 -11 ; "date"
- 12 [2] LOADK 2 -12 ; "%a %F"
- 13 [2] GETGLOBAL 3 -1 ; dt
- 14 [2] CALL 1 3 0
- 15 [2] CALL 0 0 1
- 16 [3] GETGLOBAL 0 -10 ; print
- 17 [3] GETGLOBAL 1 -2 ; os
- 18 [3] GETTABLE 1 1 -11 ; "date"
- 19 [3] LOADK 2 -12 ; "%a %F"
- 20 [3] GETGLOBAL 3 -1 ; dt
- 21 [3] ADD 3 3 -13 ; - 86400
- 22 [3] CALL 1 3 0
- 23 [3] CALL 0 0 1
- 24 [3] RETURN 0 1
- ben@ben-1520:~/work$ <strong>lua luac.out</strong>
- <font color="#ff0000">Mon 1582-10-04
- Tue 1582-10-05</font>
- ben@ben-1520:~/work$
复制代码 运行结果和 C 语言一样。
Lisp
Lisp 语言在2010年6月编程语言排行榜中排名第十六位。下面就是 GregorianTest.lisp 程序:
安装 GNU Common Lisp 软件包,gcl 可以作为交互窗口,也可编译源程序(使用 –compile 参数),还可以解释执行(使用 –f 参数):- ben@ben-1520:~/work$ <strong>sudo apt-get install gcl</strong>
- ben@ben-1520:~/work$ <strong>gcl</strong>
- GCL (GNU Common Lisp) 2.6.7 CLtL1 Feb 15 2010 17:57:54
- Source License: LGPL(gcl,gmp), GPL(unexec,bfd,xgcl)
- Binary License: GPL due to GPL'ed components: (XGCL READLINE BFD UNEXEC)
- Modifications of this banner must retain notice of a compatible license
- Dedicated to the memory of W. Schelter
- Use (help) to get some basic information on how to use GCL.
- Temporary directory for compiler files set to /tmp/
- ><strong>(bye)</strong>
- ben@ben-1520:~/work$ <strong>gcl -f GregorianTest.lisp</strong>
- <font color="#ff0000">Mon 1583-01--88 seconds since 1900-01-01: -10011254400
- Tue 1583-01--87 seconds since 1900-01-01: -10011168000</font>
- ben@ben-1520:~/work$
复制代码 运行结果基本和 C 语言一样,但是有一个 bug,认为 1582-10-04 是1583年1月的第 –88 天。这是一个很奇怪的 bug,其它大部分日期是正常的,如下所示:- ben@ben-1520:~/work$ <strong>gcl -f GregorianTest2.lisp</strong>
- Sun 2010-06-20 seconds since 1900-01-01: 3485980800
- Mon 2010-06-21 seconds since 1900-01-01: 3486067200
- ben@ben-1520:~/work$ <strong>gcl -f GregorianTest3.lisp</strong>
- Tue 1582-05-04 seconds since 1900-01-01: -10024473600
- Wed 1582-05-05 seconds since 1900-01-01: -10024387200
- ben@ben-1520:~/work$
复制代码 此外,还可以选择安装 GNU CLISP 软件包,clisp 是符合 ANSI Common Lisp 标准的编译器、解释器和调试器:- ben@ben-1520:~/work$ <strong>sudo apt-get install clisp</strong>
- ben@ben-1520:~/work$ <strong>clisp</strong>
- i i i i i i i ooooo o ooooooo ooooo ooooo
- I I I I I I I 8 8 8 8 8 o 8 8
- I \ `+' / I 8 8 8 8 8 8
- \ `-+-' / 8 8 8 ooooo 8oooo
- `-__|__-' 8 8 8 8 8
- | 8 o 8 8 o 8 8
- ------+------ ooooo 8oooooo ooo8ooo ooooo 8
- Welcome to GNU CLISP 2.44.1 (2008-02-23) <http: clisp.cons.org="">
- Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
- Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
- Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
- Copyright (c) Bruno Haible, Sam Steingold 1999-2000
- Copyright (c) Sam Steingold, Bruno Haible 2001-2008
- Type :h and hit Enter for context help.
- [1]> <strong>(bye)</strong>
- Bye.
- ben@ben-1520:~/work$ <strong>clisp GregorianTest2.lisp</strong>
- Sun 2010-06-20 seconds since 1900-01-01: 3485952000
- Mon 2010-06-21 seconds since 1900-01-01: 3486038400
- ben@ben-1520:~/work$ <strong>clisp GregorianTest.lisp</strong>
- <font color="#ff0000">*** - incorrect date: 1582-10-4 0:0:0, time zone NIL</font>
- ben@ben-1520:~/work$
- </http:>
复制代码 可以看出,GNU CLISP 不支持 1900-01-01 以前的日期。
Logo
在2010年6月编程语言排行榜中排名第三十六位的 Logo 语言的原型来自 Lisp 语言,内置一套海龟绘图系统,很适合于儿童学习。下面是一个用海龟绘图的 mn_eck.logo 程序:- 1: to n_eck :ne :sz
- 2: repeat :ne [rt 360 / :ne fd :sz]
- 3: end
- 4:
- 5: to mn_eck :ne :sz
- 6: repeat :ne [rt 360 / :ne n_eck :ne :sz]
- 7: end
- 8:
- 9: mn_eck 36 20
复制代码 安装 UC Berkeley Logo 软件包,启动交互窗口:- ben@ben-1520:~/work$ <strong>sudo apt-get install ucblogo</strong>
- ben@ben-1520:~/work$ <strong>logo</strong>
- Welcome to Berkeley Logo version 5.5
- ? <strong>print exp 1</strong>
- 2.71828182845905
- ? <strong>load "mn_eck.logo</strong>
- ? <strong>bye</strong>
- Thank you for using Logo.
- Have a nice day.
- ben@ben-1520:~/work$
复制代码 运行结果如下所示:
我没有在 Logo 语言中找到有关日期方面的函数。如果有哪们朋友知道的话,请在评论中告诉我。谢谢!
总结
儒略历1582年10月4日星期四的下一天是格里历1582年10月15日星期五。能够正确处理的语言有:
- Java (使用 java.util.GregorianCalendar 类)
- Scala (使用 java.util.GregorianCalendar 类)
- Ruby (使用 Date 类)
- Visual Basic.NET (使用 System.Globalization.Calendar 相关的类,必须由用户自己指定使用儒略历还是格里历)
- C# (使用 System.Globalization.Calendar 相关的类,必须由用户自己指定使用儒略历还是格里历)
- F# (使用 System.Globalization.Calendar 相关的类,必须由用户自己指定使用儒略历还是格里历)
- PHP (使用 cal_to_jd 和 cal_from_jd 函数,必须由用户自己指定使用儒略历还是格里历)
把格里历外推到1582年10月15日之前,取代儒略历,从而认为1582年10月4日是星期一的语言有:
- Visual Basic.NET (使用 System.DateTime 类)
- C# (使用 System.DateTime 类)
- F# (使用 System.DateTime 类)
- C (GNU C 编译器,使用 tm 结构和 mktime 函数)
- C++ (使用 boost::gregorian::date 和 date_duration 类)
- Python (使用 date 和 timedelta 类)
- JavaScript (使用 Date 类)
- Perl (使用核心模块的 localtime 和 timelocal 函数,或者使用 DateTime 模块)
- Fortran (使用 ctime 函数,必须由用户自己从 1970-01-01 倒数 141,438 天得到 1582-10-04)
- Lua (使用 os.time 和 os.date 函数)
- Lisp (使用 GNU Common Lisp 软件包,使用 encode-universal-time 和 decode-universal 函数)
认为1582年10月有4日是星期二或者星期六(很奇怪的决定)的语言有:
- PHP (使用 DateTime 类)
- Delphi (使用 Free Pascal 编译器,使用 TTimeStamp 类型和 EnCodeDate、DateTimeToTimeStamp 函数)
标准库不支持1582年10月4日,或者无法计算某一日期是星期几的语言有:
- C (Microsoft Visual Studio 2010 的 C++ 编译器,使用 tm 结构和 mktime 函数)
- Pascal (使用 GNU Pascal 编译器,使用 TimeStamp 类型和 GetTimeStamp 函数)
- Lisp (使用 GNU CLISP 软件包,使用 encode-universal-time 和 decode-universal 函数)
- Logo (使用 UC Berkeley Logo 软件包,没有找到有关日期的函数)
参考资料
- GNU Fortran
- GNU Fortran: CTIME: Convert a time into a string
- The Programming Language Lua
- Programming in Lua: Date and Time
- Wikipedia: Lisp (programming language)
- The Common Lisp Cookbook – Dates and Times
- Wikipedia: Logo (programming language)
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |