<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="http://blog.bs2.to/styles/rss.css" type="text/css"?>
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
>
 <channel>
  <title>LGJ Notes</title>
  <link>http://blog.bs2.to/EdwardLee</link>
  <description>蒙古大夫 2.0。
這裡紀錄的都不是我的專業，我的專業就是不專業。別傻了，什麼是專業？</description>
  <pubDate>Thu, 18 Mar 2010 18:28:04 +0800</pubDate>
  <generator>http://blog.bs2.to</generator>
        <item>
   <title>OCaml 的 Hello world</title>
   <description>
    每種程式語言的第一個展示的程式，幾乎就是 Hello World。我不清楚這個歷史淵源，不過，管他的，反正總是要有第一個，不是嗎？而且，這只不過是個破題且借題發揮的手段。XD&lt;p&gt;&lt;br/&gt;要提 hello world 之前，首先先提一下 OCaml 的 interactive mode，就是執行 &lt;tt&gt;ocaml&lt;/tt&gt; 這個執行檔所形成和使用者互動的狀態，這時可以馬上鍵入 OCaml 的 expression/phrase 來測試他的結果，和寫成程式碼，編譯出來的結果「幾乎」是一樣的，對於初接觸的人，想對 OCaml 的運作多一些瞭解，這個互動模式是個不錯的開始，因為他除了會馬上反應出結果出來，也對各種型別的轉變有所交待。&lt;p&gt;

在 OCaml 互動模式裡頭的 hello world，那就太簡單了：
&lt;pre&gt;
[edt1023@lgj ~]$ ocaml
        Objective Caml version 3.09.3

# print_string &quot;Hell, World!&quot;;;
Hell, World!- : unit = ()
#
&lt;/pre&gt;
這樣就完成了。他的傳回值其實是 unit（就是 &lt;tt&gt;()&lt;/tt&gt;，等同於 C 的 &lt;tt&gt;void&lt;/tt&gt;），印出的字串，是他的 side effect 所造成的。當然，&lt;tt&gt;print_string&lt;/tt&gt; 並不會送出換行字元（&lt;tt&gt;\n&lt;/tt&gt;），所以，可以改用 &lt;tt&gt;print_endline&lt;/tt&gt;：
&lt;pre&gt;
# print_endline &quot;Hello, World!&quot;;;
Hello, World!
- : unit = ()
#
&lt;/pre&gt;
或者，你也可以在 &lt;tt&gt;print_string&lt;/tt&gt; 後另加一行 &lt;tt&gt;print_char &#039;\n&#039;&lt;/tt&gt; 也是一樣。&lt;p&gt;

再來就是看怎麼把寫成的程式碼拿來編譯成可執行檔：
&lt;pre&gt;
(*
  vim:ts=2 sw=2 et
  hello-text.ml :
  Edward G.J. Lee (12/27/06)
*)

print_string &quot;Hello World!&quot;;
print_char &#039;\n&#039;;;
(* 或 print_endline ();; 一行就解決了 *)
&lt;/pre&gt;
這裡要說明的是，&lt;tt&gt;(*……*)&lt;/tt&gt; 是註解，幾個 expression 可以組成一個 phrase，
各 expression 後以一個分號 &lt;tt&gt;;&lt;/tt&gt; 結束，一整個 phrase 則以雙分號 &lt;tt&gt;;;&lt;/tt&gt; 來結束。簡單的說，phrase 才是一個完整功能的句子，expression 只是單一的表示式（函數，functional language 是以 function 為處理單位的，連傳回的值本身也是 function），如果搞不清楚，那通通給他以雙分號結束也是可以的。&lt;p&gt;

將上面的程式碼存檔成 &lt;tt&gt;hello-text.ml&lt;/tt&gt;，接下來就要開始編譯。在 OCaml 有兩個編譯器，一個是 bytecode compiler，編譯出來的可執行檔，檔案較小，但執行速度較慢。另一種是 native-binary compiler，這會編譯成和系統相依的原生可執行檔，檔案會大一些，但執行速度會快一點，而且，無需安裝 OCaml 就可以在同一平台上執行：
&lt;pre&gt;
bytecode compiler:
ocamlc -o hello-text hello-text.ml
native-binary compiler:
ocamlopt -o hello-text hello-text.ml
&lt;/pre&gt;
然後就可以執行 &lt;tt&gt;hello-text&lt;/tt&gt; 這個可執行檔了，玩玩看吧！&lt;p&gt;

這樣的 hello world 好像太「純潔」了一點？我們用 OCaml 內建的繪圖 module 來試看看，順便看怎麼在 OCaml 使用 module：
&lt;pre&gt;
(*
  vim:ts=2 sw=2 et
  hello-graph.ml :
  ocamlc graphics.cma hello-graph.ml -o hello-graph
  ocamlopt graphics.cmxa hello-graph.ml -o hello-graph
  Edward G.J. Lee (12/27/06)
*)

open Graphics;;
open_graph &quot; 640x480&quot;;;

