找回密码
 立即注册
首页 业界区 业界 Ubuntu 中的编程语言(下)

Ubuntu 中的编程语言(下)

丘奕奕 2025-5-29 19:44:51
在上一篇随笔中介绍了四种编程语言。这次再介绍四种编程语言:Fortran、Lua、Lisp 和 Logo。
Fortran

Fortran 语言在2010年6月编程语言排行榜中排名第三十一位。下面就是 GregorianTest.for 程序:
1.png

我没有在 Fortran 语言的标准库中找到设置指定日期的函数,只好从 1970-01-01 往回倒数 141,438 天得到 1582-10-04 。
安装 GNU Fortran 编译器,编译和运行:
  1. ben@ben-1520:~/work$ <strong>sudo apt-get install gfortran</strong>
  2. ben@ben-1520:~/work$ <strong>gfortran --version</strong>
  3. GNU Fortran (Ubuntu 4.4.3-4ubuntu5) 4.4.3
  4. Copyright (C) 2010 Free Software Foundation, Inc.
  5. GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
  6. You may redistribute copies of GNU Fortran
  7. under the terms of the GNU General Public License.
  8. For more information about these matters, see the file named COPYING
  9. ben@ben-1520:~/work$ <strong>gfortran GregorianTest.for</strong>
  10. ben@ben-1520:~/work$ <strong>./a.out</strong>
  11. <font color="#ff0000">Mon Oct  4 08:05:52 1582
  12. Tue Oct  5 08:05:52 1582</font>
  13. ben@ben-1520:~/work$
复制代码
运行结果和 C 语言一样。
Lua

Lua 语言在2010年6月编程语言排行榜中排名第十七位。下面就是 GregorianTest.lua 程序:
2.png
 
安装 Lua 软件包。luac 是编译器,用于将源程序编译为字节码,默认的文件名是 luac.out,luac –l 可以查看字节码。lua 可以作为交互窗口,也可以解释执行 lua 源程序,还可以用于运行编译后的字节码:
  1. ben@ben-1520:~/work$ <strong>sudo apt-get install lua5.1</strong>
  2. ben@ben-1520:~/work$ <strong>lua</strong>
  3. Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
  4. > <strong>print(math.pi)</strong>
  5. 3.1415926535898
  6. > <strong>(^D)</strong>
  7. ben@ben-1520:~/work$ <strong>lua GregorianTest.lua</strong>
  8. <font color="#ff0000">Mon 1582-10-04
  9. Tue 1582-10-05</font>
  10. ben@ben-1520:~/work$ <strong>luac GregorianTest.lua</strong>
  11. ben@ben-1520:~/work$ <strong>luac -l</strong>
  12. main <GregorianTest.lua:0,0> (24 instructions, 96 bytes at 0x17bf530)
  13. 0+ params, 4 slots, 0 upvalues, 0 locals, 13 constants, 0 functions
  14.         1        [1]        GETGLOBAL        0 -2        ; os
  15.         2        [1]        GETTABLE         0 0 -3        ; "time"
  16.         3        [1]        NEWTABLE         1 0 3
  17.         4        [1]        SETTABLE         1 -4 -5        ; "year" 1582
  18.         5        [1]        SETTABLE         1 -6 -7        ; "month" 10
  19.         6        [1]        SETTABLE         1 -8 -9        ; "day" 4
  20.         7        [1]        CALL             0 2 2
  21.         8        [1]        SETGLOBAL        0 -1        ; dt
  22.         9        [2]        GETGLOBAL        0 -10        ; print
  23.         10        [2]        GETGLOBAL        1 -2        ; os
  24.         11        [2]        GETTABLE         1 1 -11        ; "date"
  25.         12        [2]        LOADK            2 -12        ; "%a %F"
  26.         13        [2]        GETGLOBAL        3 -1        ; dt
  27.         14        [2]        CALL             1 3 0
  28.         15        [2]        CALL             0 0 1
  29.         16        [3]        GETGLOBAL        0 -10        ; print
  30.         17        [3]        GETGLOBAL        1 -2        ; os
  31.         18        [3]        GETTABLE         1 1 -11        ; "date"
  32.         19        [3]        LOADK            2 -12        ; "%a %F"
  33.         20        [3]        GETGLOBAL        3 -1        ; dt
  34.         21        [3]        ADD              3 3 -13        ; - 86400
  35.         22        [3]        CALL             1 3 0
  36.         23        [3]        CALL             0 0 1
  37.         24        [3]        RETURN           0 1
  38. ben@ben-1520:~/work$ <strong>lua luac.out</strong>
  39. <font color="#ff0000">Mon 1582-10-04
  40. Tue 1582-10-05</font>
  41. ben@ben-1520:~/work$
复制代码
运行结果和 C 语言一样。
Lisp

Lisp 语言在2010年6月编程语言排行榜中排名第十六位。下面就是 GregorianTest.lisp 程序:
3.png
 