set_color red;
set_font &quot;-*-*-*-r-*--82-*-*-*-p-*-iso8859-1&quot;;
moveto 100 200;
draw_string (&quot;Hello World!&quot;);
print_endline &quot;Press any key to stop...&quot;;
read_line ();;
&lt;/pre&gt;
編譯的方法已經寫在註解裡頭，這是典型使用模組（module）的方法。不使用 &lt;tt&gt;open&lt;/tt&gt;，寫成以下的方式也是一樣：
&lt;pre&gt;
Graphics.open_graph &quot; 640x480&quot;;;

Graphics.set_color red;
Graphics.set_font &quot;-*-*-*-r-*--82-*-*-*-p-*-iso8859-1&quot;;
Graphics.moveto 100 200;
Graphics.draw_string (&quot;Hello World!&quot;);
print_endline &quot;Press any key to stop...&quot;;
read_line ();;
&lt;/pre&gt;
不過，這樣會變得很煩，尤其是模組名稱很長的時候，這時也可以使用 alias 來簡化：
&lt;pre&gt;
module Gr = Graphics;;
Gr.open_graph &quot; 640x480&quot;;;

Gr.set_color red;
Gr.set_font &quot;-*-*-*-r-*--82-*-*-*-p-*-iso8859-1&quot;;
Gr.moveto 100 200;
Gr.draw_string (&quot;Hello World!&quot;);
print_endline &quot;Press any key to stop...&quot;;
read_line ();;
&lt;/pre&gt;
以下是執行結果（這是在 FreeBSD X Window 編譯、執行的，所以是依 X 的取字方式）：&lt;p&gt;

&lt;center&gt;
&lt;a href=&quot;http://pic.bs2.to/db/e/edwardlee/C329CBA3/B254AC20B5.png&quot;&gt;
&lt;img src=&quot;http://pic.bs2.to/db/e/edwardlee/C329CBA3/B254AC20B5.png&quot; width=&quot;80%&quot; height=&quot;80%&quot; alt=&quot;hello-graph 圖示&quot; title=&quot;hello-graph 圖示；click 放大&quot;&gt;
&lt;/center&gt;&lt;/a&gt;&lt;p&gt;
   </description>
   <link>http://blog.bs2.to/post/EdwardLee/7293</link>
   <comments>http://blog.bs2.to/post/EdwardLee/7293</comments>
   <guid>http://blog.bs2.to/post/EdwardLee/7293</guid>
      <dc:creator>EdwardLee</dc:creator>
      
    <category>OCaml 筆記</category>
         <pubDate>Wed, 27 Dec 2006 21:33:36 +0800</pubDate>
   <source url="http://blog.bs2.to/rss/rss20/EdwardLee">LGJ Notes</source>
     </item>
          <item>
   <title>OCaml 初探</title>
   <description>
    程式語言百百種，各有支持者，互相攻擊、爭辯哪種語言較好也是層出不窮，但終究是沒有個確切的結論，原因，可能是每個人所好及所要用的領域不同的關係？這個我不清楚，我不是學這方面的，既然學有專精的各位專家們都辯得一塌糊塗了，這表示，這不關我事，我只要選個適合我用，而且能用的程式語言來用就成了。&lt;p&gt;&lt;br/&gt;據說（我不是這方面的專家，所以，只能「據說」），&lt;a href=&quot;http://caml.inria.fr/ocaml/&quot;&gt;OCaml&lt;/a&gt;
是
&lt;a href=&quot;http://en.wikipedia.org/wiki/Functional_language&quot;&gt;functional language&lt;/a&gt; 的一種。而 functional language 大約有三種特性：
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/First_class_function&quot;&gt;first-class function&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;將 &lt;a href=&quot;http://en.wikipedia.org/wiki/Side_effect_%28programming%29&quot;&gt;side effect&lt;/a&gt; 降至最低程度。據說這樣比較容易預測結果。&lt;/li&gt;
&lt;li&gt;immutable data structure&lt;/li&gt;
&lt;/ul&gt;
請自行連結到維基百科去看一看。OCaml 並不是純的 functional language，所以，上面的一些特性，有些地方會打破，不過，有些很難避免，例如 IO（input/output）就不容易沒有 side effect 來實作。&lt;p&gt;

除了上面的特性外，OCaml 也是一種
&lt;a href=&quot;http://en.wikipedia.org/wiki/Static-typing_%28programming_languages%29#Static_and_dynamic_typing&quot;&gt;statically typed&lt;/a&gt;（強型別）的語言。我們可以由 OCaml 的 interactive mode 來測試一下就知道了：
&lt;pre&gt;
[edt1023@lgj ~]$ ocaml
        Objective Caml version 3.09.3

# let x = 3 + 3.0 ;;
This expression has type float but is here used with type int
# #quit;;
[edt1023@lgj ~]$
&lt;/pre&gt;
3 是整數，3.0 是浮點數，兩者型別不同，不能直接相加，而且 &lt;tt&gt;+&lt;/tt&gt; 是給整數運算用的，浮點數運算要使用 &lt;tt&gt;+.&lt;/tt&gt;。&lt;p&gt;