安装 GNU Common Lisp 软件包,gcl 可以作为交互窗口,也可编译源程序(使用 –compile 参数),还可以解释执行(使用 –f 参数):
  1. ben@ben-1520:~/work$ <strong>sudo apt-get install gcl</strong>
  2. ben@ben-1520:~/work$ <strong>gcl</strong>
  3. GCL (GNU Common Lisp)  2.6.7 CLtL1    Feb 15 2010 17:57:54
  4. Source License: LGPL(gcl,gmp), GPL(unexec,bfd,xgcl)
  5. Binary License:  GPL due to GPL'ed components: (XGCL READLINE BFD UNEXEC)
  6. Modifications of this banner must retain notice of a compatible license
  7. Dedicated to the memory of W. Schelter
  8. Use (help) to get some basic information on how to use GCL.
  9. Temporary directory for compiler files set to /tmp/
  10. ><strong>(bye)</strong>
  11. ben@ben-1520:~/work$ <strong>gcl -f GregorianTest.lisp</strong>
  12. <font color="#ff0000">Mon 1583-01--88  seconds since 1900-01-01: -10011254400
  13. Tue 1583-01--87  seconds since 1900-01-01: -10011168000</font>
  14. ben@ben-1520:~/work$
复制代码
运行结果基本和 C 语言一样,但是有一个 bug,认为 1582-10-04 是1583年1月的第 –88 天。这是一个很奇怪的 bug,其它大部分日期是正常的,如下所示:
  1. ben@ben-1520:~/work$ <strong>gcl -f GregorianTest2.lisp</strong>
  2. Sun 2010-06-20  seconds since 1900-01-01: 3485980800
  3. Mon 2010-06-21  seconds since 1900-01-01: 3486067200
  4. ben@ben-1520:~/work$ <strong>gcl -f GregorianTest3.lisp</strong>
  5. Tue 1582-05-04  seconds since 1900-01-01: -10024473600
  6. Wed 1582-05-05  seconds since 1900-01-01: -10024387200
  7. ben@ben-1520:~/work$
复制代码
此外,还可以选择安装 GNU CLISP 软件包,clisp 是符合 ANSI Common Lisp 标准的编译器、解释器和调试器:
  1. ben@ben-1520:~/work$ <strong>sudo apt-get install clisp</strong>
  2. ben@ben-1520:~/work$ <strong>clisp</strong>
  3.   i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo
  4.   I I I I I I I      8     8   8           8     8     o  8    8
  5.   I  \ `+' /  I      8         8           8     8        8    8
  6.    \  `-+-'  /       8         8           8      ooooo   8oooo
  7.     `-__|__-'        8         8           8           8  8
  8.         |            8     o   8           8     o     8  8
  9.   ------+------       ooooo    8oooooo  ooo8ooo   ooooo   8
  10. Welcome to GNU CLISP 2.44.1 (2008-02-23) <http: clisp.cons.org="">
  11. Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
  12. Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
  13. Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
  14. Copyright (c) Bruno Haible, Sam Steingold 1999-2000
  15. Copyright (c) Sam Steingold, Bruno Haible 2001-2008
  16. Type :h and hit Enter for context help.
  17. [1]> <strong>(bye)</strong>
  18. Bye.
  19. ben@ben-1520:~/work$ <strong>clisp GregorianTest2.lisp</strong>
  20. Sun 2010-06-20  seconds since 1900-01-01: 3485952000
  21. Mon 2010-06-21  seconds since 1900-01-01: 3486038400
  22. ben@ben-1520:~/work$ <strong>clisp GregorianTest.lisp</strong>
  23. <font color="#ff0000">*** - incorrect date: 1582-10-4 0:0:0, time zone NIL</font>
  24. ben@ben-1520:~/work$
  25. </http:>
复制代码
可以看出,GNU CLISP 不支持 1900-01-01 以前的日期。
Logo

在2010年6月编程语言排行榜中排名第三十六位的 Logo 语言的原型来自 Lisp 语言,内置一套海龟绘图系统,很适合于儿童学习。下面是一个用海龟绘图的 mn_eck.logo 程序:
  1. 1:  to n_eck :ne :sz
  2. 2:    repeat :ne [rt 360 / :ne fd :sz]
  3. 3:  end
  4. 4:  
  5. 5:  to mn_eck :ne :sz
  6. 6:    repeat :ne [rt 360 / :ne n_eck :ne :sz]
  7. 7:  end
  8. 8:  
  9. 9:  mn_eck 36 20
复制代码
安装 UC Berkeley Logo 软件包,启动交互窗口:
  1. ben@ben-1520:~/work$ <strong>sudo apt-get install ucblogo</strong>
  2. ben@ben-1520:~/work$ <strong>logo</strong>
  3. Welcome to Berkeley Logo version 5.5
  4. ? <strong>print exp 1</strong>
  5. 2.71828182845905
  6. ? <strong>load "mn_eck.logo</strong>
  7. ? <strong>bye</strong>
  8. Thank you for using Logo.
  9. Have a nice day.
  10. ben@ben-1520:~/work$
复制代码
运行结果如下所示:
4.png
 
我没有在 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)

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册