所以，OCaml 的型別「審判」是在編譯時期就決定的，型別不合，編譯就不會過，能編譯通過，代表不會有型別上的錯誤。也就是說 OCaml 還是一個很「龜毛」的程式語言，得規規矩矩的寫才讓你通過編譯，一不小心就會打回票。這和一般傳統的程式語言是在 run time 才決定型別不同。&lt;p&gt;

光 &lt;tt&gt;print&lt;/tt&gt; 的函式，在 OCaml 就有好多種，以便應付不同的型別：
&lt;ul&gt;
&lt;li&gt;&lt;tt&gt;print_string&lt;/tt&gt;&lt;/li&gt;
&lt;li&gt;&lt;tt&gt;print_int&lt;/tt&gt;&lt;/li&gt;
&lt;li&gt;&lt;tt&gt;print_float&lt;/tt&gt;&lt;/li&gt;
&lt;li&gt;&lt;tt&gt;print_char&lt;/tt&gt;&lt;/li&gt;
&lt;/ul&gt;
好玩吧！習慣一般語言寫法的人一開始還真的是容易搞錯。&lt;p&gt;

OCaml 對於基本內建的型別是會自動推斷（type inference），不必事先宣告，例如：unit、int、char、float、bool、string、ref……等等。&lt;p&gt;

說了一堆，其實用用看就知道了，有原則就有例外，這在 OCaml 常常會碰到。不過，他尚未被人商業化炒作，算是學術圈的程式語言（漸漸也有商業用途），所以看起來比較「清純」，將來會不會被人青睞，我不清楚，但好像
&lt;a href=&quot;http://en.wikipedia.org/wiki/Microsoft_.NET&quot;&gt;MS .NET&lt;/a&gt;
有個
&lt;a href=&quot;http://en.wikipedia.org/wiki/F_Sharp&quot;&gt;F#&lt;/a&gt;
是盡量和 OCaml 相容的。
   </description>
   <link>http://blog.bs2.to/post/EdwardLee/7277</link>
   <comments>http://blog.bs2.to/post/EdwardLee/7277</comments>
   <guid>http://blog.bs2.to/post/EdwardLee/7277</guid>
      <dc:creator>EdwardLee</dc:creator>
      
    <category>OCaml 筆記</category>
         <pubDate>Tue, 26 Dec 2006 23:21:13 +0800</pubDate>
   <source url="http://blog.bs2.to/rss/rss20/EdwardLee">LGJ Notes</source>
     </item>
          <item>
   <title>OCaml 筆記</title>
   <description>
    這裡還沒正式開張啦！&lt;p&gt;&lt;br/&gt;&lt;a href=&quot;http://caml.inria.fr/&quot;&gt;OCaml&lt;/a&gt; 是
&lt;a href=&quot;http://en.wikipedia.org/wiki/ML_programming_language&quot;&gt;ML(Meta-language)&lt;/a&gt; 的一個分支，是屬於一種
&lt;a href=&quot;http://en.wikipedia.org/wiki/Functional_programming&quot;&gt;functional language&lt;/a&gt;，但他不是純的 functional language。像
&lt;a href=&quot;http://en.wikipedia.org/wiki/LISP&quot;&gt;LISP&lt;/a&gt;
也是 functional language 的一種，但也都不是『純』的 functional language。&lt;p&gt;

Ocaml 可以使用三種 programming style，&lt;a href=&quot;http://en.wikipedia.org/wiki/Imperative_languages&quot;&gt;imperative&lt;/a&gt;、&lt;a href=&quot;http://en.wikipedia.org/wiki/Functional_programming&quot;&gt;purely functional&lt;/a&gt; 及
&lt;a href=&quot;http://en.wikipedia.org/wiki/Object_Oriented_Programming&quot;&gt;object oriented&lt;/a&gt;，可以使用其中的一種，也可以三種混合使用。不過，我不知道這是優點，還是缺點，大概是因人而異吧？&lt;p&gt;

我不是玩程式設計的，如果不小心看了這個筆記，造成精神恍忽、生長激素分泌失調、食欲不振、夜不安眠、盜冷汗、夫妻失和、不舉、冷感、白帶、攝護腺腫大、便秘……，那是你的問題。:-)&lt;p&gt;

本系列文章不定期開張，不定期修訂，不保證內容的正確性。&lt;p&gt;

&lt;br /&gt;&lt;p&gt;

&lt;p align=&quot;right&quot;&gt;蒙古包 2.0&lt;br /&gt;蒙古大夫 留&lt;br /&gt;2006.12.23&lt;/p&gt;
   </description>
   <link>http://blog.bs2.to/post/EdwardLee/7235</link>
   <comments>http://blog.bs2.to/post/EdwardLee/7235</comments>
   <guid>http://blog.bs2.to/post/EdwardLee/7235</guid>
      <dc:creator>EdwardLee</dc:creator>
      
    <category>OCaml 筆記</category>
         <pubDate>Sat, 23 Dec 2006 15:43:35 +0800</pubDate>
   <source url="http://blog.bs2.to/rss/rss20/EdwardLee">LGJ Notes</source>
     </item>
     </channel>
</rss>
